[英]OnDestroy not being called for background Service in Android
我面临着类似的问题
不知道是否从BroadcastReceiver调用了服务onDestroy
由于尚未回答,因此我提出了一个新问题。 我的服务代码如下所示(未粘贴完整代码):
public class GPSLogger extends Service implements LocationListener,
com.google.android.gms.location.LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(GPSLogger.TAG, "StartAtBootService -- onStartCommand()");
// lot of init code
if (isGooglePlayServicesAvailable()) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(freq);
mLocationRequest.setFastestInterval(freq / 4);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
return START_STICKY;
}
}
@Override
public void onDestroy() {
Util.sendNotification("Service", "OnDestroy", this, 100);
stopLocationUpdates();
DBHelper.closeDb();
Log.i(GPSLogger.TAG, "StartAtBootService Destroyed");
Intent broadcastIntent = new Intent("cc.siara.gpslogger.RestartService");
sendBroadcast(broadcastIntent);
super.onDestroy();
}
}
从ADB日志中可以看到,操作系统多次启动了该服务,并调用了onStartCommand。 但是我什至找不到“ StartAtBootService被销毁”。
我正在测试的设备是Samsung SM-J120G Android 5.1.1 API 22。
我知道Android会偶尔停止服务以释放内存。 但是,无论如何,Android可以运行清理代码吗?
在以下情况下,将调用onDestroy:
当您停止使用服务时。
当操作系统内存不足并决定停止您的应用程序时。
当您通过活动或广播接收者的意图呼叫停止服务时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.