使用计时器/闹钟在启动时更新服务

发布于 2024-12-13 03:31:25 字数 450 浏览 2 评论 0原文

我有一个在启动时启动的更新服务。问题是我希望它进行检查,然后等待一段时间并重新启动。当我的服务与应用程序绑定时,我使用闹钟执行此操作,但现在它是独立的,我只使用广播接收器在启动时启动它。问题是现在由于某种原因我无法将闹钟与它集成。 我只有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伊面 2024-12-20 03:31:25

我找到了问题的答案:

private boolean service_started=false;
private PendingIntent mAlarmSender;

@Override
public void onReceive(Context context, Intent intent) { 
    if(!service_started){
        // Create an IntentSender that will launch our service, to be scheduled 
        // with the alarm manager.    
        mAlarmSender = PendingIntent.getService(context,  
                0, new Intent(context, UpdateService.class), 0);

        //We want the alarm to go off 30 secs from now.  
        long firstTime = SystemClock.elapsedRealtime();   
        // Schedule the alarm!       
        AlarmManager am = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);   
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,   
                firstTime,30*1000, mAlarmSender);    

        service_started=true;
    }
}

最终,我的问题是我没有得到正确的上下文,如下所示:

(AlarmManager)getSystemService(ALARM_SERVICE);

更改为
(AlarmManager)context.getSystemService(context.ALARM_SERVICE);

I found the answer to my problem:

private boolean service_started=false;
private PendingIntent mAlarmSender;

@Override
public void onReceive(Context context, Intent intent) { 
    if(!service_started){
        // Create an IntentSender that will launch our service, to be scheduled 
        // with the alarm manager.    
        mAlarmSender = PendingIntent.getService(context,  
                0, new Intent(context, UpdateService.class), 0);

        //We want the alarm to go off 30 secs from now.  
        long firstTime = SystemClock.elapsedRealtime();   
        // Schedule the alarm!       
        AlarmManager am = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);   
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,   
                firstTime,30*1000, mAlarmSender);    

        service_started=true;
    }
}

Eventually,my problem was that I didn't get the context right as follows:

(AlarmManager)getSystemService(ALARM_SERVICE);

changed to
(AlarmManager)context.getSystemService(context.ALARM_SERVICE);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文