Start BroadCastReceiver From Activity

后端 未结 1 1351
半阙折子戏
半阙折子戏 2021-01-13 01:58

I want to start a BroadcastReceiver from an Activity. How can I do that?

相关标签:
1条回答
  • 2021-01-13 02:27

    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 讨论(0)
提交回复
热议问题