在 Android 6.0 处于 Doze 模式时如何使闹钟管理器工作? [英] How to make Alarm Manager work when Android 6.0 in Doze mode?

查看:39
本文介绍了在 Android 6.0 处于 Doze 模式时如何使闹钟管理器工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Google Play 上两个闹钟应用的开发者.我正在尝试让它们与 Android 6.0 一起使用.但是,打盹模式使其不会响铃.我把它们放在白名单上,我放了一个前台通知图标,我不知道我还能做什么 - 在打盹模式下,警报管理器警报仍然被忽略.但是,时钟应用程序(它是 Google Play 而不是 AOSP 应用程序)则不同.在时钟应用上启用闹钟后,adb deviceidle step"将始终显示为active",而不会显示为idle"、idle_pending"或其他任何内容.

I am a developer of two alarm clock apps on Google Play. I am trying to get them to work with Android 6.0. However, Doze mode makes it so they do not ring. I put them on the white list, I put a foreground notification icon up, I'm not sure what else I can do - when in Doze mode, the the Alarm Manager alarms are still ignored. The Clock app (which is a Google Play rather than AOSP app), however, is different. When the alarm is enabled on the Clock app, "adb deviceidle step" will always read "active" and never "idle", "idle_pending" or anything else.

Android 是否在作弊,给它自己的应用更多的权力,也就是.拔苹果"?Google Play 上的所有闹钟应用都将无法使用了吗?有点担心,这些高质量的应用程序每个都需要一年的兼职开发时间,对我来说是很大的收入来源.任何关于我如何让这些工作的线索都会有很大的帮助.

Is Android cheating here, giving its own app more power, aka. "pulling an apple"? Are all alarm clock apps on Google Play about to become non-functional? Kind of worried here, these are quality apps that each took a year of part-time development time, and are big income sources for me. Any clues on how I could get these to work would be a huge help.

设置AlarmManager意图:

Setting the AlarmManager intent:

        Intent intent = new Intent(context, ReceiverAlarm.class);
        if (android.os.Build.VERSION.SDK_INT >= 16) {
            intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        }
        amSender = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT); //FLAG_CANCEL_CURRENT seems to be required to prevent a bug where the intent doesn't fire after app reinstall in KitKat
        am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, scheduleToTime+1, amSender);

和 ReceiverAlarm 类:

and the ReceiverAlarm class:

public class ReceiverAlarm extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    if (wakeLock == null) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Theme.appTitle);
        wakeLock.acquire();
    }
    X.alarmMaster.startRingingAlarm(true);
}

以及X.alarmMaster.startRingingAlarm()方法的相关部分:

and the relevant parts of the X.alarmMaster.startRingingAlarm() method:

    if (wakeLock == null) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Theme.appTitle);
        wakeLock.acquire();
    }

    if (screenWakeLock == null) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        screenWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, Theme.appTitle+" scr");
        screenWakeLock.acquire();
    }

    Intent alarmIntent = new Intent(Intent.ACTION_VIEW);
    alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    alarmIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    alarmIntent.setClass(context, ActivityAlarmAlarm.class);

    context.startActivity(alarmIntent);

为了更容易阅读,一些方法已被内联粘贴.

Some of the methods have been pasted inline for easier readability.

推荐答案

Doze 和 App Standby 肯定会改变闹钟和唤醒锁的行为,但它们绝对不是你的世界末日!

Doze and App Standby definitely change the behavior in regards to alarms and wakelocks, but they're definitely not the end of the world for you!

您是否尝试过使用方法 setAlarmclock() 而不是 set()?它是专为闹钟设计的,可能能够打瞌睡.您可以使用一些 adb 命令手动将手机置于休眠或应用待机模式:https://developer.android.com/preview/features/power-mgmt.html

Have you tried using the method setAlarmclock() instead of set()? It's designed specifically for alarm clocks and may be able to cut through doze. There are a few adb commands you can use to manually put a phone into doze or app standby mode: https://developer.android.com/preview/features/power-mgmt.html

如果这不能唤醒您的应用程序,则有一个万无一失的方法 setExactAndAllowWhileIdle() 旨在无论如何都将手机从瞌睡中唤醒.最坏的情况是,您可以使用此方法唤醒您的应用,并使用唤醒来安排下一个闹钟.

If that isn't able to wake your app up, there's the surefire method setExactAndAllowWhileIdle() is designed to wake the phone from doze no matter what. Worst case scenario, you can wake your app up with this method and use the wakeup to schedule the next alarm.

另一个值得一读的页面是这篇博客文章,其中包含后台工作和警报的流程图:https://plus.google.com/+AndroidDevelopers/posts/GdNrQciPwqo

Another page worth a read is this blog post with its flowchart for background work and alarms: https://plus.google.com/+AndroidDevelopers/posts/GdNrQciPwqo

这篇关于在 Android 6.0 处于 Doze 模式时如何使闹钟管理器工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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