AlarmManager and BroadcastReceiver instead of Service - is that bad ? (Timeout)

笑着哭i 提交于 2019-11-27 19:51:27

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.

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.

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.

public void onReceive(Context context, Intent intent) {
        // start your Asynctask from here. which will post data in doInBackground() method
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!