使用计时器/闹钟在启动时更新服务
我有一个在启动时启动的更新服务。问题是我希望它进行检查,然后等待一段时间并重新启动。当我的服务与应用程序绑定时,我使用闹钟执行此操作,但现在它是独立的,我只使用广播接收器在启动时启动它。问题是现在由于某种原因我无法将闹钟与它集成。 我只有 UpdateService 类和广播接收器类。到目前为止,我在广播接收器中的代码是这样的,但我想将闹钟放在这里,表示安排服务每 30 秒启动一次。我真的需要这个。
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, UpdateService.class);
context.startService(startServiceIntent);
}
欢迎任何建议。提前致谢。
I have an update service that starts at boot. The thing is I want it to make a check and then wait for a period of time and restart itself. I did this with alarm clock when my service was tied to an application but now it is independent and I only use a broadcast receiver to start it at boot. The problem is that now for some reason I can't integrate my alarm clock with it.
I only have my UpdateService class and my broadcastreceiver class.My code so far in the broadcastreceiver is this, but I want to put the alarm clock here to say schedule the service to start every 30 seconds. I really need this.
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, UpdateService.class);
context.startService(startServiceIntent);
}
Any suggestions are welcome. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了问题的答案:
最终,我的问题是我没有得到正确的上下文,如下所示:
更改为
(AlarmManager)context.getSystemService(context.ALARM_SERVICE);
I found the answer to my problem:
Eventually,my problem was that I didn't get the context right as follows:
changed to
(AlarmManager)context.getSystemService(context.ALARM_SERVICE);