Manage the service API from BroadcastReceiver

I get an error when I try to associate the onReceive () of the recipient with the local service before I write the API to it.

"IntentReceiver components are not allowed to contact services"

What is the best way to get some of the data from a service while receiving a radio transmitter.

Thank you very much.

PS I need to receive a response synchronously, that is, I am waiting for a response from the service, so a callback may not be possible.

+4
source share
1 answer

What is the best way to get some of the data from a service while receiving a radio transmitter.

If the BroadcastReceiver is created from something else (for example, an action) and configured using registerReceiver() , then โ€œsomething elseโ€ is what should be required for the service and do the work.

If BroadcastReceiver is a component registered in the manifest, you need to reconsider your approach to any problem that you are trying to solve. These types of BroadcastReceivers cannot bind to services and cannot spend a lot of time doing work. If the BroadcastReceiver cannot do its job in less than, say, 10 ms, it should delegate control to the service via startService() . This service can then do the work on the background thread.

PS I need to receive a response synchronously, that is, I am waiting for a response from the service

See above.

+4
source

Source: https://habr.com/ru/post/1339048/


All Articles