文章目录
- 发送标准广播
- 发送有序广播
发送标准广播
先定义一个BroadcastReceiver来准备接收广播,新建一个MyBroadcastReceiver。
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
}
}
在AndroidManifest.xml中对BroadcastReceiver进行修改。
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<!--让MyBroadcastReceiver接收一条值为com.example.broadcasttest.MY_BROADCAST的广播-->
<action android:name="android.intent.action.MY_BROADCAST" />
</intent-filter>
</receiver>
修改activity_main.xml中的代码。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Broadcast"
/>
</LinearLayout>
修改MainActivity中的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
intent.setComponent(new ComponentName("com.example.broadcasttest", "com.example.broadcasttest.MyBroadcastReceiver"));
sendBroadcast(intent);
}
});
}
发送有序广播
新建一个项目,创建AnotherBroadcastReceiver。
public class AnotherBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "received in AnotherBroadcastReceiver", Toast.LENGTH_SHORT).show();
}
}
在AndroidManifest.xml中对这个BroadcastReceiver的配置进行修改。
<receiver
android:name=".AnotherBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST" />
</intent-filter>
</receiver>
AnotherBroadcastReceiver同样接收的是com.example.broadcasttest.MY_BROADCAST这条广播。现在重新运行程序,并点击“Send Broadcast”按钮,就会分别弹出两次提示信息。
修改BroadcastTest项目中MainActivity的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Log.d(TAG, "onClick: ");
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
intent.setComponent(new ComponentName("com.example.broadcasttest", "com.example.broadcasttest.MyBroadcastReceiver"));
// 修改代码
sendOrderedBroadcast(intent, null);
// --------
}
});
}
发送有序广播只需要改动一行代码,将sendBroadcast()方法改成sendOrderedBroadcast()方法。sendOrderedBroadcast()方法接收两个参数:第一个参数仍然是Intent;第二个参数是一个与权限相关的字符串,这里传入null就行了。
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<!--让MyBroadcastReceiver接收一条值为com.example.broadcasttest.MY_BROADCAST的广播-->
<action android:name="android.intent.action.MY_BROADCAST" />
</intent-filter>
</receiver>
通过android:priority属性给BroadcastReceiver设置优先级,优先级比较高的BroadcastReceiver就可以先收到广播。这里将MyBroadcastReceiver的优先级设成了100,以保证它一定会在AnotherBroadcastReceiver之前收到广播。
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: receive in MyBroadcastReceiver");
Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
abortBroadcast();
}
}
再在MyBroadcastReceiver中使用了abortBroadcast()方法将这条广播截断,后面的BroadcastReceiver将无法再接收到这条广播。重新运行程序,并点击“Send Broadcast”按钮,此时只有MyBroadcastReceiver中的Toast信息能够弹出,说明这条广播经过MyBroadcastReceiver之后确实终止传递了。