This has been asked so many times, I have seen and applied many answers but nothing seems to be helping here.
This is how i am starting server on application launch
startService(new Intent(MavsMainActivity.this, LocationUpdateService.class));
Manifest
<service android:name=".myservices.services.LocationUpdateService"
android:process=":locationService"
/>
When i forcefully closed the application, It triggers the onCreate()
of service again. I have tried return START_STICKY
and return START_NOT_STICKY
, one re-starts the service on Application onDestroy
, and one restarts it when user relaunch the application after destroying it respectively.
This is my service class, Kindly guide me how can i keep service running on background even user destroys the application.
@Override
public void onCreate() {
if (isGooglePlayServicesAvailable()) {
/* min dist for location change, here it is 10 meter */
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
Toast.makeText(getApplicationContext(), "Service created", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand: ");
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(BACKGROUND_INTERVAL);
mLocationRequest.setFastestInterval(30000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setSmallestDisplacement(20);
sendOfflineData();
Toast.makeText(getApplicationContext(), "onStart Called", Toast.LENGTH_SHORT).show();
return START_STICKY ;
}