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
Related
I have created a service which runs in background and send GPSlocation server after every 30 seconds
but after few hours the service stops working
how to prevent that or restart the service if OS killed the service
Change onStartCommand() method as follows.
public int onStartCommand(Intent intent, int flags, int startId) {
//Your code
return Service.START_STICKY;
}
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 created a service and returned "START_REDELIVERY_INTENT" in "onStartCommand()" method, as i want to run the service even after destroying the App's MainActivity.
App should restart just after destroying the App with previous non-null intent. But App is taking some time to restart. We can see the status of restarting App in device setting (Below is snapshot). This snapshot is after destroying App..... What i do? Any Help please?
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
super.onStartCommand(intent, flags, startId);
Log.d(TAG, "intent: "+intent);
return START_REDELIVER_INTENT;
}
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