Skip to content Skip to sidebar Skip to footer

Alarmmanager And Broadcastreceiver Instead Of Service - Is That Bad ? (timeout)

BACKGROUND INFO: I need to update some data from the web, about every hour or so, even when my app is closed. The update of the data itself takes about 40 seconds to 1 minute. It i

Solution 1:

Why this timeout ?

You are running on the main application thread. You cannot run on the main application thread for more than a few seconds. Also, while doing this, you are harming the performance of the device (because you are running with foreground priority), such as causing frame-rate loss in games or videos.

Any easy way to avoid this ?

Don't do significant work (>100ms) on the main application thread. Have your BroadcastReceiver delegate to an IntentService, perhaps a WakefulIntentService.

Did I set up my BroadcastReceiver correctly in the manifest ?

Please please please please please get rid of the android:process=:remote. You do not need it, it is not helping you, and it is degrading performance of the device even further.

Should I absolutely go for a Service for this kind of "Refresh from Web" functionality ? (considering this article : http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/) If YES (I should switch to a service): Any good snippets of code/tutorial for this ...

IMHO, yes. Then again, I wrote that blog post. For an example, see the WakefulIntentService project.

Solution 2:

For information, I've tried with a new thread and it works when on Wifi (takes about 1'30" to update the data when phone is asleep, it doesn't get 'killed' !

//let's try with a new separate thread ?
        new Thread(new Runnable() {
            public void run() {
                Refresh_HIST_DATA();
            }
          }).start();

but NOT when on Mobile (GPRS), as it gets killed after about 10 secs!

It's half-a-solution for the moment and I will try CommonsWare's solution for a cleaner/more sustainable approach...

Let's see if the new thread solution works allways fine or was just luck (I've tested only during a couple hours) ...

If anyone else has another suggestion, please do post it.

Solution 3:

Instead of thread. You can start a AsyncTask from your broadcast receiver onRecive() method. This will not block the UI thread. I myself have done same in my projects which is of same nature i.e. It has to post data every 1 hour.

publicvoidonReceive(Context context, Intent intent) {
        // start your Asynctask from here. which will post data in doInBackground() method
}

Post a Comment for "Alarmmanager And Broadcastreceiver Instead Of Service - Is That Bad ? (timeout)"