Android:计划以巨大的延迟重新启动崩溃的服务?
问题说明
我有一个正在运行的服务,操作系统正在杀死该服务 - 问题是当它杀死它并安排重新启动时,它被安排在一个多小时后重新启动.此服务为蓝牙连接更改维护两个接收器,这就是为什么我需要更快地重新启动,而不是在重新启动"状态中停留一个多小时.
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`
在这 3600 秒过去后,它会简单地杀死它并在 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());
}
任何关于我做错了什么的建议将不胜感激.我已经搜索过,但没有找到任何似乎与重新启动的长时间延迟有关的任何内容.START_REDELIVER_INTENT
是否必须激活父 Activity 才能工作?(因此当我的主要活动被清理时,我无法在不重新打开主要活动的情况下重新启动服务?),还是发生了其他事情?
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 ms 的重启时间真的很长.
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.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfijga
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20