gpt4 book ai didi

java - BroadcastReceiver (TIME_TICK) 每晚都会死掉?

转载 作者:行者123 更新时间:2023-12-01 04:18:18 25 4
gpt4 key购买 nike

我想为体育网络服务编写某种后台实时报价应用程序......我希望我的应用程序能够始终调用 TIME_TICK。

顺便说一句:我也尝试过使用AlarmManager,但问题是一样的。

但现在我的问题...

我使用带有服务的接收器作为执行部分。注册后每分钟都会正确调用接收器。但每天晚上服务都会终止,并且永远不会再被调用。

在 Android 2.x 上一切正常,但 Android 4.x 每天都会停止接收器...是否有可能让应用程序在 Android 4.x 上保持 Activity 状态?

接收者已在我的主要 Activity 中注册:

registerReceiver(new MyReceiver(), new IntentFilter(Intent.ACTION_TIME_TICK));

list 条目:

<service android:name="de.pepdev.MyService" />
<receiver android:name="de.pepdev.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.TIME_TICK" />
</intent-filter>
</receiver>

接收器级别:

public class MyReceiver extends BroadcastReceiver
{
public static long nextExecTime = 0;
public static Calendar currentTime = Calendar.getInstance();

@Override
public void onReceive(Context context, Intent intent)
{
currentTime = Calendar.getInstance();

if(nextExecTime <= currentTime.getTimeInMillis())
{
Intent service = new Intent(context, MyService.class);
context.startService(service);
}
}
}

最佳答案

I also tried to use the AlarmManager, but the problem is the same

AlarmManager是一个比 ACTION_TIME_TICK 更好的答案,特别是如果您让用户配置轮询频率(包括“请不要轮询,因为我希望电池和带宽使用量保持在较低水平”的选项)。

请随时就您认为遇到的任何问题提出单独的 StackOverflow 问题。

But every night the service is terminated and will never be called again

Android 可以并且将会在任何时候终止您的进程,无论是应用户请求还是由于老化。

Manifest-entries:

<receiver>毫无意义,如 you cannot register for ACTION_TIME_TICK via the manifest .

关于java - BroadcastReceiver (TIME_TICK) 每晚都会死掉?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19224905/

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