How can I run a service with regular location updates in android - android

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.

Related

Service is not running in background in nougat

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

How to restart android service if killed

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;
}

Timer inside Android service

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.

Android service is restarting slowly

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;
}

Restarting Services in Android

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

Categories

Resources