通知在不遵循 AlarmManager 的情况下工作

我创建了两个不同的每日通知。两者都将在不同的给定时间运行。第二个通知将从 API 获取一些数据,检查来自该 API 的数据是否与今天的日期匹配,并显示它。


每次我打开警报时都会运行第一个通知(我已经创建了打开或关闭通知的设置),但它不会在给定的时间运行。


另一个甚至不会运行 BroadcastReceiver 类,即使我已经设置了警报。


这是我的代码


MainActivity.java


public static void setAlarmDaily(Context context){

    Calendar calendar = Calendar.getInstance();

    calendar.set(Calendar.HOUR_OF_DAY, 7);

    calendar.set(Calendar.MINUTE, 0);

    calendar.set(Calendar.SECOND,0);

    Intent intent = new Intent(context, NotifyService.class);

    intent.putExtra(EXTRA_TYPE, DAILY_REQUEST_CODE);

    AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,100,intent,PendingIntent.FLAG_UPDATE_CURRENT);


    if(alarmManager != null){

        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);

    }

}


public static void setAlarmRelease(Context context){

    Intent intent = new Intent(context, NotifyService.class);

    intent.putExtra(EXTRA_TYPE, LATEST_REQUEST_CODE);

    AlarmManager alarmManager1 = (AlarmManager)context.getSystemService(ALARM_SERVICE);

    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context,101,intent,PendingIntent.FLAG_UPDATE_CURRENT);


    Calendar calendar1 = Calendar.getInstance();

    calendar1.set(Calendar.HOUR_OF_DAY, 8);

    calendar1.set(Calendar.MINUTE, 0);

    calendar1.set(Calendar.SECOND,0);


    if(alarmManager1 != null){

        alarmManager1.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent1);

    }

}


幕布斯6054654
浏览 88回答 1
1回答

慕桂英3389331

尝试改变alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,&nbsp; &nbsp; &nbsp;calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);到&nbsp;long dateTime = calendar.getTimeInMillis();&nbsp;if (dateTime <= System.currentTimeMillis()) {&nbsp; &nbsp; &nbsp;time = dateTime + 24 * 3600 * 1000;&nbsp;}&nbsp;&nbsp;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {&nbsp; &nbsp; &nbsp; &nbsp; alarmManager.setWindow(AlarmManager.RTC_WAKEUP, dateTime ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AlarmManager.INTERVAL_DAY, pendingIntent);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, dateTime ,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AlarmManager.INTERVAL_DAY, pendingIntent);&nbsp; &nbsp; }如果您阅读了与AlarmManager相关的 Android 文档从 API 19 (Build.VERSION_CODES.KITKAT) 开始,警报传递不准确:操作系统将转移警报以尽量减少唤醒和电池使用。有新的 API 来支持需要严格交付保证的应用程序;请参阅setWindow(int, long, long, android.app.PendingIntent)和setExact(int, long, android.app.PendingIntent)。targetSdkVersion 早于 API 19 的应用程序将继续看到以前的行为,即所有警报都在请求时准确传递。编辑:我建议在完成各自的任务后,在您的 broadcastReceiver 中调用您的setAlarmDaily和方法。setAlarmRelease并更改一次警报的方法。像这样:long dateTime = calendar.getTimeInMillis();&nbsp;if (dateTime <= System.currentTimeMillis()) {&nbsp; &nbsp; &nbsp;time = dateTime + 24 * 3600 * 1000;&nbsp;}&nbsp;&nbsp;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {&nbsp; &nbsp; &nbsp; &nbsp; //alarmManager.setExact(AlarmManager.RTC_WAKEUP, dateTime, pendingIntent); // not suggested&nbsp; &nbsp; &nbsp; &nbsp; // 15 mins of window to call the alarm and have battery optimization&nbsp; &nbsp; &nbsp; &nbsp; long windowMillis = 15 * 60 * 1000L;&nbsp; &nbsp; &nbsp; &nbsp; alarmManager.setWindow(AlarmManager.RTC_WAKEUP, dateTime ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; windowMillis, pendingIntent);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; alarmManager.set(AlarmManager.RTC_WAKEUP, dateTime, pendingIntent);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java