[英]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.