我想从一个
Activity
开始一个 BroadcastReceiver
。 我该怎么做?定义你的BroadcastReceiver:
private final BroadcastReceiver receiver
= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Do something
}
};
在onResume中注册您的接收器:
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction("SOME_ACTION");
registerReceiver(receiver, filter);
}
在onPause中取消注册接收器:
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}