自定义广播和广播优先级

Posted zhongyinghe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义广播和广播优先级相关的知识,希望对你有一定的参考价值。

1、发送广播代码

 1 package com.example.alame;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.content.Intent;
 6 import android.view.View;
 7 
 8 public class MainActivity extends Activity {
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_main);
14     }
15 
16     public void click(View view){
17         Intent intent = new Intent();
18         intent.setAction("com.exemple.oooxxx");
19         //发送一条无序的广播;如果是无序的广播,则所有的广播接收者都能够接收到这个广播事件
20         //sendBroadcast(intent);
21         //如果广播是有序的发送出去,则广播接受者按优先级来接收广播事件;有序广播特点:高优先级的广播接收者可以终止掉广播事件
22         //sendOrderedBroadcast(intent, null);
23         //这种方式的FinalBroadcast广播接受者无论如何都会接收到广播
24         sendOrderedBroadcast(intent, null, new FinalBroadcast(), null, 0, null, null);
25     }
26 }

2、FinalBroadcast是无需在清单文件中进行配置的,并且无论如何都会接收到广播

 1 package com.example.alame;
 2 
 3 import android.content.BroadcastReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6 
 7 public class FinalBroadcast extends BroadcastReceiver {
 8 
 9     @Override
10     public void onReceive(Context context, Intent intent) {
11         // TODO Auto-generated method stub
12          System.out.println("我是最后接收者");
13     }
14 
15 }

 

 

3、测试接收自定义广播事件代码

1)清单文件

 1  <receiver android:name=".MyBroadcastReceiver">
 2             <intent-filter android:priority="1000">
 3                 <action android:name="com.exemple.oooxxx"/>
 4             </intent-filter>
 5         </receiver>
 6          <receiver android:name=".MyBroadcastReceiver2">
 7             <intent-filter android:priority="500">
 8                 <action android:name="com.exemple.oooxxx"/>
 9             </intent-filter>
10         </receiver>
11          <receiver android:name=".MyBroadcastReceiver3">
12             <intent-filter android:priority="100">
13                 <action android:name="com.exemple.oooxxx"/>
14             </intent-filter>
15         </receiver>

在清单文件中设置了优先级

2)广播接收者代码

 1 package com.example.module3g;
 2 
 3 import android.content.BroadcastReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6 import android.widget.Toast;
 7 
 8 public class MyBroadcastReceiver extends BroadcastReceiver {
 9 
10     @Override
11     public void onReceive(Context context, Intent intent) {
12         // TODO Auto-generated method stub
13         System.out.println("receriver 1");
14         Toast.makeText(context, "接收到频道", 0).show();
15         //该函数会终止掉广播,造成低优先级的广播接受者接收不到广播
16         //abortBroadcast();
17     }
18 
19 }
 1 package com.example.module3g;
 2 
 3 import android.content.BroadcastReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6 import android.widget.Toast;
 7 
 8 public class MyBroadcastReceiver2 extends BroadcastReceiver {
 9 
10     @Override
11     public void onReceive(Context context, Intent intent) {
12         // TODO Auto-generated method stub
13         System.out.println("receriver 2");
14         Toast.makeText(context, "接收到频道2", 0).show();
15     }
16 
17 }
 1 package com.example.module3g;
 2 
 3 import android.content.BroadcastReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6 import android.widget.Toast;
 7 
 8 public class MyBroadcastReceiver3 extends BroadcastReceiver {
 9 
10     @Override
11     public void onReceive(Context context, Intent intent) {
12         // TODO Auto-generated method stub
13         System.out.println("receriver 3");
14         Toast.makeText(context, "接收到频道3", 0).show();
15     }
16 
17 }

 

以上是关于自定义广播和广播优先级的主要内容,如果未能解决你的问题,请参考以下文章

Android四大组件详解

BroadCast Receiver的使用

android 广播自定义广播接收问题

自定义广播(BroadcastReceiver)事件 --Android开发

安卓自定义广播没有响应

安卓自定义广播没有响应