is service will stop working when we clear the app from recent apps? because when iam running below code. To update lat,lng when location changed.when i clear the app from recent apps the location update is getting stopped.when i see my running apps view the service was in running state.please clear the doubt how service will work exactly.Thank in Advance.
public class GPSTracker extends Service implements
ConnectionCallbacks,
OnConnectionFailedListener,
LocationListener, GooglePlayServicesClient.ConnectionCallbacks {
public static final long UPDATE_INTERVAL = 5000;
public static final long FASTEST_INTERVAL = 1000;
private LocationClient mLocationClient;
private LocationRequest mLocationRequest;
private boolean mInProgress;
private boolean servicesAvailable = false;
DatabaseHandler db;
@Override
public void onCreate() {
super.onCreate();
mInProgress = false;
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setInterval(UPDATE_INTERVAL) // Set the update interval to 5 seconds
.setFastestInterval(FASTEST_INTERVAL); // Set the fastest update interval to 1 second
servicesAvailable = servicesConnected();
setUpLocationClientIfNeeded();
db = new DatabaseHandler(this);
}
private boolean servicesConnected() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
return ConnectionResult.SUCCESS == resultCode;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.d("TAG", "onLocationChanged onStart Command");
if (!servicesAvailable || mLocationClient.isConnected() || mInProgress)
return START_STICKY;
setUpLocationClientIfNeeded();
if (!mLocationClient.isConnected() || !mLocationClient.isConnecting() && !mInProgress) {
mInProgress = true;
mLocationClient.connect();
}
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
/* mInProgress = false;
if (servicesAvailable && mLocationClient != null) {
mLocationClient.removeLocationUpdates( this);
mLocationClient = null;
}
*/
super.onDestroy();
}
private void setUpLocationClientIfNeeded() {
if (mLocationClient == null)
mLocationClient = new LocationClient(this, this, this);
}
/*
* LocationListener Callbacks
*/
@Override
public void onLocationChanged(Location location) {
//Carnival.updateLocation(location);
Log.d("TAG", "onLocationChanged " + location.getLongitude());
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact("" + location.getTime(), " Latitude " + location.getLatitude() + " Longitude " + location.getLongitude()));
}
/*
* GooglePlayServicesClient Callbacks
*/
@Override
public void onConnected(Bundle bundle) {
if (mLocationClient != null) {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}
}
@Override
public void onDisconnected() {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
mInProgress = false;
/*
* Google Play services can resolve some errors it detects.
* If the error has a resolution, try sending an Intent to
* start a Google Play services activity that can resolve
* error.
*/
if (!connectionResult.hasResolution()) {
// If no resolution is available, display an error dialog
}
}
}