您的位置:首页 > 移动开发 > Android开发

Android有序广播_广播三

2012-12-05 00:09 423 查看
有序广播:

1.普通广播是完全异步的,可以在同一时刻(逻辑上)被所有接收者接收到,消息传递的效率比较高,但缺点是是不能控制接收的顺序,接收者不能将处理结果传递给下一个接收者,并且无法终止广播,而有序广播可以解决以上问题

2.有序广播是用权限控制多个广播接收器接收同一个广播时的接收顺序,并且可以中止广播(BroadcastReceiver类中的abortBroadcast函数),也可以在先监听到的广播接收器里增加数据传给下一个接收的广播接收器(不能修改之前的值)(使用setResultExtras和getResultExtras函数)

3.有序广播的发送,例:下面代码是发送了有序广播,该广播类型为farsight.inf,装有数据school:三人行,自定义广播权限为far.sight的广播

(例子1)

Intent intent = new Intent();

intent.setAction("farsight.inf");

Bundle bundle = new Bundle();

bundle.putString("school", "三人行");

intent.putExtras(bundle);

sendOrderedBroadcast(intent,"far.sight");

在发送广播时调用ContextWrapper类中的sendOrderedBroadcast函数(Activity继承了ContextWrapper),而不是sendBroadcast函数,并且sendOrderedBroadcast函数中有两个参数,第二个参数是为有序广播自定义权限,如果不想设置权限,可以传null。

4.定义接收有序广播的广播接收器,下面定义了三个广播接收器

public class OneBroadcastReceiver extends BroadcastReceiver {

 @Override

 public void onReceive(Context context, Intent intent) {

  Toast.makeText(context, "OneBroadcasReceiver...", Toast.LENGTH_LONG).show();

  Bundle bundle = intent.getExtras();//可以用new Bundle();运行结果也一样

  bundle.putString("school", "三人行私人培训");

  bundle.putInt("age", 100);

  this.setResultExtras(bundle);

 }

}

public class TwoBroadcastReceiver extends BroadcastReceiver {

 @Override

 public void onReceive(Context context, Intent intent) {

  System.out.println("TwoBroadcastReceiver");

  Bundle bundle = intent.getExtras();

  String school = bundle.getString("school");

  int age = bundle.getInt("age");

  String inf = "school:"+school+",age:"+age;

  System.out.println(inf);

  

  Bundle bundle2 =this.getResultExtras(true);

  String school2 = bundle2.getString("school");

  int age2 = bundle2.getInt("age");

  String inf2 = "school:"+school2+",age:"+age2;

  System.out.println(inf2);

  abortBroadcast();//中断广播

 }

}

public class ThreeBroadcastReceiver extends BroadcastReceiver {

 @Override

 public void onReceive(Context context, Intent intent) {

  System.out.println("ThreeBroadcastReceiver");

 }

}

在AndroidManifest.xml文件中配置广播接收器

<?xml version="1.0" encoding="utf-8"?>

<manifest .......>

 <application ......>

  ......

  <receiver android:name="OneBroadcastReceiver包名.OneBroadcastReceiver">

   <intent-filter android:priority="1000">

    <action android:name="farsight.inf"></action>

   </intent-filter>

  </receiver>

  <receiver android:name="TwoBroadcastReceiver包名.TwoBroadcastReceiver">

   <intent-filter android:priority="999">

    <action android:name="farsight.inf"></action>

   </intent-filter>

  </receiver>

  <receiver android:name="ThreeBroadcastReceiver包名.ThreeBroadcastReceiver">

   <intent-filter android:priority="998">

    <action android:name="farsight.inf"></action>

   </intent-filter>

  </receiver>

 </application>

 ......

 <uses-permission android:name="far.sight" />

 <permission android:protectionLevel="normal" android:name="far.sight" />

</manifest>

上面代码和配置文件的说明:

(1)上面的广播接收器在同一个项目中,所以配置在了同一个AndroidManifest.xml文件中,它们不在同一个项目中,则该配置在各自的AndroidManifest.xml文件中

(2)<uses-permission android:name="far.sight" />  //如果发广播时权限为null则不配置该配置

上面配置有序广播(farsight.inf)的自定权限(far.sight),要与发送广播时的sendOrderedBroadcast(intent,"far.sight");中的第二个参数一至

<permission android:protectionLevel="normal" android:name="far.sight" />//如果发广播时权限为null则不配置该配置

上面配置的是使用自定义的权限

(3)在广播接收器配置的<intent-filter>标签中都有属性 android:priority的配置,该属性配置的是广播接收器的优先级,优先级的值范围是-1000到+1000之间,优先级高的广播接收器会先接收到广播。

(4)运行结果分析:当(例子1)代码执行发送广播后,后面的三个广播接收器监听的结果是

OneBroadcastReceiver先监听到,因为优先级配置最高(1000),其中onReceive函数代码分析

public void onReceive(Context context, Intent intent) {

       Toast.makeText(context, "OneBroadcasReceiver...", Toast.LENGTH_LONG).show();

       Bundle bundle = intent.getExtras();//可以用new Bundle();运行结果也一样

       bundle.putString("school", "三人行私人培训");

       bundle.putInt("age", 100);

       this.setResultExtras(bundle);往广播中新增加数据

}

当OneBroadcastReceiver中onReceiver执行完后,TwoBroadcastReceiver监听到广播,因为优先级(999)次OneBroadcastReceiver,其中onReceiver函数会执行,

public void onReceive(Context context, Intent intent) {

     System.out.println("TwoBroadcastReceiver");

     Bundle bundle = intent.getExtras();

     //取出的值是发送广播中装的值

     String school = bundle.getString("school");//三人行

    //取出的值是0,因为发送广播时没有装age为键的值,虽然OneBroadcastReceiver中新增加了age为键的值,但是这种方式

    //取不到

    int age = bundle.getInt("age");//0

    String inf = "school:"+school+",age:"+age;

    System.out.println(inf);//school:三人行,age:0

    //取出OneBroadcastReceiver中往广播中新增加的值

    Bundle bundle2 =this.getResultExtras(true);

    String school2 = bundle2.getString("school");//三人行私人培训

    int age2 = bundle2.getInt("age");//100

    String inf2 = "school:"+school2+",age:"+age2;

    System.out.println(inf2);//school:三人行私人培训,age:100

    abortBroadcast();//中断广播由于该函数中断了监听到的广播,所以ThreeBroadcastReceiver也无法再收到广播了

}

同于TwoBroadcastReceive中onReceiver函数最后abortBroadcast()中断了广播,所以ThreeBroadcastReceiver无法收到广播。

4、有序广播的其它注意事项

(1)有序广播被广播接收器接收时,广播接收器注册也可以不设置监听优先级即<intent-filter android:priority="1000">中的android:priority属性不用配置,这样仍然可以监听到广播,只是这样一来监听的顺序就是另外的一会事了。也就是说,也可以按照监听普通的广播一样去监有序广播。

(2)怎么样才能判断一个广播是有序还是无序呢?

BoradcastReceiver中函数 public final boolean isOrderedBroadcast() ,可以判断当前进程正监听到的广播是否有序,如果有序返回true,无序返回false

(3)广播是否有序与广播是否有权限无关!当然两者也可以结合使用!

(4)多个广播接收器监听有序广播时,如果没有按照监听有序广播的形式去监听,即在注册广播接收器时不设置优先级,则不同项目中的广播接收器的监听顺序是任意的,但是同一个项目中的广播接收器是先注册的先监听到广播。例

在AndroidManifest.xml文件中配置广播接收器

<?xml version="1.0" encoding="utf-8"?>

<manifest .......>

 <application ......>

  ......

  <receiver android:name="OneBroadcastReceiver包名.OneBroadcastReceiver">

   <intent-filter>

    <action android:name="farsight.inf"></action>

   </intent-filter>

  </receiver>

  <receiver android:name="TwoBroadcastReceiver包名.TwoBroadcastReceiver">

   <intent-filter>

    <action android:name="farsight.inf"></action>

   </intent-filter>

  </receiver>

  <receiver android:name="ThreeBroadcastReceiver包名.ThreeBroadcastReceiver">

   <intent-filter>

    <action android:name="farsight.inf"></action>

   </intent-filter>

  </receiver>

 </application>

 ......

 <uses-permission android:name="far.sight" />

 <permission android:protectionLevel="normal" android:name="far.sight" />

</manifest>

以上配置在监听有序广播时,没有设置监听的优先级,所以监听到的顺序是OneBroadcastReceiver、TwoBroadcastReceiver、ThreeBroadcastReceiver

(5)经过我的测试发现有些板本,不管用sendBroadcast函数还是用sendOrderedBroadcast函数发送广播,都是有序的广播
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息