关闭应用程序后,Android AlarmManager在某些设备上无法使用 [英] Android AlarmManager not working on some devices when the app is closed

查看:417
本文介绍了关闭应用程序后,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在某些设备上无法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆