安卓:巨大延迟坠毁服务安排重新启动? [英] Android: Scheduling restart of crashed service with HUGE delay?

查看:294
本文介绍了安卓:巨大延迟坠毁服务安排重新启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务运行的操作系统是杀害 - 问题是,当它杀死它和时间表重新启动,它正在计划稍后重新启动了一个多小时。该服务维护两个接收器的蓝牙连接的变化,这就是为什么我需要重新启动坐在重新启动的状态了一个多小时的更迅速代替。

I have a service running which the OS is killing - the problem is that when it kills it and schedules the restart, it's being scheduled to restart over an hour later. This service maintains two receivers for bluetooth connection changes, which is why i need to to restart much more quickly instead of sitting in the "Restarting" state for over an hour.

下面是从日志剪断:

I/ActivityManager( 1064): No longer want com.deadbeat.bta (pid 25455): hidden #17
W/ActivityManager( 1064): Scheduling restart of crashed service com.deadbeat.bta/com.deadbeat.btalib.BTService in 3600210ms`

在此三千六百秒后,它将简单地杀死它,并再次重新安排为2个小时后,等。当发生这种情况,看来的onDestroy(),的onCreate(),和onStartCommand()不被调用。开始的主要活动将成功地重新启动该服务,所有将被罚款了几个小时,直到这将启动再次发生。

After this 3600 seconds elapses, it will simply kill it and reschedule again for 2 hours later, and so on. When this happens, it appears that onDestroy(), onCreate(), and onStartCommand() are not being called. Starting the main activity will successfully restart the service and all will be fine for a couple of hours until this starts happening again.

这开始发生时,我提出要求我通过在附加功能时启动服务的变化。

This started happening when i made a change that requires i pass in extras when the service is started.

下面是我的onStartCommand和的onCreate是否有帮助?

Here is my onStartCommand and onCreate if that helps...

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("BTA", ">>> onStartCommand()");
    setIntent(intent);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        setGlobals((Globals) extras.getSerializable("Globals"));
    }
    if (getGlobals() == null) {
        Log.e("BTA", "!!! Call an ambulance!!");
    }
    Log.i(getGlobals().getLogPrefix(), ">>> Service starting up");

    setWorker(new BTAWorker(this, getGlobals()));
    getWorker().doLog("Service Worker Set and Active");

    return START_REDELIVER_INTENT;
}

@Override
public void onCreate() {
    // Create
    super.onCreate();
    Log.d("BTA", ">>> onCreate()");
    // Register BroadcastReceiver with connect and disconnect actions
    IntentFilter intentToReceiveFilter = new IntentFilter();
    intentToReceiveFilter.addAction("android.bluetooth.device.action.ACL_CONNECTED");
    intentToReceiveFilter.addAction("android.bluetooth.device.action.ACL_DISCONNECTED");
    registerReceiver(this.mIntentReceiver, intentToReceiveFilter, null, this.mHandler);
    Log.d("BTA", ">>> Bluetooth State Receiver registered");
    Log.d("BTA", ">>> Intent = " + getIntent());
}

任何意见,至于是什么,我做错了会大大AP preciated。我已经搜查,但没有发现任何东西,这似乎与此有关迟迟没有重新启动。难道父活动必须激活 START_REDELIVER_INTENT 工作? (因此,当我的主要活动是清理,我不能再重新启动该服务,而无需重新打开主活动?),或者是别的什么事?

Any advice as to what i'm doing wrong would be greatly appreciated. I've searched, but didn't find anything that seems to be related to this long delay in restarting. Does the parent activity have to be active for START_REDELIVER_INTENT to work? (Therefore when my main activity is cleaned up, i can no longer restart the service without re-opening the main activity?), or is something else going on?

推荐答案

我是完全在同样的情况。重新启动时间约为3M-9M毫秒真的很大。

I was totally in the same situation. Restart time was really huge around 3M-9M ms.

我做了很多关于这个问题的研究很多次我发现,startForeground可以解决一切。但我不满意这个解决方案。

I did a lot of research about this issue and many times I found that startForeground can solve everything. But I wasn’t satisfied with this solution.

在我的情况我以前HandlerThread处理后台任务的解决办法是改变这种线程的优先级THREAD_PRIORITY_BACKGROUND为默认值。恼人的事情是,我发现THREAD_PRIORITY_BACKGROUND在官方服务的例子。

In my situation I used HandlerThread to process background tasks and the solution was to change this thread priority THREAD_PRIORITY_BACKGROUND to the default. The annoying thing was that I found THREAD_PRIORITY_BACKGROUND in an official service example.

在onStartCommand仍然没有被调用,但之后我感动一切的onCreate(onStartCommand刚刚返回与START_STICKY),现在一切工作正常。

The onStartCommand still not being called but after I moved everything to onCreate (onStartCommand just returns with START_STICKY), now everything is working fine.

这篇关于安卓:巨大延迟坠毁服务安排重新启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆