Android 四大组件学习之BroadcastReceiver三

本节学习广播的分类。

广播分为无序广播和有序广播

无序广播:

广播发送者的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也是对无序广播没实用的。

这个大家能够试试。







posted @ 2017-04-20 16:15  yfceshi  阅读(166)  评论(0编辑  收藏  举报