在Android 8.1.0中,AlarmManager无法正常工作。

3

我的需求是在特定的时间,如用户生日或节日设置通知

我正在使用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);
    }

请参考以下链接:https://dev59.com/QVYO5IYBdhLWcg3wIuPp#46305462 和 https://medium.com/@iiro.krankka/its-time-to-kiss-goodbye-to-your-implicit-broadcastreceivers-eefafd9f4f8a - Radhey
2个回答

1

你能给一些作业调度器的例子吗?我尝试过,但无法安排一年后的作业。 - Joker
但如果您使用工作管理器,则无需使用作业调度程序。 - tushar
好的,你能提供一个WorkManager的最小示例,以帮助调度作业吗? - Joker
你可以在网上找到教程,并查看下面提到的链接:https://dev59.com/-VUL5IYBdhLWcg3wbni4 - tushar
如果我需要在特定时间启动任务怎么办?WorkManager 不适用于此(任务可以被延迟)。 - Vadim Kotov

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接