Android中的每个应用程序,都可以对自己关注的内容注册广播,当关注的内容发生变化时,可以通过广播监听到。这些内容可能来自系统,也可能来自其他应用程序,或本程序的某些数据变化。
注意
1.广播所在的程序关闭后,依旧可以收听,监听到广播。
2.在广播中执行耗时操作,必须开线程。
3.Android8.0取消了大部分静态注册广播,需要动态注册。
BoradcastReceiver基本用法
1.静态注册
在开发过程中,想要监听某个模块的数据变化,
根据此变化,来改动其它模块的数据时,就需要注册广播监听。
Android8.0后基本失效了
Activity提供API sendBroadcast()来发送一条广播。
1.private void sendBroadcast() {
//发送广播
Intent intent = new Intent(this, MyBroadcast.class);
intent.putExtra("key","发送的数据");
也可以通过隐式意图启动
//intent.setAction("com.w.review.broadcast.MY_BROADCAST");
sendBroadcast(intent);
}
2.mainfests中注册接收的广播
android:name:广播接收者。
enabled:是否启用这个广播。
exported:是否允许这个广播接收器,接收本程序外的广播。
priority:接收广播的优先级,优先级高的先接收,只对有序广播有效。
action:设置监听的广播发送者。
<application>
<receiver
android:name=".broadcast.MyBroadcast"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="com.w.review.broadcast.MY_BROADCAST" />
</intent-filter>
</receiver>
</application>
3.用自定义的广播接收器,接收sendBroadcast()发送的广播
public class MyBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String result = intent.getStringExtra("key");
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}
}
2.动态注册
1.在Activity中动态注册广播接收者
private void registerBroadcast() {
//自定义的广播接收者
mBroadcast = new MyBroadcast();
IntentFilter intentFilter = new IntentFilter(
"com.w.review.broadcast.MY_BROADCAST");
//注册广播
registerReceiver(mBroadcast, intentFilter);
}
注意:要在销毁的生命周期内解除注册,防止内存泄漏
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mBroadcast);
}
发送广播
private void sendBroadcast() {
//启动广播
Intent intent = new Intent(this, MyBroadcast.class);
intent.putExtra("key","发送的数据");
//也可以通过隐式意图启动
//intent.setAction("com.w.review.broadcast.MY_BROADCAST");
sendBroadcast(intent);
}
2.在mainfests中声明广播接收者
<receiver
android:name=".broadcast.MyBroadcast"
android:enabled="true"
android:exported="true">
</receiver>
3.用自定义的广播接收器,接收sendBroadcast()发送的广播
public class MyBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String result = intent.getStringExtra("key");
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}
}
广播分类
有序广播
1.是一种同步执行的广播,在广播发出后,同一时刻只会有一个广播接收器收到此消息。
2.通过优先级匹配,优先级高的广播接收者先获取广播,可以取消和修改广播的数据。
发送有序广播的API
sendOrderedBroadcast(intent,
null,
null,
null,
0,
"initData",
new Bundle());
参数:
1.意图
2.receiverPermission:设置查看广播权限
3.resultReceiver:所有有序广播发送结束后,此广播接收最后的数据。
直接new,不需要注册,不管广播是否终止,这个广播都能接收最后的数据。
4.scheduler:一般没用
5.initialCode: 初始化编码,区别你的广播
6.initialData: 初始化数据,广播的名称
7.initialExtras Bundle:传递各种数据
举个例子
三个有序广播接收者,监听一个数据源。
public class MainActivity extends AppCompatActivity {
private Button mBtnJump;
private Context mContext;
private BroadcastReceiver[] mReceivers = {new FirstBroadcast(),
new SecondBroadcast(), new ThirdBroadcast()};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initEvent();
registerBroadcast();
}
private void initEvent() {
mBtnJump.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendBroadcast();
}
});
}
private void initView() {
mContext = this;
mBtnJump = findViewById(R.id.btn_jump);
}
private void registerBroadcast() {
for (int i = 0; i < 3; i++) {
IntentFilter intentFilter =
new IntentFilter("com.w.review.broadcast.DEMO");
intentFilter.setPriority(10 - i);
registerReceiver(mReceivers[i], intentFilter);
}
}
private void sendBroadcast() {
Intent intent = new Intent("com.w.review.broadcast.DEMO");
Bundle bundle = new Bundle();
bundle.putString("key","传递的数据");
sendOrderedBroadcast(intent,null,
new FinalResultBroadcast(),
null,0,"初始化数据",
bundle);
}
@Override
protected void onDestroy() {
super.onDestroy();
for (int i = 0; i < 3; i++)
unregisterReceiver(mReceivers[i]);
}
}
mainfests中声明广播
<application>
<receiver android:name=".broadcast.FirstBroadcast"></receiver>
<receiver android:name=".broadcast.SecondBroadcast"></receiver>
<receiver android:name=".broadcast.ThirdBroadcast"></receiver>
<receiver android:name=".broadcast.FinalResultBroadcast"></receiver>
</application>
public class FirstBroadcast extends BroadcastReceiver {
private static final String TAG = "FirstBroadcast";
@Override
public void onReceive(Context context, Intent intent) {
String resultData = getResultData();
Bundle bundle = getResultExtras(true);
String value = bundle.getString("key");
Log.i(TAG, "初始化数据:" + resultData + ",传递参数:" + value);
bundle.putString("key","修改的传递数据");
setResultExtras(bundle);
setResultData("修改数据");
}
}
public class SecondBroadcast extends BroadcastReceiver {
private static final String TAG = "SecondBroadcast";
@Override
public void onReceive(Context context, Intent intent) {
String resultData = getResultData();
Bundle bundle = getResultExtras(true);
String value = bundle.getString("key");
Log.i(TAG, "初始化数据:" + resultData + ",传递参数:" + value);
bundle.putString("key","修改的传递数据");
setResultExtras(bundle);
setResultData("修改数据");
abortBroadcast();//中断传递
}
}
public class ThirdBroadcast extends BroadcastReceiver {
private static final String TAG = "ThirdBroadcast";
@Override
public void onReceive(Context context, Intent intent) {
String resultData = getResultData();
Bundle bundle = getResultExtras(true);
String value = bundle.getString("key");
Log.i(TAG, "初始化数据:" + resultData + ",传递参数:" + value);
}
}
public class FinalResultBroadcast extends BroadcastReceiver {
private static final String TAG = "FinalResultBroadcast";
@Override
public void onReceive(Context context, Intent intent) {
String resultData = getResultData();
Bundle bundle = getResultExtras(true);
String value = bundle.getString("key");
Log.i(TAG, "最终结果数据:" + resultData + ",最终传递参数:" + value);
}
}
结果
I/FirstBroadcast: 初始化数据:初始化数据,传递参数:传递的数据
I/SecondBroadcast: 初始化数据:修改数据,传递参数:修改的传递数据
I/FinalResultBroadcast: 最终结果数据:修改数据,最终传递参数:修改的传递数据
无序广播
1.异步执行的广播,在广播发出后,所有的广播接收器几乎都会同一时刻收到。
sendBroadcast(Intent intent);
本地广播
发出的广播只能够在应用程序内部传播,只能接收来自本应用的广播。
可以无需在mainfests中声明。
public class MainActivity extends AppCompatActivity {
private Button mBtnJump;
private Context mContext;
private MyLocalBroadcast mBroadcast;
private LocalBroadcastManager mLocalBroadcastManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initEvent();
registerBroadcast();
}
private void initEvent() {
mBtnJump.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendBroadcast();
}
});
}
private void initView() {
mContext = this;
mBtnJump = findViewById(R.id.btn_jump);
}
private void registerBroadcast() {
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter("com.w.review.broadcast.DEMO");
mBroadcast = new MyLocalBroadcast();
mLocalBroadcastManager.registerReceiver(mBroadcast, intentFilter);
}
private void sendBroadcast() {
Intent intent = new Intent("com.w.review.broadcast.DEMO");
intent.putExtra("key", "传递数据");
mLocalBroadcastManager.sendBroadcast(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocalBroadcastManager.unregisterReceiver(mBroadcast);
}
}
public class MyLocalBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String res = intent.getStringExtra("key");
Toast.makeText(context,res,Toast.LENGTH_LONG).show();
}
}