Android服务自行停止?

时间:2014-10-30 06:52:11

标签: android service timer countdowntimer

我正在尝试使用android CountdownTimer 创建自己的时钟或计时器。所以,我想要,即使我关闭了我的应用程序,Android服务应该运行,哪个将更新我的时钟。因此,当我打开我的申请时,我可以看到最新的时间。

我的代码工作正常,但经过很长一段时间后,服务自行停止。因此,计时器停止,我在应用程序中看到时间。

所以,请告诉我,如何限制服务自行停止。

2 个答案:

答案 0 :(得分:0)

可以通过START_REDELIVER_INTENT,START_STICKY

实现这一目标
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    // START_REDELIVER_INTENT
    return START_STICKY;
}

答案 1 :(得分:0)

Android系统只会在内存不足时强制停止服务,并且必须为具有用户焦点的活动恢复系统资源。

但是你可以使用startForeground(int,Notification)。 http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)

您可以在此处找到许多有用的信息:http://developer.android.com/guide/components/services.html