Reputation: 83
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.
Here's a snip from the log:
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`
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.
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());
}
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?
Upvotes: 4
Views: 7162
Reputation: 11834
I was totally in the same situation. Restart time was really huge around 3M-9M ms.
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.
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.
The onStartCommand still not being called but after I moved everything to onCreate (onStartCommand just returns with START_STICKY), now everything is working fine.
Upvotes: 3