简体   繁体   English

广播接收器(TIME_TICK)每天晚上死吗?

[英]BroadcastReceiver (TIME_TICK) dies every night?

I want to write some kind of background-live-ticker app for sports-web-services... I would like my app to be able to call the TIME_TICK all the time. 我想为体育网络服务编写某种后台实时运行的应用程序...我希望我的应用程序能够一直调用TIME_TICK。

Btw: I also tried to use the AlarmManager, but the problem is the same. 顺便说一句:我也尝试使用AlarmManager,但是问题是一样的。

But now my problem... 但是现在我的问题...

I use a Receiver with a Service for the execution part. 我将带有服务的接收器用于执行部分。 The Receiver is called every minute correctly after register. 注册后,接收器每分钟都会正确调用一次。 But every night the service is terminated and will never be called again. 但是每天晚上该服务都会终止,并且永远不会再被调用。

On Android 2.x everything works fine but Android 4.x will stop the Receiver every day... Is there any posibility to keep the app alive on Android 4.x? 在Android 2.x上,一切正常,但Android 4.x每天都会停止Receiver ...是否有可能使该应用在Android 4.x上保持活动状态?

The Reveiver is registered in my Main-Activity: 收款人已在我的主要活动中注册:

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

Manifest-entries: 清单条目:

<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>

Receiver-class: 接收器类:

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,但是问题是一样的

AlarmManager is a far better answer than ACTION_TIME_TICK , particularly if you let the user configure the polling frequency (including an option for "never poll, please, as I like my battery and bandwidth usage to stay low"). ACTION_TIME_TICK相比, AlarmManager是一个更好的答案,特别是如果您让用户配置轮询频率(包括“请不要轮询,因为我希望电池和带宽使用保持较低水平”的选项)时尤其如此。

Please feel free to ask a separate StackOverflow question regarding whatever problems you feel your are experiencing with it. 关于您遇到的任何问题,请随时询问一个单独的StackOverflow问题。

But every night the service is terminated and will never be called again 但是每天晚上服务都会终止,永远不会再被调用

Android can and will terminate your process at any point, either by user request or due to old age. Android可以并且将在任何时候根据用户请求或由于年龄原因终止您的流程。

Manifest-entries: 清单条目:

The <receiver> is pointless, as you cannot register for ACTION_TIME_TICK via the manifest . <receiver>是毫无意义的,因为您无法通过清单注册ACTION_TIME_TICK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM