【问题标题】:AlarmManager not working in Android 8.1.0AlarmManager 在 Android 8.1.0 中不起作用
【发布时间】:2019-05-02 12:53:37
【问题描述】:

我的要求设置为Notification 特定时间,例如用户生日或假期

我正在使用 AlarmManager 使用广播接收器安排通知

代码在6.0 中运行良好(即使应用程序被杀死,从最近的列表中滑动)但在Android 8.1.0 上无法运行(Mf : Oppo)

阅读了ThisThis 以及许多答案,但没有找到任何帮助 知道如何解决这个问题

这是我的代码

    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);
    }

【问题讨论】:

标签: java android


【解决方案1】:

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

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

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

【讨论】:

  • 你能举一些我尝试过但一年后没有工作的工作调度员的例子
  • 但是如果您使用工作管理器,则无需使用作业调度程序。
  • 哦,你能提供一个可以帮助安排工作的工作管理器的最小示例
  • 你可以在网上找到教程,看看提到的链接:stackoverflow.com/questions/50363541/…
  • 如果我需要在特定时间触发任务怎么办? Workmanager 不适合这个(任务可以延期)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多