Android 在安卓4.4中,将应用程序从最近的任务中移出会永久性地终止应用程序及其服务。知道为什么吗?
与以前的版本不同,在4.4中,将应用程序从最近的任务中移出会永久性地杀死应用程序及其服务(如强制停止),即使它正在运行后台服务。它显示0处理1个服务,但服务也不工作。理想情况下,它不应该杀死后台服务,在4.3之前的版本中也不应该。你知道为什么会发生在4.4中吗?明白了。这是4.4中的一个bug。我试过这个,效果非常好(不过这是一个肮脏的锻炼) 只需重写此方法-:Android 在安卓4.4中,将应用程序从最近的任务中移出会永久性地终止应用程序及其服务。知道为什么吗?,android,service,android-4.4-kitkat,Android,Service,Android 4.4 Kitkat,与以前的版本不同,在4.4中,将应用程序从最近的任务中移出会永久性地杀死应用程序及其服务(如强制停止),即使它正在运行后台服务。它显示0处理1个服务,但服务也不工作。理想情况下,它不应该杀死后台服务,在4.3之前的版本中也不应该。你知道为什么会发生在4.4中吗?明白了。这是4.4中的一个bug。我试过这个,效果非常好(不过这是一个肮脏的锻炼) 只需重写此方法-: public void onTaskRemoved(Intent rootIntent) { Log.e("FLAGX : "
public void onTaskRemoved(Intent rootIntent) {
Log.e("FLAGX : ", ServiceInfo.FLAG_STOP_WITH_TASK + "");
Intent restartServiceIntent = new Intent(getApplicationContext(),
this.getClass());
restartServiceIntent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(
getApplicationContext(), 1, restartServiceIntent,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext()
.getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartServicePendingIntent);
super.onTaskRemoved(rootIntent);
}
从这个问题
这是解决办法
在前台服务中:
@Override
public void onTaskRemoved( Intent rootIntent ) {
Intent intent = new Intent( this, DummyActivity.class );
intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity( intent );
}
在舱单中:
<activity
android:name=".DummyActivity"
android:theme="@android:style/Theme.NoDisplay"
android:enabled="true"
android:allowTaskReparenting="true"
android:noHistory="true"
android:excludeFromRecents="true"
android:alwaysRetainTaskState="false"
android:stateNotNeeded="true"
android:clearTaskOnLaunch="true"
android:finishOnTaskLaunch="true"
/>
你为什么在这里使用AlarmManager?我也需要在常规服务上实现这一点吗?我使用它是因为当你刷的时候,android会杀死一切,包括你的服务,然后就没有办法重新启动你的服务。通过设置警报,我确保重新启动服务。我升级了。。。但是我怎么能接受这个答案呢?这是你的问题:伙计,这是你的问题。只有你有能力接受:p这很有帮助。谢谢。这是android 4.4的解决方案,但不是5.1。我的一款安卓6.0设备似乎没有这个bug。
public class DummyActivity extends Activity {
@Override
public void onCreate( Bundle icicle ) {
super.onCreate( icicle );
finish();
}
}