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

Android 四大组件学习之BroadcastReceiver三

2015-07-25 23:30 666 查看
本节学习广播的分类。 广播分为无序广播和有序广播

无序广播:

广播发送者的action与广播接收者的action都匹配的话,所以广播介绍者都可以收到这条广播,并且没有先后顺序,可以认为是同时收到

有序广播:

广播发送者的action与广播接收者的action都匹配的话,所以广播介绍者都可以收到这条广播,但是有先后顺序,高优先级的先收到

既然知道什么是无序广播和有序广播后, 那我们举例说明:

那我们模拟生活中一个例子说明。 某高校正在举行体操比赛。这时候我们伟大的计算机学院就开会了。 院长将几个班级的导员开会完,导员又组织各个班的班长开会,最后各个班级的班长给大家传达会议精神。

我们假设: 某某某高校校长是这次广播的发送者,计算机学院的院长,导员和各个班的班长是广播接收者

先用无序广播举例:

第一: 创建校长应用程序,也就是广播的发送者:

public class PresidentActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void sendBroadcast(View v)
{
Intent intent = new Intent();
intent.setAction("com.demo.SPORT_MEET_SPIRIT");
intent.putExtra("SPORT_MEET", "每个学生早上7点必须上早操");
sendBroadcast(intent);
}
}
第二: 创建计算机学校院长接收者,也就是广播的接收者

public class ComputerPresidentReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String spirit = intent.getExtras().getString("SPORT_MEET");
Log.i("TeacherReceiver", "校长说: "+spirit);
}

}


第三:创建导员接收者,也就是广播的介绍者

public class TeacherReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String spirit = intent.getExtras().getString("SPORT_MEET");
Log.i("TeacherReceiver", "院长说: "+spirit);
}
}


第三:创建最不听话的计算机同学接收者,也就是广播的接收者:

public class StudentReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//得到广播的数据
String spirit = intent.getExtras().getString("SPORT_MEET");
//显示
Log.i("StudentReceiver", "导员说: "+spirit);
}
}


此时清单文件中配置为:

<receiver android:name="com.demo.broadcastreceiveractivity.ComputerPresidentReceiver">
<intent-filter >
<action android:name="com.demo.SPORT_MEET_SPIRIT"></action>
</intent-filter>
</receiver>
<receiver android:name="com.demo.broadcastreceiveractivity.TeacherReceiver">
<intent-filter >
<action android:name="com.demo.SPORT_MEET_SPIRIT"></action>
</intent-filter>
</receiver>
<receiver android:name="com.demo.broadcastreceiveractivity.StudentReceiver">
<intent-filter >
<action android:name="com.demo.SPORT_MEET_SPIRIT"></action>
</intent-filter>
</receiver>


ok。当我们校长发送广播后:看看现象:



可以看到先是校长说,然后院长说,再然后是导员说。看起来好像挺有顺序的。但是我将配置文件的顺序改为:

<receiver android:name="com.demo.broadcastreceiveractivity.StudentReceiver">
<intent-filter >
<action android:name="com.demo.SPORT_MEET_SPIRIT"></action>
</intent-filter>
</receiver>
<receiver android:name="com.demo.broadcastreceiveractivity.TeacherReceiver">
<intent-filter >
<action android:name="com.demo.SPORT_MEET_SPIRIT"></action>
</intent-filter>
</receiver>
<receiver android:name="com.demo.broadcastreceiveractivity.ComputerPresidentReceiver">
<intent-filter >
<action android:name="com.demo.SPORT_MEET_SPIRIT"></action>
</intent-filter>
</receiver>


学生放在最前面注册,则结果为:



这时候顺序明显不对了。 这就是无序广播的特点,对于接受没有先后顺序。这明显和实际不符合。要想做到符合就必须用有序广播

举例有序广播:

首先: 给每个广播接受者设置权限,权限是-1000到1000。其中1000的优先级最高

<receiver android:name="com.demo.broadcastreceiveractivity.StudentReceiver">
<intent-filter android:priority="500">
<action android:name="com.demo.SPORT_MEET_SPIRIT"></action>
</intent-filter>
</receiver>
<receiver android:name="com.demo.broadcastreceiveractivity.TeacherReceiver">
<intent-filter android:priority="800">
<action android:name="com.demo.SPORT_MEET_SPIRIT"></action>
</intent-filter>
</receiver>
<receiver android:name="com.demo.broadcastreceiveractivity.ComputerPresidentReceiver">
<intent-filter android:priority="1000">
<action android:name="com.demo.SPORT_MEET_SPIRIT"></action>
</intent-filter>
</receiver>


其次: 发送广播的方式就的改变:

public void sendBroadcast(View v)
{
Intent intent = new Intent();
intent.setAction("com.demo.SPORT_MEET_SPIRIT");
//intent.putExtra("SPORT_MEET", "每个学生早上7点必须上早操");

//发送的广播为无序广播
//sendBroadcast(intent);

//发送有序广播
sendOrderedBroadcast(intent, null, null, null, 0, "每个学生早上7点必须上早操", null);
}


然后,就每个接收者做个调整:

计算机院长把校长的话给改了,校长说计算机要当第一必须6点半起来:

public class ComputerPresidentReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//如果发送的时候数据是通过intent发送的,就通过intent取得
//String spirit = intent.getExtras().getString("SPORT_MEET");

//如果不是,就是简单的字符串的话,可以通过getResultData得到
String spirit = getResultData();
Log.i("ComputerPresidentReceiver", "校长说: "+spirit);
setResultData("每个学生早上6点半必须上早操");
}

}
这时候广播到导员了,导员说计算机系必须当第一,6点起来:

public class TeacherReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//String spirit = intent.getExtras().getString("SPORT_MEET");
String spirit = getResultData();
Log.i("TeacherReceiver", "院长说: "+spirit);
setResultData("每个学生早上6点必须上早操");
}

}
这时候班长传达旨意来了:

public class StudentReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//得到广播的数据
//String spirit = intent.getExtras().getString("SPORT_MEET");
//显示
String spirit = getResultData();
Log.i("StudentReceiver", "导员说: "+spirit);
setResultData("谁爱上谁上去,反正我不去!");
}
}


这时候同学们就说: 谁爱上谁上去,反正我不去!

看一下运行效果:



这样校长的旨意就传达了。但是同学们到底该怎么办呢? 有一天学校的检查部门(也就是一帮臭b学生会,来检查上早操的人数)

这时候我们就需要对发送者在改造:

public void sendBroadcast(View v)
{
Intent intent = new Intent();
intent.setAction("com.demo.SPORT_MEET_SPIRIT");
//intent.putExtra("SPORT_MEET", "每个学生早上7点必须上早操");

//发送的广播为无序广播
//sendBroadcast(intent);

//发送有序广播
sendOrderedBroadcast(intent, null, new resultReceiver(), null, 0, "每个学生早上7点必须上早操", null);
}

//广播的最终接受者
class resultReceiver extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String spirit = getResultData();
Log.i("resultReceiver", "计算机上早情况:" + spirit);
}

}


演示效果为:



学生会一看,计算机系8点了还没人上早操,就几个胆小的去上了。

这就是有序广播,发送者发送后,第一个接受者可以对广播的内容修改,同时也可以终止广播的继续发送。但是广播的最终接受者是一定可以收到此广播的。

比如: 有一天导员比较忙,忘记了传达会议的精神

public class TeacherReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//String spirit = intent.getExtras().getString("SPORT_MEET");
String spirit = getResultData();
Log.i("TeacherReceiver", "院长说: "+spirit);

//由于太忙,忘了,也就不传播会议精神了
abortBroadcast();
//setResultData("每个学生早上6点必须上早操");
}
}


结果为:



导员没有给班长传达会议精神,同学们也没收到, 当然也没人去上早操。但是学生会还是会说到最终的广播消息的。

注意: 如果你发送的是无序广播的话,使用abortBroadcast();是无法终止广播的传送的,对无序广播是没有用的。 同样setResultData也是对无序广播没有用的。

这个大家可以试试。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息