安卓自定义广播接收者
1.定义广播的key
public static final String BROADCAST_LOGIN_OUT= "login_account_out";
2.创建广播接收类对象
// 初始化的时候注册广播接收类型
private void registerReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(AppCacheKey.BROADCAST_LOGIN_OUT);
// 调用activity的方法注册广播接收者
registerReceiver(myReceiver, filter);
}
// 退出之前调用activity的方法注销广播接收者
unregisterReceiver(myReceiver);
// 创建广播接收者
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BROADCAST_LOGIN_OUT)) {
//通知
需要实现业务的代码
}
}
};
3.调用activity的方法发送广播
sendBroadcast(new Intent(BROADCAST_LOGIN_OUT));