how can a manifest-declared broadcast receiver communicate with an activity or service?

I asked a couple of times for concrete scenarios, and you did not really provide any, so I am going to answer with an abstract scenario: suppose that your broadcast is designed to send a command to the service.

In that case, you could:

  1. Implement a singleton (here called CommandBus, for lack of a better idea of a name). Ideally, that singleton would be set up by a dependency inversion framework (e.g., Dagger/Hilt).

  2. Have CommandBus expose some reactive way to get commands to the service. If you were using Kotlin, that could be a SharedFlow. Since you are using Java, that could be an RxJava Observable, or perhaps a simple callback.

  3. Have MyAccessibilityService get access to the CommandBus singleton and use whatever you set up in step #2 to find out about commands.

  4. Have CommandBus expose an API to hand it commands to deliver to the service (e.g., a sendCommand() method).

  5. Have MyBroadcastReceiver get access to the CommandBus singleton and call the method from step #4 in onReceive().

At this point, when MyBroadcastReceiver receives a broadcast, it tells CommandBus to send the command, which then flows over to MyAccessibilityService, which goes and does something.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top