为了账号安全,请及时绑定邮箱和手机立即绑定

AlarmManager 在 Android 8.1.0 中不起作用

AlarmManager 在 Android 8.1.0 中不起作用

森栏 2022-11-02 17:15:27
我的要求设置Notification为特定时间,例如用户生日或假期我正在使用AlarmManager使用广播接收器来安排通知代码工作正常6.0(即使应用程序被杀死,从最近的列表中刷卡)但不能正常工作Android 8.1.0(Mf:Oppo)阅读了这个和这个以及许多答案,但没有找到任何有用的任何想法如何解决这个问题这是我的代码   AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);    //creating a new intent specifying the broadcast receiver\    Intent i = new Intent(this, HolidayBroadcast.class);    i.putExtra("eventName",islamicHoliday.getEventName());    i.putExtra("dateH", testTmp.getCalendar().getTimeInMillis());    i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);    //creating a pending intent using the intent    PendingIntent pi = PendingIntent.getBroadcast(this, new Random().nextInt(), i, PendingIntent.FLAG_UPDATE_CURRENT);    //setting alarm    if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M) {        am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,getTimeInMillis(), pi);    }    else    {        am.setExact(AlarmManager.RTC_WAKEUP, getTimeInMillis(), pi);    }
查看完整描述

2 回答

?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

您可以使用链接中提到的 workmanager。它支持所有 api 版本并且易于使用,因为它向后兼容 API 级别 14

https://developer.android.com/topic/libraries/architecture/workmanager

https://medium.com/androiddevelopers/introducing-workmanager-2083bcfc4712


查看完整回答
反对 回复 2022-11-02
?
隔江千里

TA贡献1906条经验 获得超10个赞

如果您的任务对时间要求很高,则不建议使用 WorkManager API,因为您的警报管理器无法正常工作,在 Android 8 上使用 AlarmManager.setexactandallowwhileidle(),设置准确时间并允许在应用程序被杀死时唤醒通知。



查看完整回答
反对 回复 2022-11-02
  • 2 回答
  • 0 关注
  • 176 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信