Dynamically register/unregister a broadcast receiver in android

前端 未结 5 1858
我寻月下人不归
我寻月下人不归 2020-12-16 03:03

I want to dynamically register and unregister my receiver class with the broadcast: \"android.net.wifi.STATE_CHANGE\" This works very well if I do this in the manifest. But

相关标签:
5条回答
  • 2020-12-16 03:49

    Perhaps I'm a bit too late, but the problem lies on the fact that you are setting the receiver = null in your onPause method, and then never setting it again. You are also trying to register it in your onResume method but only if it is null, which makes no sense too.

    You should change the logic where you set/test the null value of the receiver, to instead just use a boolean variable to keep track of the receiver status (if it's registered or not).

    0 讨论(0)
  • 2020-12-16 03:49
    public void registerBroadcastReceiver(View view) {
    
        this.registerReceiver(broadCastReceiver, new IntentFilter(
            "android.intent.action.TIME_TICK"));
    }
    
    
    public void unregisterBroadcastReceiver(View view) {
    
        this.unregisterReceiver(broadCastReceiver);
    }
    
    0 讨论(0)
  • 2020-12-16 03:57

    The LocalBroadcastManager class is used to register for and send broadcasts of Intents to local objects within your process. This is faster and more secure as your events don't leave your application.

    The following example shows an activity which registers for a customer event called my-event.

    @Override
    public void onResume() {
    super.onResume();
    
       // Register mMessageReceiver to receive messages.
       LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
         new IntentFilter("my-event"));
    }
    
    // handler for received Intents for the "my-event" event 
    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
       // Extract data included in the Intent
       String message = intent.getStringExtra("message");
       Log.d("receiver", "Got message: " + message);
     }
    };
    
    @Override
    protected void onPause() {
       // Unregister since the activity is not visible
       LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
       super.onPause();
    } 
    // This method is assigned to button in the layout
    // via the onClick property
    public void onClick(View view) {
       sendMessage();
    }
    
    // Send an Intent with an action named "my-event". 
    private void sendMessage() {
      Intent intent = new Intent("my-event");
      // add data
      intent.putExtra("message", "data");
      LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    } 
    
    0 讨论(0)
  • 2020-12-16 03:57

    Don't add dynamic broadcast receiver in onReceive on broadcast file. Add it on first activity or main activity of your application. If you needed it only when your application is open. But if you need it always received response just added it on manifest file

    Register dynamic broadcast receiver on main activity

    MyReceiver reciver;
    
    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
        intentFilter.addAction("android.net.wifi.STATE_CHANGE");
        reciver = new MyReceiver();
        registerReceiver(reciver, intentFilter);
    }
    

    Unregister that broadcast receiver on activity stop or closed

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(reciver);
    }
    
    0 讨论(0)
  • 2020-12-16 03:59

    use the below methods to register/unregister your receiver:

    registerReceiver(BroadcastReceiver receiver, new IntentFilter("android.net.wifi.STATE_CHANGE"));
    unregisterReceiver(BroadcastReceiver receiver);
    

    For reference have a look at this

    0 讨论(0)
提交回复
热议问题