关闭应用程序后,Android AlarmManager在某些设备上无法使用

编程入门 行业动态 更新时间:2024-10-23 04:59:19
本文介绍了关闭应用程序后,Android AlarmManager在某些设备上无法使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图每半小时从AlarmManager setRepeating()运行IntentService.我想发送一个广播,从广泛的演员到意图服务.在该服务中,将完成某些功能.

但是,最初,AlarmManager在应用程序处于关闭状态时不会触发.

当我的应用程序正在运行或处于后台状态时,警报运行正常,而当我关闭应用程序时,该警报在某些设备上无法正常工作.

即使应用已关闭,我应该怎么办?

解决方案

来自setRepeating()的文档:

从API 19开始,所有重复的警报都是不精确的.

此外, setRepeating()不适用于打ze .

您应该使用确切的警报(根据设备的API级别由相应的AlarmManager方法设置):

if (Build.VERSION.SDK_INT >= 23) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent); } else if (Build.VERSION.SDK_INT >= 19) { alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent); } else { alarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent); }

并在每次射击时重新安排他们的时间.

对于重新计划,您可以将原始触发时间添加到Intent:

intent.putExtra(KEY_TRIGGER_TIME, triggerTime);

然后在onReceive()中检索此额外内容,为其添加所需的间隔,并使用新值重新安排警报:

@Override public void onReceive(Context context, Intent intent) { long triggerTime = intent .getLongExtra(KEY_TRIGGER_TIME, System.currentTimeMillis()); // adding one day to the current trigger time triggerTime += TimeUnit.DAYS.toMillis(1); // set a new alarm using the new trigger time // ... }

注意:正如上面评论中提到的@Opiatefuchs,某些制造商(例如Xiaomi或Huawei)可能会实现某些节电功能,这些功能可以防止触发警报并且无法以编程方式绕过警报./p>

I am trying to run IntentService as such from AlarmManager setRepeating() for every half an hour. I want to send a to broadcast, from broad cast to intent service. In the service, some functionality will be done.

But, initially the AlarmManager in not triggering while the app is in closed state.

When my app is running or in background state the alarm is working fine and when I close the app the alarm is not working in some devices.

What should I do to run the alarm even if the app is closed?

解决方案

From the documentation of setRepeating():

As of API 19, all repeating alarms are inexact.

Moreover, setRepeating() does not work with Doze.

You should use exact alarms (set by the appropriate AlarmManager method based on the API level of the device):

if (Build.VERSION.SDK_INT >= 23) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent); } else if (Build.VERSION.SDK_INT >= 19) { alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent); } else { alarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent); }

And reschedule them every time they fire.

For the rescheduling you could add the original trigger time to the Intent:

intent.putExtra(KEY_TRIGGER_TIME, triggerTime);

Then retrieve this extra in onReceive(), add your desired interval to it and reschedule the alarm using the new value:

@Override public void onReceive(Context context, Intent intent) { long triggerTime = intent .getLongExtra(KEY_TRIGGER_TIME, System.currentTimeMillis()); // adding one day to the current trigger time triggerTime += TimeUnit.DAYS.toMillis(1); // set a new alarm using the new trigger time // ... }

NOTE: As @Opiatefuchs mentioned in the comment above, some manufacturers (such as Xiaomi or Huawei) may implement certain battery saver functions that can prevent alarms from being fired and cannot be bypassed programmatically.

更多推荐

关闭应用程序后,Android AlarmManager在某些设备上无法使用

本文发布于:2023-11-27 09:24:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637498.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   设备   在某些   AlarmManager   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!