If I understand correctly, then the restart of the service after the "kill" is answered by the flags returned in onStartCommand. For verification, I used the simplest service:

public class TestService extends Service { final String LOG_TAG = "myLogs"; public void onCreate() { super.onCreate(); Log.d(LOG_TAG, "TestService onCreate"); } public void onDestroy() { super.onDestroy(); Log.d(LOG_TAG, "TestService onDestroy"); } public int onStartCommand(Intent intent, int flags, int startId) { Log.d(LOG_TAG, "TestService onStartCommand") readFlags(flags); MyRun mr = new MyRun(startId); new Thread(mr).start(); return START_REDELIVER_INTENT; } public IBinder onBind(Intent arg0) { return null; } void readFlags(int flags) { switch (flags) { case START_FLAG_REDELIVERY: Log.d(LOG_TAG, "START_FLAG_REDELIVERY"); break; case START_FLAG_RETRY: Log.d(LOG_TAG, "START_FLAG_RETRY"); break; default: Log.d(LOG_TAG, "Flag: " + flags); } } class MyRun implements Runnable { int startId; public MyRun(int startId) { this.startId = startId; Log.d(LOG_TAG, "Run#" + startId + " create"); } public void run() { Log.d(LOG_TAG, "Run#" + startId + " start"); try { TimeUnit.SECONDS.sleep(15); } catch (InterruptedException e) { e.printStackTrace(); } stop(); } void stop() { Log.d(LOG_TAG, "Run#" + startId + " end, stopSelfResult(" + startId + ") = " + stopSelfResult(startId)); } } 

which ran on three devices (4.1.2, 4.4.2, 6.0.1). But after the "killing" of the process, the service was restarted on only one of the devices, producing a log

 D/myLogs: TestService onCreate D/myLogs: TestService onStartCommand D/myLogs: START_FLAG_REDELIVERY D/myLogs: MyRun#1 create D/myLogs: MyRun#1 start D/myLogs: MyService onDestroy D/myLogs: MyRun#1 end, stopSelfResult(1) = true 

On other devices, the service was dying to meet. The situation is similar with other sticky flags. I misunderstand the service restart mechanism or the problem in something else?

  • Try changing return START_REDELIVER_INTENT to return START_STICKY - shcherbuk

0