stopSelf(int startId) does not stop the started service if the service is restarted after crash

I’m handling one song download at a time in a started service. I’ve implemented a handler and background thread and looper on that thread that handles one message(song download) at a time.

I’m handling downloads one by one in a started service and calling stopSelf(int startId) after each download is finished in order. If the service is started and does not crashes, then, after all the downloads are completed, the onDestroy of my Download service is called Automatically. But suppose if one song is downloaded and then service is crashed and is restarted by the system after some time, then, the stopSelf(int startId) is called after each remaining download but when the last song is downloaded and stopSelf(int startId) is called for that last song, the onDestroy of my Download service is not called automatically. I have also tried the newer version of stopSelf(int startId) that is stop stopSelfResult(int startId), this returns true if the service will be stopped. This method returns true for last song startId if the service is not crashed. But if the service is crashed and restarted then this method is called but returns false for the last song’startId, even though the startId for the last song is onStartComamnd method, after restarting service, is same passed to this method. This is the MyDownloadServiceClass

public class MyDownloadService extends Service { private static final String TAG = "MyTag"; private DownloadThread mDownlaodThread;  //this is started service  public MyDownloadService() { }   @Override public void onCreate() {     super.onCreate();     Log.d(TAG, "onCreate: called");      mDownlaodThread = new DownloadThread();     mDownlaodThread.start();      while (mDownlaodThread.mHandler == null) {      }     mDownlaodThread.mHandler.setService(this); }  @Override public int onStartCommand(Intent intent, int flags, int startId) {      Log.d(TAG, "onStartCommand: called with Song Name: " +             intent.getStringExtra(MainActivity.MESSAGE_KEY) + " Intent     Id: " + startId);     final String songName =     intent.getStringExtra(MainActivity.MESSAGE_KEY);      Message message = Message.obtain();     message.obj = songName;     message.arg1 = startId;      mDownlaodThread.mHandler.sendMessage(message);      return Service.START_REDELIVER_INTENT; }  @Override public IBinder onBind(Intent intent) {     Log.d(TAG, "onBind: called");     return null; }  private void downloadSong(final String songName) {     Log.d(TAG, "run: staring download");     try {         Thread.sleep(4000);     } catch (InterruptedException e) {         e.printStackTrace();     }     Log.d(TAG, "downloadSong: " + songName + " Downloaded..."); }  @Override public void onDestroy() {     super.onDestroy();     Log.d(TAG, "onDestroy: called"); } } 

And this is the download handler.

private static final String TAG = "MyTag"; private MyDownloadService mService;  public DownloadHandler() { }  @Override public void handleMessage(Message msg) {      downloadSong(msg.obj.toString());     mService.stopSelf(msg.arg1);     Log.d(TAG, "handleMessage: Song Downloaded: "+msg.obj.toString() + " Intent Id: "+msg.arg1); }  private void downloadSong(final String songName){     Log.d(TAG, "run: staring download");     try {         Thread.sleep(4000);     } catch (InterruptedException e) {         e.printStackTrace();     }      Log.d(TAG, "downloadSong: "+songName+" Downloaded..."); }  public void setService(MyDownloadService downloadService) {     this.mService=downloadService; } 

What is the problem and whats the solution? Thanks.