1、 发送无序广播:创建Intent,设置action,通过sendBroadcast(intent)就可以把广播发出去,当前的设备上只要有广播接收者注册了相同的action,就可以收到广播,并且在发广播的时候,可以通过Intent传递数据
2、接收无序广播:注册广播接收者,指定对应的action,就可以收到这个广播
接收的顺序 是否可以中断 发送的方法
有序广播 可以通过priority设置接收顺序 abortBroadcast()可以中断 sendOrderedBroadcast()
无序广播 大家一起收到 不可以中断 sendBroadcast()
3、如何区分有序广播和无序广播?接收到广播之后在onReceive方法中调用abortBroadcast()方法,如果没有异常说明是有序广播,如果报BroadcastReceiver trying to return result during a non-ordered broadcast异常说明是无序广播
******无序*****发送广播**********
创建一个发送广播项目和接收广播项目
在发送广播项目里首先定义布局:
******布局:****
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送广播"
android:onClick="send"
/>
在MainActivity 中
···
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View v){
Intent intent = new Intent();
intent.setAction("com.krr.broadcast");
intent.putExtra("key", "Hello");
sendBroadcast(intent);
}
}
···
*****接收广播********
首先创建一个类
···
//接受自定义广播
public class CustomRecevier extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.e("TAG", "接受广播"+intent.getStringExtra("key"));
}
}
···
*****广播接收器需要在清单文件里注册的
*******清单文件*********
···
<receiver android:name=".CustomRecevier">
<intent-filter >
<action android:name="com.krr.broadcast"/>
</intent-filter>
</receiver>
···
********效果图*********


**************有序发送广播*****
首先创建一个发送项目和接收项目
************发送***********
在布局中
···
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发广播"
android:onClick="send"
/>
···
MainActivity中**************
···
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View v){
Intent intent = new Intent();//通过intent携带数据
intent.setAction("com.krr.sendrice");
//收到广播时需要的权限
String receiverPermission=null;
//作为最终的广播接收者
BroadcastReceiver resultReceiver =null;
//处理最终的广播接收者用到Handler 如果传null会在主线程处理
Handler scheduler=null;
//初始化数据
String initialData="每人100斤";
sendOrderedBroadcast(intent, receiverPermission,
resultReceiver, scheduler, Activity.RESULT_OK,
initialData, null);//发送有序广播
}
}
···
*有序接收者 ****我们需要创建多个接收者看谁先谁后
这里我创建3个
**********代码如下*******
//州
public class StateRecevice extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String resultData=getResultData();//获取数据
Toast.makeText(context, resultData, Toast.LENGTH_SHORT).show();
setResultData("现在每人60斤");
// abortBroadcast();//终止广播继续发送 只有有序广播可以中断
}
}
**********第2个****接收者*******
···
//市
public class CityRecevice extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String resultData=getResultData();//获取数据
Toast.makeText(context, resultData, Toast.LENGTH_SHORT).show();
setResultData("现在每人40斤");
}
}
···
第3个**************
···
//县
public class TownRecevice extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String resultData=getResultData();//获取数据
Toast.makeText(context, resultData, Toast.LENGTH_SHORT).show();
setResultData("免除个人所得税");
}
}
···
都在清单文件里注册
···
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".StateRecevice">
<intent-filter android:priority="1000" >
<action android:name="com.krr.sendrice"/>
</intent-filter>
</receiver>
<receiver android:name=".CityRecevice">
<intent-filter android:priority="800" >
<action android:name="com.krr.sendrice"/>
</intent-filter>
</receiver>
<receiver android:name=".TownRecevice">
<intent-filter android:priority="500" >
<action android:name="com.krr.sendrice"/>
</intent-filter>
</receiver>
</application>
···
*******效果图**********


有序广播 优先级排列:
优先级高的有权中断 :
调用abortBroadcast();
方法
只有有序广播可以中断
有序广播的resultReceiver 作为最后的广播接收者
我们在发送广播中创建一个类Final 不需要清单文件注册 需要在MainActivity 中 new出来
···
//钦差大臣
public class Final extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String resultData =getResultData();
// Toast.makeText(context, "final:", + resultData,Toast.LENGTH_SHORT).show();
}
}
···
final最后执行
网友评论