【问题标题】:Prevent killing of service when clear recent tasks清除最近的任务时防止杀死服务
【发布时间】:2015-02-21 03:45:40
【问题描述】:

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

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

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

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

这是我解除服务绑定的方法(在 onPause 中)

unbindService(serviceConnection);

【问题讨论】:

  • 您的设备是什么版本的Android?服务最终会被系统重启吗?
  • 4.1 果冻豆。是的,服务在大约 4 秒后重新启动并发出通知
  • 我也测试其他应用程序。除了 Poweramp 播放器外,几乎所有人都有相同的行为。他只是无视杀戮
  • @Karakuri 有什么建议吗?
  • 如果几秒后重启,真的有问题吗?

标签: java android service


【解决方案1】:

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

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

虚拟活动:

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

就是这样。但是这个 hack 有一个副作用 - 在你刷应用程序后,带有最近记录的面板将立即隐藏

【讨论】:

  • 我正在从我的警报的 onRecieve 开始我的活动,现在我希望我的活动应该出现在最近的列表中,但是一旦 i ==2 就会出现 i ==2 的条件,那么这个活动应该完成并从最近列表中删除,如果我使用 xml 属性,那么它将完全从最近列表中隐藏
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
  • 2019-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-16
  • 1970-01-01
相关资源
最近更新 更多