Search code examples
androidbroadcastreceiveraccessibility

Where to call unregisterReceiver in AccessibilityService


I'm a bit confused. An AccessibilityService can get all new incomming notifications and send information with a Broadcast to a BroadcastReceiver. My AccessibilityService does so

public void onServiceConnected() {
    // ...
    Communication c = new Communication();
    IntentFilter filter = new IntentFilter();

    filter.addAction("com.cilenco.lockscreen.notification.send");
    registerReceiver(c, filter);
}   

Intent intent=new Intent("com.cilenco.lockscreen.notification.send");

intent.putExtra("string1", string1);
intent.putExtra("string2", string2);

sendBroadcast(intent);

After I send the Broadcast the AccessibilityService is still alive. If a new notification is detected onServiceConnected is called again but then the Reciever is connected again in I never called

unregisterReceiver(c);

Where do I have to call this?


Solution

  • You call unregisterReceiver() when you no longer want to receive broadcasts. Convention is that it is called in your onPause(). Or sonner if you have no need for it anymore.