AlarmManager 和 BroadcastReceiver 而不是 Service - 那不好吗?(暂停) [英] AlarmManager and BroadcastReceiver instead of Service - is that bad ? (Timeout)
问题描述
背景信息:
我需要大约每小时更新一次网络上的数据,即使我的应用程序关闭也是如此.数据本身的更新大约需要 40 秒到 1 分钟.然后将其保存为可序列化的文件.当我的应用程序启动时读取此文件.
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 is then saved as a Serializable to a file. This file is read when my app starts.
这是我目前采用的方法(不使用服务)
像这样使用 AlarmManager 和 BroadcastReceiver :
use the AlarmManager and BroadcastReceiver like this :
private void set_REFRESH_DATA_Alarm(){
mContext = Main.this;
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
broadcast_intent = new Intent(mContext,
RepeatingAlarmReceiver_REFRESH_DATA.class);
pendingIntent = PendingIntent.getBroadcast(mContext, 0, broadcast_intent, 0);
// do a REFRESH every hour, starting for the first time in 30 minutes from now ...
Calendar now = Calendar.getInstance();
long triggerAtTime = now.getTimeInMillis()+ (1 * 30 * 60 * 1000); // starts in 30 minutes
long repeat_alarm_every = (1 * 60 * 60 * 1000); // repeat every 60 minutes
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime,
repeat_alarm_every, pendingIntent);
}
我的RepeatingAlarmReceiver_REFRESH_DATA.class负责从网络更新数据:
My RepeatingAlarmReceiver_REFRESH_DATA.class takes care of updating the Data from the Web:
public class RepeatingAlarmReceiver_REFRESH_DATA extends BroadcastReceiver {
public static Context mContext;
ConnectivityManager mConnectivity;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
// if Network connection is OK (Wifi or Mobile) then Load data ...
mConnectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
Log.i("Hub",
"mConnectivity.getNetworkInfo(0)="
+ mConnectivity.getNetworkInfo(0));
Log.i("Hub",
"mConnectivity.getNetworkInfo(1)="
+ mConnectivity.getNetworkInfo(1));
if ((mConnectivity.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)
|| (mConnectivity.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED)) {
Log.i("Hub", "Connectivity OK ...");
Refresh_HIST_DATA();
} else {
// else Show Dialog "No network connection" ...
Log.i("Hub",
"No network connection for the moment... will try again later!");
}
}
// =========================================================================
private void Refresh_HIST_DATA() {
Log.i("Hub", "Refresh_HIST_DATA()... Starting ...");
// etc...
}
}
在清单中我有:
<receiver android:name="com.cousinHub.myapp.RepeatingAlarmReceiver_REFRESH_DATA" android:process=":remote" />
问题:
警报按时触发并开始更新,但在大约 10 秒后停止(超时):
The alarm gets fired on time and the update starts but then after about 10 seconds it stops (Timeout):
06-25 11:55:05.278:警告/ActivityManager(76):超时广播广播记录{44bb4348空值} -接收者=android.os.BinderProxy@44bcc670
06-25 11:55:05.278: WARN/ActivityManager(76): Timeout of broadcast BroadcastRecord{44bb4348 null} - receiver=android.os.BinderProxy@44bcc670
06-25 11:55:05.278:警告/活动管理器(76):接收器超时期间:ResolveInfo{44bb42c0com.cousinHub.myapp.RepeatingAlarmReceiver_REFRESH_DATAp=0 o=0 m=0x0}
06-25 11:55:05.278: WARN/ActivityManager(76): Receiver during timeout: ResolveInfo{44bb42c0 com.cousinHub.myapp.RepeatingAlarmReceiver_REFRESH_DATA p=0 o=0 m=0x0}
06-25 11:55:05.278:信息/过程(76):发送信号.PID:819 SIG:9
06-25 11:55:05.278: INFO/Process(76): Sending signal. PID: 819 SIG: 9
06-25 11:55:05.298:信息/活动管理器(76):进程com.cousinHub.myapp:remote (pid 819)已经死了.
06-25 11:55:05.298: INFO/ActivityManager(76): Process com.cousinHub.myapp:remote (pid 819) has died.
ps:奇怪的是,在我的 HTC Hero(仍在 Android 1.5 - API 级别 4)上大约 10 秒后不会发生这种超时",但在我的 Nexus One(2.1-update1)上很好
问题:
- 为什么会超时?有什么简单的方法可以避免这种情况吗?
- 我是否在清单中正确设置了 BroadcastReceiver?我是否需要添加一些内容(以避免此超时)?
- 我绝对应该为这种从网络刷新"功能寻求服务吗?(考虑这篇文章:http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/)如果是(我应该切换到服务):任何关于此的好的代码片段/教程......
- Why this timeout ? Any easy way to avoid this ?
- Did I set up my BroadcastReceiver correctly in the manifest ? Do I need to add something (to avoid this timeout) ?
- 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 ...
一如既往,感谢您的帮助.
As allways, thanks for your help.
H.
推荐答案
为什么会超时?
您正在主应用程序线程上运行.您不能在主应用程序线程上运行超过几秒钟.此外,这样做会损害设备的性能(因为您是 以前台优先级运行),例如导致游戏或视频中的帧率丢失.
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 ?
不要在主应用程序线程上做大量工作(>100 毫秒).让您的 BroadcastReceiver
委托给 IntentService
,也许是 <代码>WakefulIntentService.
Don't do significant work (>100ms) on the main application thread. Have your BroadcastReceiver
delegate to an IntentService
, perhaps a WakefulIntentService
.
我是否设置了 BroadcastReceiver在清单中是否正确?
Did I set up my BroadcastReceiver correctly in the manifest ?
拜托拜托拜托请摆脱android:process=:remote
.您不需要它,它对您没有帮助,而且会进一步降低设备的性能.
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.
我应该绝对去寻求服务吗对于这种从网络刷新"功能?(考虑到这文章 :http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/)如果是(我应该切换到服务):任何好的代码片段/教程这...
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 ...
恕我直言,是的.然后,我又写了那篇博文.有关示例,请参阅 WakefulIntentService
项目.
IMHO, yes. Then again, I wrote that blog post. For an example, see the WakefulIntentService
project.
这篇关于AlarmManager 和 BroadcastReceiver 而不是 Service - 那不好吗?(暂停)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!