在发送广播之前,我们还是需要先定义一个广播接收器来准备接收此广播才行,不然发出去也是白发。因此新建一个MyBroadcastReceiver继承自BroadcastReceiver,代码如下所示:
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
}
}
这里当MyBroadcastReceiver收到自定义的广播时,就会弹出received in MyBroadcastReceiver的提示。然后在AndroidManifest.xml中对这个广播接收器进行注册:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcasttest"
android:versionCode="1"
android:versionName="1.0">
......
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
<uses-permission android:name="android.permission.RECEIVE_ROOT_COMPLETED">
<application
android:allowBackup="true"
android:icon="@mipmap/logo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
......
<receiver android:name=".MyBroadcastReceiver"
<intent-filter>
<action android:name="com.example.broadcasttext.MY_BROADCAST">
</intent-filter>
</receiver>
</application>
</manifest>
可以看到,这里让MyBroadcastReceiver接收一条值为com.example.broadcasttext.MY_BROADCAST的广播,因此待会儿在发送广播时,我们就需要发出这样的一条广播。
接下来修改activity_main.xml中的代码,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Broadcast"
/>
</LinearLayout>
这里在布局文件中定义了一个按钮,用于作为发送广播的触发点。然后修改MainActivity中的代码,如下所示:
public class MainActivity extends Activity{
......
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
@Ovrride
public void onClick(View v){
Intent intent = new Intent("com.example.broadcasttext.MY_BROADCAST");
sendBroadcast(intent);
}
});
......
}
......
}
可以看到,我们在按钮的点击事件里面加入了发送自定义广播的逻辑。首先构建出了一个Intent对象,并把要发送的广播的值传入,然后调用了Context的sendBroadcast()方法将广播发送出去,这样所有监听com.example.broadcasttext.MY_BROADCAST这条广播的广播接收器就会收到消息。此时发出去的广播就是一条标准广播。
这样我们就成功完成了发送自定义广播的功能。另外,由于广播是使用Intent进行传递的,因此你还可以在Intent中携带一些数据传递给广播接收器。
广播是一种可以跨进程的通信方式,这一点从前面接收广播的时候就可以看出来了。因此在我们应用程序内发出的广播,其他的应用程序应该也是可以收到的。为了验证这一点,我们需要再新建一个BroadcastTest2项目。
将项目创建好之后,还需要在这个项目下定义一个广播接收器,用于接收上以小姐中的自定义广播。新建AnotherBroadcastReceiver继承自BroadcastReceiver,代码如下所示:
public class AnotherBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceiver(Context context, Intent intent){
Toast.makeText(context, "received in AnotherBroadcastReceiver", Toast.LENGTH_SHORT).show();
}
}
这里仍然是在广播接收器的onReceiver()方法中弹出了一段文本信息。然后在AndroidManifest.xml中对这个广播接收器进行注册,代码如下所示:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcasttest2"
android:versionCode="1"
android:versionName="1.0">
......
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
<uses-permission android:name="android.permission.RECEIVE_ROOT_COMPLETED">
<application
android:allowBackup="true"
android:icon="@mipmap/logo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
......
<receiver android:name=".AnotherBroadcastReceiver"
<intent-filter>
<action android:name="com.example.broadcasttext.MY_BROADCAST">
</intent-filter>
</receiver>
</application>
</manifest>
可以看到,AnotherBroadcastReceiver同样接收的是com.example.broadcasttext.MY_BROADCAST这条广播。现在运行BroadcastTest2项目将这个程序安装到模拟器上,然后重新回到BroadcastTest项目的主界面,并点击一下Send Broadcast按钮,就会分别弹出两次提示信息。
这样就强有力的证明了,我们的应用程序发出的广播是可以被其他的应用程序接收到的。
不过到目前位置,程序发出的都还是标准广播,现在我们来尝试一下发送有序广播。关闭Broadcasttest2项目,然后修改MainActivity中的代码,如下所示:
public class MainActivity extends Activity{
......
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
@Ovrride
public void onClick(View v){
Intent intent = new Intent("com.example.broadcasttext.MY_BROADCAST");
sendOrderBroadcast(intent, null);
}
});
......
}
......
}
可以看到,发送有序广播只需要改动一行代码,即将sendBroadcast()方法改成sendOrderBroadcast()。sendOrderBroadcast()方法接收两个参数,第一个参数仍然是Intent,第二个参数是一个与权限相关的字符串,这里传入null就行了。现在重新运行程序,并点击Send Broadcast按钮,你会发现,两个应用程序仍然都可以接收到这条广播了。
看上去好像和标准广播没什么区别嘛,不过别忘了,这个时候的广播接收器是有先后顺序的,而且前面的广播接收器还可以将广播阶段,以组织其继续传播。
那么该如何设定广播接收器的先后顺序呢?当然是在注册的时候进行设定的了,修改AndroidManifest.xml中的代码。如下所示:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcasttest"
android:versionCode="1"
android:versionName="1.0">
......
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
<uses-permission android:name="android.permission.RECEIVE_ROOT_COMPLETED">
<application
android:allowBackup="true"
android:icon="@mipmap/logo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
......
<receiver android:name=".MyBroadcastReceiver"
<intent-filter android:priority="100">
<action android:name="com.example.broadcasttext.MY_BROADCAST">
</intent-filter>
</receiver>
</application>
</manifest>
可以看到,我们通过android:priority属性给广播接收器设置了优先级,优先级比较高的广播接收器就可以先接收到广播。这里将MyBroadcastReceiver的优先级设成了100,以保证它一定会在AnotherBroadcastReceiver之前收到广播。
既然已经获得了接收广播的优先权,那么MyBroadcastReceiver就可以选择是否允许广播继续传递了。修改MyBroadcastReceiver中的代码,如下所示:
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceiver(Context context, Intent intent){
Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
abortBroadcast();
}
}
如果在onReceiver()方法中调用了abortBroadcast()方法,就表示将这条广播截断,后面的广播接收器将无法再收到这条广播。现在重新运行程序,并点击一下Send Broadcast按钮,你会发现,只有MyBroadcastReceiver中的Toast信息能够弹出,说明这条广播经过MyBroadcastReceiver之后确实是终止传递了。