0

I have following piece of code in onCreate() of a AIDL service

 @Override
 public void onCreate() {
 log("onCreate()");
  if (SystemProperties.getInt(DONT_START,0)==1) {
     // Do not start the service if some system property is set
     log("not starting service");
     this.stopSelf();
     }
 }

This service is "binded to" or started from multiple places. I have logs in all the lifecycle methods. Sometimes I see that when service is started after this.stopSelf() - I see onStartCommand() getting called - but no onCreate() - which indicates that this onStartCommand() has been called on the already stopped service

The logs during the issue - 1% times when it fails looks like :

onCreate()

not starting service"

onBind() return null since service not started

onStartCommand()

The logs in normal flow in same scenario when issue is not seen - 99% times looks like :

onCreate()

not starting service"

onBind() return null since service not started

onCreate()

onStartCommand()

Other similar issues I looked at :

I see in similar thread StopSelf does not stop my service that it was recommended to use :

android.os.Process.killProcess(android.os.Process.myPid());

Should I be using this too instead of

   this.stopSelf() 
Community
  • 1
  • 1
RocketRandom
  • 1,102
  • 7
  • 20
  • may be your service restart due to your code as you said it start from multiple places. – Sagar Maiyad Dec 17 '14 at 11:03
  • if the service is restarted due to my code - (which is expected in this case) - this should result in the onCreate() getting called before the onStartCommand(). I have updated original post to include logs from case where it works - as i said it almost always works - issue is seen only rarely. – RocketRandom Dec 17 '14 at 11:09
  • Rather than trying to stop the service in `onCreate()`, don't start the service in the first place. – CommonsWare Dec 17 '14 at 12:11
  • I agree that is the better approach.But I want to try and avoid making changes to multiple applications which start this service. I do not want to make them "aware" of the system property. I need a way to fail the start - when application requests to start. – RocketRandom Dec 17 '14 at 13:13

0 Answers0