gpt4 book ai didi

android - AlarmManager 和 BroadcastReceiver 而不是 Service - 这样不好吗? (暂停)

转载 作者:可可西里 更新时间:2023-11-01 19:03:46 24 4
gpt4 key购买 nike

背景信息:

我需要从网络上更新一些数据,大约每小时更新一次,即使我的应用已关闭。数据更新本身大约需要 40 秒到 1 分钟。然后将其作为可序列化对象保存到文件中。当我的应用程序启动时会读取此文件。

这是我目前采用的方法(不使用服务)

像这样使用 AlarmManager 和 BroadcastReceiver :

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 负责更新来自 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...
}
}

在我的 list 中:

<receiver android:name="com.cousinHub.myapp.RepeatingAlarmReceiver_REFRESH_DATA" android:process=":remote" />

问题:

警报按时触发并开始更新,但在大约 10 秒后停止(超时):

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: 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: INFO/Process(76): Sending signal. PID: 819 SIG: 9

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)上很好

问题:

  1. 为什么超时?有什么简单的方法可以避免这种情况吗?
  2. 我是否在 list 中正确设置了 BroadcastReceiver?我是否需要添加一些东西(以避免超时)?
  3. 我绝对应该为这种“从 Web 刷新”功能寻求服务吗? (考虑这篇文章:http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/)如果是(我应该切换到服务):任何好的代码 fragment/教程......

一如既往,感谢您的帮助。

H.

最佳答案

Why this timeout ?

您正在主应用程序线程上运行。您不能在主应用程序线程上运行超过几秒钟。此外,在执行此操作时,您会损害设备的性能(因为您是 running with foreground priority ),例如导致游戏或视频的帧速率丢失。

Any easy way to avoid this ?

不要在主应用程序线程上做大量工作(>100 毫秒)。让您的 BroadcastReceiver 委托(delegate)给 IntentService,可能是 WakefulIntentService .

Did I set up my BroadcastReceiver correctly in the manifest ?

拜托拜托拜托拜托拜托摆脱android:process=:remote。您不需要它,它对您没有帮助,而且会进一步降低设备的性能。

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项目。

关于android - AlarmManager 和 BroadcastReceiver 而不是 Service - 这样不好吗? (暂停),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3117350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com