4

I know this look like tons of question around SO. But it's not (although I can also be wrong).

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

The user start this Service through an Activity. It then connect to the Bluetooth device and start pooling and saving data to a SQLiteDataBase. The user can then minimize the activity and do other stuff.
When he returns (if ever, he can stop the service through a notification area button) to the application it checks if the Service is running and if so, it starts another Activity which show the data that is being pulled from the OBD2.

My question is, between this visualization Activity and the Service should I use and by this I mean the recommended or the right one:

  • LocalBroadcast? This is actually what I am using. Every time the service pull some data, it sends a broadcast with the data everytime it was pulled. Then in the onReceive method call runOnUiThread to update the respective View.
  • Messenger? As far as I know (never used it) I should send a Messenger from the Activity to the Service (much like a Handler) and in the Service it should send the Messages with the data pulled. But from this I would get a RemoteObjectException if the Activity was destroyed (like I said, the user could just minimized the activity and then it got GCed). So, I would probably need a way of sending the Messenger to the Service every time the Activity gets created and check if it's ok to use the messenger form the Service every time (if that's even possible, I've never used this).
  • BindService? Should I bind to the service when I open the Activity and then get the data directly from methods in the Service? But this would probably mean I would have another thread in the Activity gets this data from the Service every time, right?
  • Handler? (for a moment now I realize don't know the difference between Messenger and Handler, should it be that "use Messenger when Service runs in another process and Handler otherwise)

I've seen/read a lot of answers here in SO and through the web in general. But in the end I don't see a ultimate answer for my case. But I'm sorry if this is just because I couldn't figure it out.

Thanks in advance!

EDIT: forgot to mention, I would rather make use only support libraries and android framework stuff, I'm still learning Android and I want to understand what's happening within its own classes.

Community
  • 1
  • 1
Antonio Ribeiro
  • 373
  • 4
  • 12
  • bond service, use bound local service pattern – pskink Sep 23 '16 at 05:17
  • @pskink thanks for the comment! Do you care to elaborate why this is better? – Antonio Ribeiro Sep 23 '16 at 05:29
  • 1
    see [this](https://developer.android.com/guide/components/bound-services.html#Binder) – pskink Sep 23 '16 at 05:37
  • @pskink I've seen this before, but now I convinced myself that doing this is better than sending a broadcast to no one every few seconds. Is there a problem Running a new thread inside the activity so I can continuously get the results from the service? – Antonio Ribeiro Sep 23 '16 at 21:13
  • why would you want to poll data? just send data from your service by calling activity method, the same way you are calling service methods from your activity – pskink Sep 23 '16 at 21:20
  • "Then in the onReceive method call runOnUiThread to update the respective View". onReceive its ALWAYS called on the main thread (even if you sendBroadcast() from a background thread), no need for runOnUiThread. – Goran Horia Mihail Feb 19 '18 at 19:42

0 Answers0