gpt4 book ai didi

android - 如何防止Service被破坏

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:48 26 4
gpt4 key购买 nike

我有一个跟踪用户位置的服务,我通过GoogleApiClient获取用户的位置。

有时 Service 会停止,这取决于互联网或手机型号 Service 停止向网络服务发送位置信息。好像被毁了。

我怎样才能避免这种情况?

public class LocationService extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {

private static final String TAG = "LocationService";
public long UPDATE_MILLISECONDS_DEFAULT = 180000;

private boolean currentlyProcessingLocation = false;
private LocationRequest locationRequest;
private GoogleApiClient googleApiClient;

@Override
public void onCreate() {
Log.d(TAG,"Location service create");
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// if we are currently trying to get a location and the alarm manager has called this again,
// no need to start processing a new location.
if (!currentlyProcessingLocation) {
currentlyProcessingLocation = true;
startTracking();
}

return START_NOT_STICKY;
}

private void startTracking() {
Log.d(TAG, "startTracking");

if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) {
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();

if (!googleApiClient.isConnected() || !googleApiClient.isConnecting()) {
googleApiClient.connect();
}
} else {
Log.e(TAG, "unable to connect to google play services.");
}
}

protected void sendLocationToServer(Location location) {
// here I call my webservice and send location
Log.d(TAG, "Update to Server location");
}

@Override
public void onDestroy() {
Log.d(TAG,"Destroy service");
stopLocationUpdates();
super.onDestroy();

}


@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onLocationChanged(Location location) {
sendLocationToServer(location);
}

public void stopLocationUpdates() {
if (googleApiClient != null && googleApiClient.isConnected()) {
googleApiClient.disconnect();
}
}

/**
* Called by Location Services when the request to connect the
* client finishes successfully. At this point, you can
* request the current location or start periodic updates
*/
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "onConnected");

locationRequest = LocationRequest.create();
locationRequest.setInterval(UPDATE_MILLISECONDS_DEFAULT); // milliseconds for default
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);


//locationRequest.setFastestInterval(1000); // the fastest rate in milliseconds at which your app can handle location updates

LocationServices.FusedLocationApi.requestLocationUpdates(
googleApiClient, locationRequest, this);
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, "onConnectionFailed");

stopLocationUpdates();
stopSelf();
}

@Override
public void onConnectionSuspended(int i) {
Log.e(TAG, "GoogleApiClient connection has been suspend");
}


}

最佳答案

您正在从 onStartCommand() 返回 START_NOT_STICKY

正因为如此,无论何时操作系统终止您的服务(例如回收内存),它都不会被重新创建。

更改以下行:

return START_NOT_STICKY;

对此:

return START_STICKY;

来自documentation START_STICKY:

Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this.

注意 START_STICKY 不会阻止您的Service 被终止。它只是告诉操作系统尽快重新启动它(取决于可用资源)。为了使您的 Service 不太可能被杀死,您可以通过调用 startForeground() 使其在前台运行.

关于android - 如何防止Service被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39050271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com