I create a process that should start again after he was killed:

package com.vitaliy.backgroundservice; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; import java.util.concurrent.TimeUnit; public class BackgroundService extends Service{ private final String LOG = "MY_BACKGROUND_SERVICE"; @Override public void onCreate() { super.onCreate(); Log.d(LOG, "onCreate"); } @Override public void onDestroy() { super.onDestroy(); Log.d(LOG, "onDestroy"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(LOG, "onStartComand " + "name: " + intent.getStringExtra("name")); readFlag(flags); MyRun myRun = new MyRun(startId); new Thread(myRun).start(); return START_STICKY; } @Override public IBinder onBind(Intent intent) { Log.d(LOG, "onBind"); return null; } private void readFlag(int flag){ switch (flag){ case START_FLAG_REDELIVERY: { Log.d(LOG, "START_FLAG_REDELIVERY"); break; } case START_FLAG_RETRY: { Log.d(LOG, "START_FLAG_RETRY"); break; } } } class MyRun implements Runnable{ private int startId; public MyRun(int startId){ this.startId = startId; Log.d(LOG, "MyRun# " + startId + " create"); } @Override public void run() { Log.d(LOG, "MyRun# " + startId); try{ TimeUnit.SECONDS.sleep(10); }catch (InterruptedException ie){ ie.getStackTrace(); } stop(); } private void stop(){ Log.d(LOG, "MyRun# " + startId + " end, stoped = " + stopSelfResult(startId) ); } } } 

It works and even after stopping it starts working again, but only comes to the onCreate method and crashes: enter image description here

What could be the error?

  • How do you run? Error logs? What is MyRun? - VAndrJ
  • @VAndrJ, I launch from another activit, through an implicit intent. MyRun is just a separate thread in which the execution delay is performed, the logs are printed and that's it ... - Maybe_V
  • @VAndrJ, but when you return START_REDELIVER_INTENT, no errors occur and everything works fine ... - Maybe_V

0