AlarmManager 在 Android 8.1.0 中不起作用

我的要求设置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);

    }


森栏
浏览 184回答 2
2回答

凤凰求蛊

您可以使用链接中提到的 workmanager。它支持所有 api 版本并且易于使用,因为它向后兼容 API 级别 14https://developer.android.com/topic/libraries/architecture/workmanagerhttps://medium.com/androiddevelopers/introducing-workmanager-2083bcfc4712

隔江千里

如果您的任务对时间要求很高,则不建议使用 WorkManager API,因为您的警报管理器无法正常工作,在 Android 8 上使用 AlarmManager.setexactandallowwhileidle(),设置准确时间并允许在应用程序被杀死时唤醒通知。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java