Android 6.0 在打瞌睡模式下如何使警报管理器工作?
How to make Alarm Manager work when Android 6.0 in Doze mode?
我是 Google Play 上两个闹钟应用程序的开发者。我正在尝试让他们使用 Android 6.0。但是,打瞌睡模式可以让他们不响铃。我把它们放在白名单上,我把一个前景通知图标放在上面,我不确定我还能做什么——当处于打瞌睡模式时,警报管理器警报仍然被忽略。然而,时钟应用程序(这是一个 Google Play 而不是 AOSP 应用程序)是不同的。在时钟应用程序上启用闹钟后,"adb deviceidle step" 将始终显示为 "active",而不会显示为 "idle"、"idle_pending" 或任何其他内容。
Android 在这里作弊,给自己的应用程序更多的权力,又名。 "pulling an apple"? Google Play 上的所有闹钟应用程序是否都将无法运行?有点担心,这些都是高质量的应用程序,每个应用程序都花了一年的兼职开发时间,对我来说是很大的收入来源。任何关于我如何让这些工作的线索都将是一个巨大的帮助。
设置 AlarmManager 意图:
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 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()方法的相关部分:
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);
一些方法已内联粘贴以便于阅读。
Doze 和 App Standby 肯定会改变有关警报和唤醒锁的行为,但它们对您来说绝对不是世界末日!
您是否尝试过使用方法 setAlarmclock()
而不是 set()
?
它是专门为闹钟设计的,也许可以打瞌睡。您可以使用一些 adb 命令手动将 phone 置于打瞌睡或应用程序待机模式:https://developer.android.com/preview/features/power-mgmt.html
如果这无法唤醒您的应用程序,那么万无一失的方法 setExactAndAllowWhileIdle()
旨在无论如何将 phone 从休眠中唤醒。最坏的情况,你可以用这个方法唤醒你的应用程序,并使用唤醒来安排下一个闹钟。
另一个值得一读的页面是这个博客 post 及其后台工作和警报的流程图:https://plus.google.com/+AndroidDevelopers/posts/GdNrQciPwqo
将应用程序放入白名单只允许网络处于休眠模式。 AlarmManager 不受白名单影响。
对于setExactAndAllowWhileIdle()
方法,请查看SDK中的以下描述。它不会将 phone 从瞌睡中唤醒。
When the alarm is dispatched, the app will also be added to the
system's temporary whitelist for approximately 10 seconds to allow
that application to acquire further wake locks in which to complete
its work.
我是 Google Play 上两个闹钟应用程序的开发者。我正在尝试让他们使用 Android 6.0。但是,打瞌睡模式可以让他们不响铃。我把它们放在白名单上,我把一个前景通知图标放在上面,我不确定我还能做什么——当处于打瞌睡模式时,警报管理器警报仍然被忽略。然而,时钟应用程序(这是一个 Google Play 而不是 AOSP 应用程序)是不同的。在时钟应用程序上启用闹钟后,"adb deviceidle step" 将始终显示为 "active",而不会显示为 "idle"、"idle_pending" 或任何其他内容。
Android 在这里作弊,给自己的应用程序更多的权力,又名。 "pulling an apple"? Google Play 上的所有闹钟应用程序是否都将无法运行?有点担心,这些都是高质量的应用程序,每个应用程序都花了一年的兼职开发时间,对我来说是很大的收入来源。任何关于我如何让这些工作的线索都将是一个巨大的帮助。
设置 AlarmManager 意图:
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 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()方法的相关部分:
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);
一些方法已内联粘贴以便于阅读。
Doze 和 App Standby 肯定会改变有关警报和唤醒锁的行为,但它们对您来说绝对不是世界末日!
您是否尝试过使用方法 setAlarmclock()
而不是 set()
?
它是专门为闹钟设计的,也许可以打瞌睡。您可以使用一些 adb 命令手动将 phone 置于打瞌睡或应用程序待机模式:https://developer.android.com/preview/features/power-mgmt.html
如果这无法唤醒您的应用程序,那么万无一失的方法 setExactAndAllowWhileIdle()
旨在无论如何将 phone 从休眠中唤醒。最坏的情况,你可以用这个方法唤醒你的应用程序,并使用唤醒来安排下一个闹钟。
另一个值得一读的页面是这个博客 post 及其后台工作和警报的流程图:https://plus.google.com/+AndroidDevelopers/posts/GdNrQciPwqo
将应用程序放入白名单只允许网络处于休眠模式。 AlarmManager 不受白名单影响。
对于setExactAndAllowWhileIdle()
方法,请查看SDK中的以下描述。它不会将 phone 从瞌睡中唤醒。
When the alarm is dispatched, the app will also be added to the system's temporary whitelist for approximately 10 seconds to allow that application to acquire further wake locks in which to complete its work.