0

I have a service which reads data from a Socket connection and during peak hours. The frequency of the changes is every five seconds.

I have two options in mind.

  1. Update the List, using a BroadCastReceiver.

  2. Create a new Thread in the Activity where I created the List, read a static variable every five seconds, and when the change occurs, change the static variable from the Service.

Which option is more efficient and what are the pros and cons of both? If you think there is a better option, please let me know.

Thank you.

Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55

3 Answers3

1

I would use the BroadcastReceiver (with the LocalBroadcastManager) option. I would recommend you to make your model class implement Parcelable. I would definitely avoid the solution 2

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Thankyou. So if we sendbroadcast for lets say 10 times a minute, for 2 hours, i.e 1200 times it will not have any performance impact? – Abhinav Pathak Jul 17 '15 at 08:33
  • 1
    I can't tell you exactly what kind of performance impact you will be experiencing. It depends on multiple factors. One is the type and the amount of data you want to transfer. I ve something similar. A service is controlling a music player, and I broadcast the current seek position to update the ui, I broadcast twice per second and it works flawlessly on my nexus5.. But I brodcast just some integer/strings – Blackbelt Jul 17 '15 at 08:40
0

BroadCastReceiver will be efficient for your case.

It will help you to update your ListView when it receives new data. So you don't need to check for update in every 5 seconds. It will increase the performance of your App. On the other hand in case of very frequent updates it will not wait for the given interval.

So go with broadcastreceicer.

Here is the pros that you will get from BroadcastReceiver :

A Broadcast receiver wakes your application up, the inline code works only when your application is running.

For example if you want your application to be notified of an incoming call, even if your app is not running, you use a broadcast receiver.

If your application is playing audio, and you want to stop the music on an incoming call, you use the inline code.

Mohammad Arman
  • 7,020
  • 2
  • 36
  • 50
  • Thankyou. So if we sendbroadcast for lets say 10 times a minute, for 2 hours, i.e 1200 times it will not have any performance impact? – Abhinav Pathak Jul 17 '15 at 08:39
  • Nope, It will not create any issue. In order to maintain proper sequence of received messages you can use **Ordered broadcast**. For detail see the official documentation here: http://developer.android.com/intl/zh-TW/reference/android/content/BroadcastReceiver.html – Mohammad Arman Jul 17 '15 at 08:50
0

With broadcast receiver you can't uptade the UI, you can see WHY here, if you talk about other thing, explain us please... In that post you can see a other method to update UI from intent service, and I think is the best way to update UI from intent service because you only update UI when you need it and you don't overcharge the app process .

Finally if you want to update your UI every 5 seconds do it but with a little difference... I would do update every 5 seconds my UI if my app is in first plane if isn't it then update my app when app comes to first plane in OnResume() method because you don't use extra memory from device when my app is in background...

Tell me if I helped you and good programming!

Community
  • 1
  • 1