一、全局广播动态使用
1、注册广播
BroadcastReceiver receiver= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.BD_EXIT_APP)) {
finish();
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(Constants.BD_EXIT_APP);
registerReceiver(receiver, filter);
2、发送广播
Intent intent = new Intent();
intent.setAction(Constants.BD_EXIT_APP);
sendBroadcast(intent);
3、注销广播
@Override
protected void onDestroy() {
super.onDestroy();
if (receiver!= null) {
unregisterReceiver(receiver);
}
}