Android Broadcast
Android 广播
标准广播:发出一条广播后,几乎所有的接受器都会在同一时刻接受。这种广播的接受效率更高,但是缺点就是无法被截断
有序广播:广播发出后,同一时刻只有一个广播接收器可以接受,只有在该接收器接收完成后,才能继续传递。优先级高的先接收到,并可以进行拦截。
接收系统广播
Android 内置了许多系统广播,比如开机、电量变化、时间变化等等。广播接收器可以自由地对自己感兴趣的广播进行注册。注册方式分为:在代码中注册(动态注册)和在AndroidManifest.xml(静态注册)
动态注册
private IntentFilter intentFilter;
private NetworkChangeReceiver networkChangeReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
....
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECT_CHANGE");
networkChangeReceiver = new NetworkChangeReceiver();
//注册
registerReceiver(networkChangeReceiver,intentFilter);
....
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(networkChangeReceiver);
}
//实现 NetworkChangeReceiver
class NetworkChangeReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"network change",Toast.LENGTH_SHORT).show();
}
静态注册
动态注册可以很自由控制注册与注销,相对较为灵活,但是因为注册程序是放在onCreate函数中做的,所以必须运行了 app 后才可以进行接收
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hooltech.broastcasttest">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 添加相关的权限 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:name=".BootCompleteReceiver"
android:enabled="true"
android:exported="true">
<!-- 添加开机监听广播 -->
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
接收部分代码
public class BootCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context,"Boot Complete",Toast.LENGTH_LONG).show();
}
}
自定义广播
上面说的都是Android系统自带的广播,下面来自己实现自定义广播
标准广播
核心函数为 sendBroadcast 函数
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
sendBroadcast(intent);
广播接收部分就和之前的一致了
有序广播
核心函数为 sendOrderedBroadcast
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
sendBroadcast(intent);
注意在AndroidManifest.xml 中,需要添加优先级
<intent-filter android:priority="100">
<action android:name="com.example.broadcasttest.MY_BROADCAST" />
</intent-filter>
接受部分代码,添加 abortBroadcast 用于截断该条广播
public class BootCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context,"Boot Complete",Toast.LENGTH_LONG).show();
abortBroadcast();
}
}
本地广播
上面讨论的自定义广播还有一个问题,广播发送是全局发送的,这样就容易产生安全性问题,所以我们也可以实现本地广播。关键类LocalBroadcastManager
private LocalReceiver localReceiver;
private LocalBroadcastManager localBroadcastManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
localBroadcastManager = LocalBroadcastManager.getInstance(this);
...
intentFilter = new IntentFilter();
intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
localReceiver = new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver,intentFilter);
}
public class localReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context,"Boot Complete",Toast.LENGTH_LONG).show();
}
}