【发布时间】:2017-07-10 15:35:36
【问题描述】:
我自己的应用使用了与 2016 年 Google I/O 应用所展示的完全相同的技术。see source
我需要在一个非常具体的时间点提醒用户 - 使用通知。
为此,我使用AlarmManager 在正确的时间点唤醒设备:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
am.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
} else {
am.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
}
pendingIntent 是这样创建的:
final Intent intent = new Intent(MyAlarmService.ACTION_NOTIFY_RIDE, null, this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
现在,我的 MyAlarmService 类是一个简单的 IntentService 处理唤醒只是为了为用户创建通知。
我在日志中得到的消息如下:
W/ActivityManager: Background start not allowed: service Intent { act=xxx.xxx.xxx.action.NOTIFY_RIDE flg=0x4 cmp=xxx.xxx.xxx./xxx.xxx.xxx.service.MyAlarmService (has extras) }
现在,Google 自己的实现显然已经坏了——即使我不想做任何繁重的后台工作,我也不能再使用这种技术了。但是我应该如何在一个非常特定的时间点唤醒用户呢? (把我的应用想象成一个闹钟)
【问题讨论】:
标签: alarmmanager android-notifications wakeup android-8.0-oreo