文章标题 拦截有序广播
2017-06-05 09:40
295 查看
拦截有序广播程序对应的布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/stitch_one" tools:context="cn.edu.bzu.bordercast3.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="80dp" android:onClick="send" android:background="#f2d2f1f4" android:text="发送有序广播" android:textSize="20sp"/> </RelativeLayout>
上述布局文件中,定义一个Button按钮,并为按钮注册了点击事件send,当用户点击该按钮时,会发送一条有序广播。
MainActivitty代码
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void send(View view){ Intent intent=new Intent(); intent.setAction("Intercept_Stitch"); sendOrderedBroadcast(intent,null); } }
sendOrderBordercast(intent,null)用于发送一个有序广播,第一个参数指定意图,设置要发送的广播事件,第二个参数制定接受者的权限,如果不想让所有的接受者都看到,可以显式的指定接受者,不关心权限可以将其指定为null。
添加广播接收者
广播接收者1public class MyReceiverone extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i(" MyReceiverone","自定义广播接收者one,接受到了广播事件"); } }
广播接收者2
public class MyReceiverTwo extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i(" MyReceivertwo","自定义广播接收者two,接受到了广播事件"); } }
广播接收者3
public class MyReceiverthree extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i(" MyReceiverthree","自定义广播接收者three,接受到了广播事件"); } }
清单文件
<receiver android:name=".MyReceiverone"> <intent-filter android:priority="1000"> <action android:name="Intercept_Stitch"/> </intent-filter> </receiver> <receiver android:name=".MyReceiverTwo"> <intent-filter android:priority="200"> <action android:name="Intercept_Stitch"/> </intent-filter> </receiver> <receiver android:name=".MyReceiverthree"> <intent-filter android:priority="600"> <action android:name="Intercept_Stitch"/> </intent-filter> </receiver>
修改MyBroadcastReceiverTwo如下
public class MyReceiverTwo extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i(" MyReceivertwo","自定义广播接收者two,接受到了广播事件"); abortBroadcast(); Log.i(" MyReceivertwo","我是广播接收者two,广告被我终结了"); } }
若将广播接收者MyBroadcastReceiverTwo优先级同样设置为1000,并将MyBroadcastReceiverTwo注册在MyBroadcastReceiverOne前面
<receiver android:name=".MyReceiverTwo"> <intent-filter android:priority="1000"> <action android:name="Intercept_Stitch"/> </intent-filter> </receiver> <receiver android:name=".MyReceiverone"> <intent-filter android:priority="1000"> <action android:name="Intercept_Stitch"/> </intent-filter> </receiver> <receiver android:name=".MyReceiverthree"> <intent-filter android:priority="600"> <action android:name="Intercept_Stitch"/> </intent-filter> </receiver>
广播的优先级相同时,先注册的广播会先接受广播事件
相关文章推荐
- 拦截有序广播
- Android--广播的类型及拦截有序广播案例
- Android进阶之路 - BroadcaseReceiver(自定义广播、有序广播、无序广播、广播拦截、动态注册、静态注册)的详细使用方式
- 广播接收者——案例:拦截有序广播
- android,自定义广播,最终广播接收者,防止拦截广播,有序广播,无序广播
- 实现拦截一条有序广播
- 案例---拦截有序广播
- Android案例:通过简单的小案例理解有序广播的传递以及怎样将其拦截
- 实现拦截一条有序广播
- 案例-----拦截有序广播
- 文章标题 Android系统广播的发送短信和接收短信的一些用法
- 有序广播拦截
- BroadcastReceiver普通广播、有序广播、拦截广播、本地广播、Sticky广播、系统广播
- Android四大组件——BroadcastReceiver普通广播、有序广播、拦截广播、本地广播、Sticky广播、系统广播
- 拦截有序广播
- Android四大组件——BroadcastReceiver普通广播、有序广播、拦截广播、本地广播、Sticky广播、系统广播
- 广播--案例:拦截有序广播
- COPY FROM 文章标题 网络数据包拦截通用技术
- 广播的类型————拦截有序广播
- 拦截有序广播案例