In android application if I save the intent of the service that is running. How can I restart the service?
I want it to run onStartCommand function again.
Thanks
Make onStartCommand return START_STICKY .
public int onStartCommand(Intent intent, int flags, int startId){
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
Documentation link: http://developer.android.com/reference/android/app/Service.html
Related
My app's location update service not displaying in list in nougat device. But it display in lower devices.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
it is due to background execution Limit in android 7.0,you have to use either job scheduler or Foreground service for fetching location in the background.refer Android 7.0 platform changes
I'm using LocationListener in my service to get regular location updates. When I close the process, the service does not give any updates to me. What can I do?
you service as a START_STICKY.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// initialize location
initLocation();
Log.e("", "onStartCommand");
return START_STICKY;
}
It will run in background after kill..and if there is any update of location you can get update.
In Activity I start a service
Intent serv=new Intent(this, MyService.class);
serv.putExtra("mac", "mac");
startService(serv);
In the service, I get the parameter
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
String mac=intent.getExtras().getString("mac");
return super.onStartCommand(intent, flags, startId);
}
Then I kill the app, the app will crash, and the service will also crash.
If I remove this line, then it will be no problem, the service also alive after I killed the app.
String mac=intent.getExtras().getString("mac");
Why the app crash?
super.onStartCommand(intent, flags, startId) returns the START_STICKY flag by default which, according to the docs, mean that:
"if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent."
Since the Intent isn't being redelivered you likely get NPE when calling intent.getExtras().
To redeliver the Intent return the START_REDELIVER_INTENT flag from onStartCommand():
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
...
return START_REDELIVER_INTENT;
}
You are putting extras to wrong intent bleService.putExtra("mac", "mac");
Instead you should write serv.putExtra("mac", "mac");
Also you should always check if there are extras
if(getIntent().hasExtra("mac"){
//do some stuff
}
I have an Android service that starts a timer that does stuff:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
timer.scheduleAtFixedRate(new VeryImportantTask(), 0, RATE);
return START_STICKY;
}
I know Services are singleton but, does onStartCommand method called each time that I call startService()? If so, I should control that my timer is just started the first time, shouldn't I? I'm thinking in a static boolean flag in the service. Is there a better way?
In your case i.e START_STICKY you can simply check the intent value nullity
like this
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(null==intent){
// service restarted do what you want
}
return START_STICKY;
}
because first time the intent will not be null and it will be null every time in case of a restart with START_STICKY.
According to this blog post and the documentation of onStartCommand() if you have a Service you should implement onStart() and onStartCommand() and in 2.0 and higher only onStartCommand() will be called. It seems that this is not the case and in my Service BOTH are being called. This was a problem as it was trying to do the work twice, so I had to add a check in onStart() to not do anything if the OS version was < 2.0. This seems like a hack and a bug. Anyone else experience this or do I maybe have something wrong? I cut and pasted the code right from the sample.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Util.log(mCtx, "AlerterService", "onStartCommand() called");
handleStart(intent);
return super.onStartCommand(intent, flags, startId);
}
public void onStart(Intent intent, int startId) {
Util.log(mCtx, "AlerterService", "onStart() called");
handleStart(intent);
super.onStart(intent, startId);
}
The source code of onStartCommand() is:
public int onStartCommand(Intent intent, int flags, int startId) {
onStart(intent, startId);
return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;
}
So it still calls onStart();
On that blog post, the base implementantions of onStart and onStartCommand are not called. Pressumably, one of them is calling the other.