当前位置:   article > 正文

发送有序广播_击按钮发送有序广播:“mybroadcast”,自定义三个广播接收器,并设置接收器的优先级

击按钮发送有序广播:“mybroadcast”,自定义三个广播接收器,并设置接收器的优先级
一、训练项目
掌握有序广播的使用
二、案例实现

1、拦截有序广播对应的布局文件如下(actiity_main.xml):
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="@drawable/stitch_one"
  8. tools:context="com.example.broadcast.MainActivity">
  9. <Button
  10. android:layout_height="wrap_content"
  11. android:layout_width="wrap_content"
  12. android:onClick="send"
  13. android:layout_marginTop="50dp"
  14. android:layout_marginLeft="110dp"
  15. android:textSize="20sp"
  16. android:text="发送有序广播"
  17. />
  18. </RelativeLayout>
MainActivity的代码如下:
  1. package com.example.broadcast;
  2. import android.content.Intent;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. public class MainActivity extends AppCompatActivity {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. }
  12. public void send(View view){
  13. Intent intent=new Intent();
  14. intent.setAction("Intercept_Stitch");
  15. sendOrderedBroadcast(intent,null);
  16. }
  17. }
2、添加广播接收者
MyBroadcastReceiverOne,
MyBroadcastReceiverTwo,
MyBroadcastReceiverThree不同的接收者打印不同的提示信息。
广播接收者MyBroadcastReceiverOne的代如下:
  1. package com.example.broadcast;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. public class MyBroadcastReceiverOne extends BroadcastReceiver {
  7. @Override
  8. public void onReceive(Context context, Intent intent) {
  9. Log.i("MyBroadcastReceiverOne","自定义的广播接收者One,接收到了广播事件");
  10. }
  11. }

广播接收者MyBroadcastReceiverTwo的代如下:
  1. package com.example.broadcast;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. public class MyBroadcastReceiverTwo extends BroadcastReceiver {
  7. @Override
  8. public void onReceive(Context context, Intent intent) {
  9. Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件");
  10. }
  11. }


广播接收者MyBroadcastReceiverThree的代如下:
  1. package com.example.broadcast;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. public class MyBroadcastReceiverThree extends BroadcastReceiver {
  7. @Override
  8. public void onReceive(Context context, Intent intent) {
  9. Log.i("MyBroadcastReceiverThree","自定义的广播接收者Three,接收到了广播事件");
  10. }
  11. }

三个广播接收者创建成功之后,需要在清单文件中注册他们并为它们制定不同的优先级,集体代码如下所示:
  1. <receiver
  2. android:name=".MyBroadcastReceiverOne"
  3. android:enabled="true"
  4. android:exported="true">
  5. <intent-filter android:priority="1000">
  6. <action android:name="Intercept_Stitch" />
  7. </intent-filter>
  8. </receiver>
  9. <receiver
  10. android:name=".MyBroadcastReceiverTwo"
  11. android:enabled="true"
  12. android:exported="true" >
  13. <intent-filter android:priority="200">
  14. <action android:name="Intercept_Stitch" />
  15. </intent-filter>
  16. </receiver>
  17. <receiver
  18. android:name=".MyBroadcastReceiverThree"
  19. android:enabled="true"
  20. android:exported="true">
  21. <intent-filter android:priority="600">
  22. <action android:name="Intercept_Stitch" />
  23. </intent-filter>
  24. </receiver>

3、运行程序拦截有序广播





问题一:若将广播接收者 MyBroadcastReceiverTwo 优先级同样设置为 1000 ,并将 M yBroadcastReceiverTwo 注册在 MyBroadcastReceiverOne 前面,再来运行程序,观察结果,你能得出什么结论?  
结果:
 
结论:说明当两个广播接收者的优先级相等时,先注册的广播接收者会先接收到事件。

修改MyBroadcastReceiverTwo如下,观察结果,你又可以得出什么结论?

  1. package com.example.broadcast;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. public class MyBroadcastReceiverTwo extends BroadcastReceiver {
  7. @Override
  8. public void onReceive(Context context, Intent intent) {
  9. Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件");
  10. abortBroadcast();
  11. Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,广播被我终结啦");
  12. }
  13. }

结果:


结论:abortBroadcast()语句用于终止有序广播,当程序执行完该句后,广播事件将会终结,不会再向下传递。




 

MyBroadcastReceiverTwo的代如下:
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/350198
推荐阅读
相关标签
  

闽ICP备14008679号