开发者

Start BroadCastReceiver From Activity

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜