сервис прекратит работать, когда мы очистим приложение из последних приложений? потому что когда iam работает под кодом. Чтобы обновить lat, lng, когда изменилось местоположение. Когда я очищаю приложение от последних приложений, обновление местоположения становится остановленным. Когда я вижу, что мои запущенные приложения видят, что служба находилась в состоянии running.please, очистите сомнение в том, как служба будет работать точно. 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
}
}
}
у вас есть 'return START_STICKY', так что даже если его убитая система перезапустит то же самое – Raghunandan
i, вернусь START_STICKY, проверьте код выше –