java - 如何每 5 分钟唤醒一次我的 Intent 服务

标签 java android android-intent intentservice android-intentservice

<分区>

我知道之前有人问过这个问题,但我没有得到任何答案,我想创建一个一直运行线程的 Intent 服务,但是当我从应用程序中退出时,我的服务停止了,然后线程也停了下来。我需要创建一些东西来每隔几分钟唤醒一次服务。或者即使在应用程序被终止或关闭时也能防止终止服务的东西。

这就是我开始服务的方式

Intent intent= new Intent(Intent.ACTION_SYNC,null,this,IntentServ.class);
startService(intent);

最佳答案

为此,您可以使用每 1 小时启动一次服务的 AlarmManager。例如:

 AlarmManager mgr = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
 Intent notificationIntent = new Intent(context,
                UpdateService.class);
 PendingIntent pendingIntent=PendingIntent.getService(context, requestCode, Intent.parseIntent(), 0);
  mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis(), AlarmManager.INTERVAL_HOUR, pendingIntent);

关于java - 如何每 5 分钟唤醒一次我的 Intent 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30390389/

相关文章:

java - AdMob 横幅太小

android - 搜索推特时出现 twitter4j 错误

android - 如何打开图库以选择多个图像?

android - 从广播 Intent 中获取 Parcelable 数据时出现 ClassNotFoundException

java - "Changing"复杂的不可变对象(immutable对象)

java - 为什么 javax.net 文件在 src.zip 中不可用?

java - 如何从 Guice 中按类获取对象列表?

javascript - Android - 从 Firebase 数据库中删除节点的问题

java - BitSet(JAVA) 在埃拉托斯特尼筛法的实现中抛出 outofBoundsException

java - 通过单击 Android 上 Twitter 应用程序中的推文来打开应用程序