简体   繁体   中英

Send Broadcast from service to update the list fragment for every 1 minute

I have a service which runs on 24 hours daily.This service is started by an Activity and it runs indefinably. Inside the service, a separate thread runs and fetch data from server for every 1 minute. So whenever updated I used sendBroadcast to update the List Fragment and receive message from service and restart the Loader to update the List Fragment.

  1. Is this method is correct or not?
  2. If the listfragement(has broadcast receiver) destroyed the sendBroadcast produce any problem?

Scrap the whole thing and start from scratch :) Running '24 hours daily' is mostly impossible on Android, because it will go to sleep once the screen turns off. If you force it to stay on you will eat the battery in no time at all. So scrap that part. Updating data from server every minute is a bad idea too: too many network calls will the battery, and may take a long time on a 3G network. Additionally, the user probably doesn't care about those updates when they are in another app, so you will be wasting resources fetching things no one will ever see.

Use AralamManager to schedule periodic updates for your service if you have to. Re-consider the 'each minute' part. Have the activity register itself for updates onStart() and unregister onStop() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM