在Activity中发送接收广播
先定义广播接收类
public class MyCustomReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
executeMethod();
}
}
然后要接收
//创建接收对象
MyCustomReceiver myCustomReceiver = new MyCustomReceiver();
//创建过滤器,添加action
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Constants.ACTION_CUSTOM);
//注册广播
registerReceiver(myCustomReceiver, intentFilter);
在特定的地方发送
Intent intent = new Intent();
intent.setAction(Constants.ACTION_CUSTOM);
sendBroadcast(intent);