安卓:重启服务
我的应用程序有一个服务,每 X 分钟对数据库执行与 stopSelf() 相同的操作,并进入 onDestroy 方法,我已放置此代码以在同一时间后重新启动服务:
@Override
public void onDestroy() {
super.onDestroy();
Intent intent_service = new Intent(context,vampireService.class);
PendingIntent pintent = PendingIntent.getService(context, 0, intent_service,0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime()+ 4500000, pintent);
}
但我不明白为什么如果我的手机进入睡眠状态模式服务不重启!当我重新启动显示器电源时,似乎 AlarmManager 的计数时间开始......这可能吗?如果是,我该如何解决这个问题?
谢谢
My application have a Service that every X minutes do same action on the database than it stopSelf() and into onDestroy method I have palced this code for restart the service after same time:
@Override
public void onDestroy() {
super.onDestroy();
Intent intent_service = new Intent(context,vampireService.class);
PendingIntent pintent = PendingIntent.getService(context, 0, intent_service,0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime()+ 4500000, pintent);
}
But I don't understand why if my phone go in sleep mode the service not restart ! Appears that the count time of AlarmManager to start when I power back up the display....it's possibile ? If yes, how can I resolve this problem ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 ELAPSED_REALTIME 的文档中...
尝试使用 ELAPSED_REALTIME_WAKEUP 看看是否有帮助(不确定是否有帮助)但是将适用于服务)。
From the documentation for ELAPSED_REALTIME...
Try using ELAPSED_REALTIME_WAKEUP to see if that helps (not sure if it will work for a service however).
您可以(并且应该)使用警报管理器安排您的服务每 X 次运行一次。您可以选择 setRepeating。
You can (and should) schedule your Service to run every X time, using the Alarm Manager. You have this option of setRepeating.