java - 清除最近任务时防止杀死服务

标签 java android service

我的应用程序具有绑定(bind)到 Activity 的远程、前台辅助服务。当我清除最近的任务(通过滑动)服务销毁。我怎样才能防止服务被杀死。

  <service android:enabled="true"
                 android:exported="false"
                 android:stopWithTask="false"
                 android:process=":xplay"
                android:name="com.perm.x.Services.PlayingService"/>

这是我绑定(bind)的方式(在 onResume 中)

 Intent serviceIntent = new Intent(this,PlayingService.class);
        startService(serviceIntent);
        bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);

这是我如何解除绑定(bind)服务(在 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);
    }

虚拟 Activity :

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

就是这样。但是这个 hack 有一个副作用——在你滑动应用程序后,最近的面板将立即隐藏

关于java - 清除最近任务时防止杀死服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23716832/

相关文章:

java - HQL意外的AST节点:unexpected AST node:

android - 在 Activity 和 Service 之间交换对象

java - 此代码的下载速度非常慢,是否有助于改进它?

java - 是否可以使用 Web 服务器 api 中的 MpAndroid 显示图表?

java - Android按钮比较

android - 如何从您的 Android 应用程序代码启动 'Google settings'?

android - 为什么当可穿戴设备处于环境模式时不调用 onSensorChange

java - 堆积条形图 : Changing the order of the stacked columns for just one of the bars

java - Tomcat 服务 : quotes in wrapper. 配置文件

Java 8、类型注解和 JSR 308