我的要求設置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

隔江千里
TA貢獻1906條經驗 獲得超10個贊
如果您的任務對時間要求很高,則不建議使用 WorkManager API,因為您的警報管理器無法正常工作,在 Android 8 上使用 AlarmManager.setexactandallowwhileidle()
,設置準確時間并允許在應用程序被殺死時喚醒通知。
添加回答
舉報
0/150
提交
取消