Android: Should I use BroadcastReceiver vs Messenger vs BindService vs Handler

I know this sounds like tons of questions around SO. But this is not so (although I may also be wrong).

I have a long service (running in a separate thread, using blutooth socket pooling for data in the OBD2 adapter every 5 seconds).
This service works in the same process and is a Foreground service.

The user launches this service through Activity. Then it connects to the Bluetooth device and launches data merging and saving in SQLiteDataBase. Then the user can minimize activity and do other things.
When he returns (if he can ever stop the service through the notification area button) to the application that he checks to see if the service is working, and if so, it launches another action that shows the data that is pulled from OBD2.

My question is what between this visualization activity and the service I have to use, and I mean recommended or correct:

  • Localbroadcast ? This is actually what I use. Each time a service retrieves some data, it sends a broadcast with data every time it has been pulled. Then onReceivecall in the method runOnUiThreadto update the corresponding view.
  • Courier ? As far as I know (I never used it), I have to send Messenger from Activity to the service (like a handler), and in the Service it should send messages with data that has been pulled. But from this, I would get RemoteObjectExceptionif the activity was destroyed (as I said, the user could just minimize the activity and then get the GCed). So, I probably need a way to send Messenger to the Service every time an Activity is created, and check if it is ok to use Messenger in the service every time (if possible, I never used that).
  • BindService? , , ? , , , Activity, , ?
  • ? ( , Messenger Handler, " Messenger, ).

/ , SO . , . , , .

!

EDIT: , Android, Android, , .

+4

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


All Articles