我自己的应用程序使用的技术与 2016 年的 Google I/O 应用程序显示的完全相同。查看源代码
我需要在一个非常具体的时间点提醒用户——使用通知。
为此,我使用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) }
现在,谷歌自己的实现显然被破坏了——即使我不想做任何繁重的后台工作,我也不能再使用这种技术了。但是我应该如何在一个非常特定的时间点唤醒用户呢?(把我的应用想象成一个闹钟)