在清除最近的任务时防止杀死服务



我的应用程序有远程,前台aidl服务绑定到活动。当我清除最近的任务(通过滑动)服务销毁。如何防止终止服务。

  <service android:enabled="true"
                 android:exported="false"
                 android:stopWithTask="false"
                 android:process=":xplay"
                android:name="com.perm.x.Services.PlayingService"/>
下面是我如何绑定(in onResume)
 Intent serviceIntent = new Intent(this,PlayingService.class);
        startService(serviceIntent);
        bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);

这是如何取消绑定服务(在onPause)

unbindService(serviceConnection);

为了防止服务杀死我发现了一个hack(实际上我确实在这里某处读到过)

    @Override
    public void onTaskRemoved(Intent rootIntent) {
            Intent intent = new Intent(this, DummyActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
    }

DummyActivity:

public class DummyActivity extends Activity {
    @Override
    public void onCreate(Bundle icicle){
        super.onCreate(icicle);
        finish();
    }
}

就是这样。但是这个hack有一个副作用-在你滑动

应用程序后,显示最近消息的面板会立即隐藏。

最新更新