【问题标题】:Android doze mode安卓打盹模式
【发布时间】:2016-05-28 19:38:13
【问题描述】:

在我的应用程序中,我将使用警报来通知用户事件,问题是 在 Android 版本 6.x 上-
警报会在准确时间的相同分钟后发出。
仅当设备进入睡眠模式时。

我尝试在闹钟响起之前唤醒设备,但无济于事。

谁能帮助我?这是我的代码

设置闹钟

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), pendingIntent);
        if (minutesBefore > 0) {
            beforeCalendar.add(Calendar.MINUTE, -minutesBefore);
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, beforeCalendar.getTimeInMillis(), beforePendingIntent);
        }
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), pendingIntent);
        if (minutesBefore > 0) {
            beforeCalendar.add(Calendar.MINUTE, -minutesBefore);
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, beforeCalendar.getTimeInMillis(), beforePendingIntent);
        }
    } else {
        alarmManager.set(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), pendingIntent);

        if (minutesBefore > 0) {
            beforeCalendar.add(Calendar.MINUTE, -minutesBefore);
            alarmManager.set(AlarmManager.RTC_WAKEUP, beforeCalendar.getTimeInMillis(), beforePendingIntent);
        }
    }

弱锁

当我收到广播接收器发出的警报时,我会尝试唤醒设备 通过此代码从打盹模式

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        //Object flags;

            wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Alarm_WakeLock");

        wl.acquire();

但同样的几分钟后,警报仍然发出!

【问题讨论】:

  • 请您发布您的代码
  • 您是否在警报触发后执行任何与网络相关的操作(例如在服务中)?
  • 不,我的服务只是将一个活动显示为alarmActivity,以通过一些信息通知用户..

标签: android android-studio alarmmanager


【解决方案1】:

打盹模式是为了在一段时间不使用手机时节省电池电量。它通过推迟像 AlarmManager 这样的操作来做到这一点。

如果您确实需要在准确的时间触发警报,请使用 [setExactAndAllowWhileIdle()](https://developer.android.com/reference/android/app/AlarmManager.html#setExactAndAllowWhileIdle(int, long, android.app.PendingIntent))。

docs 说:

如果您需要设置在打瞌睡时触发的警报,请使用 setAndAllowWhileIdle() 或 setExactAndAllowWhileIdle()。

【讨论】:

  • 我试过了,但没有任何改变,我用我的代码更新我的问题看看吧
【解决方案2】:

docs 看来,即使在打瞌睡时 setExactAndAllowWhileIdle 也会触发,所以这应该对您有用,但您可以尝试 setAlarmClock。

使用 setAlarmClock() 设置的警报继续正常触发 - 系统在这些警报触发前不久退出打盹。

所以你可以使用它。但它旨在用于用户认为的闹钟——手机将提醒用户的预定时间(不仅仅是日历事件)。状态栏中甚至会在状态栏中显示一个图标作为物理闹钟面,并且许多锁屏还会通知用户何时安排下一个闹钟。

参考 https://www.bignerdranch.com/blog/diving-into-doze-mode-for-developers/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 2016-06-27
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 2023-03-28
    • 2016-06-21
    相关资源
    最近更新 更多