【发布时间】:2016-05-24 15:33:09
【问题描述】:
我开发了一个应用程序,它使用 AlarmManager 设置需要在一年中的某个时间触发的一堆警报(通常大约 50 个)。 这是我使用的代码,因为 4.4 kitkat 更改了 AlarmManager。
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
long setDate = fireDate.getTime(); // it's a calendar date defined above
Intent intent = new Intent(LOCAL_DISPLAY_MESSAGE_ACTION);
PendingIntent pending = PendingIntent.getBroadcast(ctx,
id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.RELEASE.startsWith("6")) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, setDate, pending);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
am.setExact(AlarmManager.RTC_WAKEUP, setDate, pending);
} else {
am.set(AlarmManager.RTC_WAKEUP, setDate, pending);
}
除了上面的代码,我正在使用清单中正确定义的广播接收器。
public class LocalReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
PushWakeLocker.acquire(context);
// do some stuff
PushWakeLocker.release();
}
}
更多信息可能会有所帮助。
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
自几个月前以来,我只收到了三星设备(5.0 / 5.1 android 版本)的差评,它们根本没有收到本地通知。我的意思是,它没有发出警报,似乎设备跳过它或没有唤醒。
在测试中,主要是使用 5.0.1 的三星 S4,我总是按时收到警报,所以这让我抓狂。 仅供参考,这段代码一直运行良好。
我对此进行了很多研究,但不幸的是我没有得到任何有用的信息。并不是他们会延迟收到警报(正如我在一些线程中所读到的),而是他们根本没有收到警报。所以这与棒棒糖和警报管理器中的已知问题无关。
感谢您的宝贵时间,欢迎提出任何建议!
【问题讨论】:
-
“这是我正在使用的代码”——我会将您的
if更改为检查Build.VERSION_CODES.M。 “我正在使用广播接收器”——你的WakeLock是不必要的,因为AlarmManager触发时,onReceive()在系统提供的WakeLock中被调用。但是,除非“做一些事情”只有几毫秒,否则您应该将该逻辑移至IntentService并使用WakefulBroadcastReceiver。除此之外,三星(如 SONY 等)在 6.0 之前有自己的打盹模式设置,因此AlarmManager是不可靠的,除非您被添加到某些特定于设备的白名单中。
标签: android alarmmanager android-5.0-lollipop samsung-mobile android-5.1.1-lollipop