自定义无序广播和有序广播
package com.example.order; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void send(View v){ Intent intent = new Intent(); intent.setAction("com.example.order.CCTV"); /* * intent :意图 * receiverPermission :指定接收者的权限 * resultReceiver :最终的广播接收者 * scheduler :Handler * initialCode :广播编码 * initialData :设置广播的数据,接收者getResultData()获取数据 * initialExtras :把数据设置到bundle,数据量大的时候用 */ //sendOrderedBroadcast(intent, null, new FinalReceiver(), new Handler(), 1, // "给每个码农补贴¥10000元", null);//后两个参数设置数据 //发送自定义有序广播 sendBroadcast(intent);//发送自定义无序广播 } /** * 无论签名的广播怎么拦截怎么终止 * 都是最后一个接收到广播的接收者 * 最终的广播接收者 * 不要在清单文件中注册 */ public class FinalReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { System.out.println("最终接收者:"+getResultData()); } } }
package com.example.code_famer; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class HighCoderFamerReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("com.example.order.CCTV".equals(intent.getAction())) { String result = getResultData(); System.out.println("高级码农:"+result); //拦截终止广播 // abortBroadcast(); //修改广播里的数据 setResultData("给每个码农补贴¥1000元"); } } }
package com.example.code_famer; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MiddleCoderFamerReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("com.example.order.CCTV".equals(intent.getAction())) { String result = getResultData(); System.out.println("中级码农:"+result); //修改广播里的数据 setResultData("给每个码农买个糖吃"); } } }
package com.example.code_famer; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class BirdCoderFamerReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("com.example.order.CCTV".equals(intent.getAction())) { String result = getResultData(); System.out.println("菜鸟码农:"+result); } } }
清单文件中: <receiver android:name="com.example.code_famer.HighCoderFamerReceiver" > <intent-filter android:priority="1000"> <action android:name="com.example.order.CCTV" /> </intent-filter> </receiver> <receiver android:name="com.example.code_famer.MiddleCoderFamerReceiver" > <intent-filter android:priority="0"> <action android:name="com.example.order.CCTV" /> </intent-filter> </receiver> <receiver android:name="com.example.code_famer.BirdCoderFamerReceiver" > <intent-filter android:priority="-1000"> <action android:name="com.example.order.CCTV" /> </intent-filter> </receiver>
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!