标签:add nbsp uri equal and set 按钮 tac tle
在之前的博客,Android-广播概念,中介绍了(广播和广播接收者)可以组件与组件之间进行通讯,有两种类型的广播(无序广播 和 有序广播),这篇博客就来讲解有序广播的代码实现:
有序广播:接收者 可以自己设置接受的顺序,所以叫有序广播
package liudeli.croadcast1; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * 定义 接收者 */ public class MyBroadcastReceiver extends BroadcastReceiver { private final String TAG = MyBroadcastReceiver.class.getSimpleName(); /** * 接收的方法 * @param context 传递过来的上下文 * @param intent 传递过来的意图,可以获取很多信息 */ @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "【111】intent.getAction():" + intent.getAction()); Log.d(TAG, "【111】intent.getDataString():" + intent.getDataString()); // 真实开发中是这样写的 if ("my.MyBroadcastReceiver.custom.action2".equals(intent.getAction())) { String data = intent.getDataString(); String[] dataArray = data.split(":"); Log.d(TAG, "【111】data:" + dataArray[1]); Log.d(TAG, "【111】music:" + intent.getStringExtra("music")); } else if ("xxx.action".equals(intent.getAction())) { // 做相应的业务逻辑处理 // ... } else if ("xxx.action".equals(intent.getAction())) { // 做相应的业务逻辑处理 // ... } } }
package liudeli.croadcast1; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * 定义 接收者 */ public class MyBroadcastReceiver2 extends BroadcastReceiver { private final String TAG = MyBroadcastReceiver.class.getSimpleName(); /** * 接收的方法 * @param context 传递过来的上下文 * @param intent 传递过来的意图,可以获取很多信息 */ @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "【222】intent.getAction():" + intent.getAction()); Log.d(TAG, "【222】intent.getDataString():" + intent.getDataString()); // 真实开发中是这样写的 if ("my.MyBroadcastReceiver.custom.action2".equals(intent.getAction())) { String data = intent.getDataString(); String[] dataArray = data.split(":"); Log.d(TAG, "【222】data:" + dataArray[1]); Log.d(TAG, "【222】music:" + intent.getStringExtra("music")); } else if ("xxx.action".equals(intent.getAction())) { // 做相应的业务逻辑处理 // ... } else if ("xxx.action".equals(intent.getAction())) { // 做相应的业务逻辑处理 // ... } } }
package liudeli.croadcast1; import android.app.Activity; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Bundle; import android.view.View; public class NewActivity2 extends Activity { private MyBroadcastReceiver myBroadcastReceiver; private MyBroadcastReceiver2 myBroadcastReceiver2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new2); /** * 【Java代码方式订阅 接收者】 * 唯一可以new的组件就是 广播 接收者 */ myBroadcastReceiver = new MyBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("my.MyBroadcastReceiver.custom.action.sendOrderBroadcast"); intentFilter.addDataScheme("zhangyusheng"); // 注意:这里不能加 : intentFilter.setPriority(88); // 在订阅的时候给自己设置优先级别 -1000 --> 1000 registerReceiver(myBroadcastReceiver, intentFilter); /** * 【Java代码方式订阅 接收者】 * 唯一可以new的组件就是 广播 接收者 */ myBroadcastReceiver2 = new MyBroadcastReceiver2(); IntentFilter intentFilter2 = new IntentFilter(); intentFilter2.addAction("my.MyBroadcastReceiver.custom.action.sendOrderBroadcast"); intentFilter2.addDataScheme("zhangyusheng"); // 注意:这里不能加 : intentFilter2.setPriority(1000); // 在订阅的时候给自己设置优先级别 -1000 --> 1000 registerReceiver(myBroadcastReceiver2, intentFilter2); } /** * 发送有序广播 */ public void sendOrderBroadcast(View view) { /** * 接收者是这样订阅的:怎么订阅,就怎么发送 * IntentFilter intentFilter = new IntentFilter(); * intentFilter.addAction("my.MyBroadcastReceiver.custom.action.sendOrderBroadcast"); * intentFilter.addDataScheme("zhangyusheng"); // 注意:这里不能加 : */ Intent intent = new Intent(); intent.setAction("my.MyBroadcastReceiver.custom.action.sendOrderBroadcast"); intent.setData(Uri.parse("zhangyusheng:亚洲第一男高音")); // 注意:要加 : intent.putExtra("music", "我期待 作词作曲 张雨生"); // 而外携带值 // 参数一:意图 参数二:谁能接收我的广播(自定义权限) sendOrderedBroadcast(intent, null); } /** * Java方式订阅接收者后 必须要解除订阅 */ @Override protected void onDestroy() { super.onDestroy(); if (myBroadcastReceiver != null) unregisterReceiver(myBroadcastReceiver); if (myBroadcastReceiver2 != null) unregisterReceiver(myBroadcastReceiver2); } }
现在就是有序广播 接收者 的信息:【222】先打印 先接受 是因为设置了优先级别:1000 ,而【111】后接受 是因为设置了优先级别:88
12-17 08:11:45.536 4165-4165/liudeli.croadcast1 D/MyBroadcastReceiver: 【222】intent.getAction():my.MyBroadcastReceiver.custom.action.sendOrderBroadcast
12-17 08:11:45.536 4165-4165/liudeli.croadcast1 D/MyBroadcastReceiver: 【222】intent.getDataString():zhangyusheng:亚洲第一男高音
12-17 08:11:45.550 4165-4165/liudeli.croadcast1 D/MyBroadcastReceiver: 【111】intent.getAction():my.MyBroadcastReceiver.custom.action.sendOrderBroadcast
12-17 08:11:45.550 4165-4165/liudeli.croadcast1 D/MyBroadcastReceiver: 【111】intent.getDataString():zhangyusheng:亚洲第一男高音
标签:add nbsp uri equal and set 按钮 tac tle
原文地址:https://www.cnblogs.com/android-deli/p/10134328.html