开发者

Start BroadCastReceiver From Activity

开发者 https://www.devze.com 2023-02-06 10:33 出处:网络
I want to start a BroadcastReceiver from开发者_StackOverflow an Activity. How can I do that?Define your BroadcastReceiver:

I want to start a BroadcastReceiver from开发者_StackOverflow an Activity. How can I do that?


Define your BroadcastReceiver:

private final BroadcastReceiver             receiver
    = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Do something
    }
};

Register your receiver in onResume:

@Override
protected void onResume() {
    super.onResume();

    IntentFilter filter = new IntentFilter();
    filter.addAction("SOME_ACTION");
    registerReceiver(receiver, filter);
}

Unregister the receiver in onPause:

@Override
protected void onPause() {
    super.onPause();

    unregisterReceiver(receiver);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消