Android: Planning for a restart of an emergency service with a long delay?

I have a service in which the OS kills - the problem is that when it kills it and plans to restart it, it is planned to restart it in an hour. This service supports two receivers for changing the Bluetooth connection, so I need to restart much faster, instead of sitting in the "Restart" state for more than an hour.

Here is a magazine clipping:

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, he simply kills him and transfers it again 2 hours later and so on. When this happens, it seems that onDestroy (), onCreate () and onStartCommand () are not being called. Starting the main action will successfully restart the service, and everything will be fine for several hours until it starts again.

It started when I made a change that requires me to go through additional functions when the service is running.

Here is my onStartCommand and onCreate if this 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 regarding what I am doing wrong will be greatly appreciated. I searched, but did not find anything like this long delay on restart. Should parent activity for START_REDELIVER_INTENT work? (Therefore, when my main action is cleared, I can no longer restart the service without reopening the main action?) Or is something else happening?

+4
source share
1 answer

I was completely in the same situation. The reboot time was really huge around 3M-9M ms.

I have researched this problem a lot, and many times I have found that startForeground can solve everything. But I was not satisfied with this decision.

In my situation, I used HandlerThread to handle background tasks, and the solution was to change this thread priority THREAD_PRIORITY_BACKGROUND to the default value. It was annoying that I found THREAD_PRIORITY_BACKGROUND in the official service example.

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

+3
source

Source: https://habr.com/ru/post/958182/


All Articles