【发布时间】:2016-10-14 05:29:25
【问题描述】:
当我们从最近的应用程序中清除应用程序时,服务是否会停止工作?因为当我在代码下面运行时。要在位置更改时更新 lat,lng。当我从最近的应用程序中清除应用程序时,位置更新将停止。当我看到我正在运行的应用程序查看服务处于运行状态时。请清除服务将如何工作的疑问。谢谢前进。
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所以即使它被杀死的系统也会重新启动 -
我返回 START_STICKY 检查上面的代码
标签: android service android-intentservice