Android:如何使用 AlarmManager 每 15 分钟重复一次服务,但仅从上午 8:00 运行到下午 18:00?

发布于 2024-11-07 13:49:03 字数 72 浏览 4 评论 0原文

我需要定期检查数据更新,但数据仅在白天更新,因此我希望仅在该时间段运行此重复操作以节省电池和带宽。

我应该怎么办?

I need to check data update periodly, but the data is only updating during the daytime, so I want this repeating action run only in that time section for saving battery and bandwidth.

What should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

凉墨 2024-11-14 13:49:03

如果服务通过 HTTP get/post/whatever 请求与云通信,请注意 C2DM 解决方案可以延长电池寿命,并且 SyncAdapter 解决方案可以提供一些好处。 (我建议观看有关这两个主题的 Google I/O 视频。)

以下代码执行的操作与您最初询问的内容接近。

public class MyUpdateService extends IntentService
{
  public MyUpdateService()
  {
    super(MyUpdateService.class.getSimpleName());
  }

  @Override
  protected void onHandleIntent(Intent intent)
  {
    // Do useful things.

    // After doing useful things...
    scheduleNextUpdate();
  }

  private void scheduleNextUpdate()
  {
    Intent intent = new Intent(this, this.getClass());
    PendingIntent pendingIntent =
        PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // The update frequency should often be user configurable.  This is not.

    long currentTimeMillis = System.currentTimeMillis();
    long nextUpdateTimeMillis = currentTimeMillis + 15 * DateUtils.MINUTE_IN_MILLIS;
    Time nextUpdateTime = new Time();
    nextUpdateTime.set(nextUpdateTimeMillis);

    if (nextUpdateTime.hour < 8 || nextUpdateTime.hour >= 18)
    {
      nextUpdateTime.hour = 8;
      nextUpdateTime.minute = 0;
      nextUpdateTime.second = 0;
      nextUpdateTimeMillis = nextUpdateTime.toMillis(false) + DateUtils.DAY_IN_MILLIS;
    }
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
  }
}

If the service is talking to the cloud with HTTP get/post/whatever requests, then note that a C2DM solution would net better battery life, and that a SyncAdapter solution could provide a few benefits. (I recommend watching the Google I/O videos on both topics.)

The following code does something close to what you originally asked about.

public class MyUpdateService extends IntentService
{
  public MyUpdateService()
  {
    super(MyUpdateService.class.getSimpleName());
  }

  @Override
  protected void onHandleIntent(Intent intent)
  {
    // Do useful things.

    // After doing useful things...
    scheduleNextUpdate();
  }

  private void scheduleNextUpdate()
  {
    Intent intent = new Intent(this, this.getClass());
    PendingIntent pendingIntent =
        PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // The update frequency should often be user configurable.  This is not.

    long currentTimeMillis = System.currentTimeMillis();
    long nextUpdateTimeMillis = currentTimeMillis + 15 * DateUtils.MINUTE_IN_MILLIS;
    Time nextUpdateTime = new Time();
    nextUpdateTime.set(nextUpdateTimeMillis);

    if (nextUpdateTime.hour < 8 || nextUpdateTime.hour >= 18)
    {
      nextUpdateTime.hour = 8;
      nextUpdateTime.minute = 0;
      nextUpdateTime.second = 0;
      nextUpdateTimeMillis = nextUpdateTime.toMillis(false) + DateUtils.DAY_IN_MILLIS;
    }
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
  }
}
翻了热茶 2024-11-14 13:49:03

按照这些简单的步骤,让服务在 Android 设备中永远保持活力。
1. 每 15 分钟使用警报管理器调用一次服务。
2.在onStart方法中返回START_STICKY。
3. 在销毁时调用警报管理器并使用以下命令重新启动服务
启动服务方法。
4.(可选)重复 onTaskRemoved 方法中的第 3 点。

Follow these easy steps to keep servce alive forever in android device.
1. Call a service by using alarm manager for every 15 minutes.
2. return START_STICKY in onStart method.
3. In on destroy call the alarm manager and restart service by using
startService method.
4.(Optional)Repeat the point number 3 in onTaskRemoved method.

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