设置警报没有在正确的时间启动

fd3cxomn  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(284)

我正在创建一个应用程序,它应该在正确的时间根据日期显示通知。但是通知来得晚或者是在打开应用程序之后。我请求您帮助我,指出代码中的错误,并对其不正确的原因进行注解。
androidmanifest.xml:

  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  2. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  3. <uses-permission android:name="android.permission.VIBRATE"/>
  4. <uses-permission android:name="android.permission.SET_ALARM" />
  5. <receiver
  6. android:name="com.zalj.schedule.MyNotifications.MyAlarm"
  7. android:enabled="true"
  8. android:exported="true"
  9. android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
  10. </receiver>

我按如下方式设置闹钟:

  1. private static void setAlarm(
  2. Context context,
  3. int id,
  4. Action action){
  5. Calendar calendar = Calendar.getInstance();
  6. AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
  7. calendar.set(Calendar.HOUR_OF_DAY, action.getStartHour());
  8. calendar.set(Calendar.MINUTE, action.getStartMinute());
  9. calendar.set(Calendar.SECOND, 0);
  10. Intent intent = new Intent(context, MyAlarm.class);
  11. //Устанавливаем команду
  12. intent.putExtra(
  13. IntentHelper.COMMAND,
  14. IntentHelper.COMMAND_NOTIFICATION_SetAlarm);
  15. //ChanelId
  16. intent.putExtra(
  17. IntentHelper.CHANEL_ID,
  18. CHANEL_ID_ACTION);
  19. //ChanelName
  20. intent.putExtra(
  21. IntentHelper.CHANEL_NAME,
  22. context.getString(R.string.Notification_chanelName_Action));
  23. //ID
  24. intent.putExtra(
  25. IntentHelper.NOTIFICATION_ID,
  26. NOTIFICATION_ID_ACTION);
  27. //Title
  28. intent.putExtra(
  29. IntentHelper.NOTIFICATION_TITLE,
  30. action.getTitle());
  31. //Message
  32. intent.putExtra(
  33. IntentHelper.NOTIFICATION_MESSAGE,
  34. action.getMessage());
  35. PendingIntent pIntent =
  36. PendingIntent.getBroadcast(
  37. context,
  38. id,
  39. intent,
  40. PendingIntent.FLAG_UPDATE_CURRENT);
  41. //Set alarm
  42. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
  43. alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
  44. else
  45. alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
  46. }

myalarm类:

  1. public class MyAlarm extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. int idCase = intent.getIntExtra(
  5. IntentHelper.COMMAND,
  6. IntentHelper.COMMAND_NOTIFICATION_UpdateAlarmToDay);
  7. switch (idCase){
  8. case IntentHelper.COMMAND_NOTIFICATION_SetAlarm:{
  9. showNotification(context ,intent);
  10. }break;
  11. case IntentHelper.COMMAND_NOTIFICATION_UpdateAlarmToDay:{
  12. updateAlarmsToNextDay(context, intent);
  13. }break;
  14. }
  15. }
  16. private void showNotification(Context context, Intent intent){
  17. NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
  18. String title;
  19. String message;
  20. String chanelId;
  21. String chanelNameDiscipline;
  22. int notificationId;
  23. title = intent.getStringExtra(IntentHelper.NOTIFICATION_TITLE);
  24. message = intent.getStringExtra(IntentHelper.NOTIFICATION_MESSAGE);
  25. chanelId = intent.getStringExtra(IntentHelper.CHANEL_ID);
  26. chanelNameDiscipline = intent.getStringExtra(IntentHelper.CHANEL_NAME);
  27. notificationId = intent.getIntExtra(IntentHelper.NOTIFICATION_ID, 0);
  28. NotificationCompat.Builder notification = new NotificationCompat.Builder(context, chanelId);
  29. notification
  30. .setSmallIcon(R.drawable.discipline_notification)
  31. .setContentTitle(title)
  32. .setContentText(message)
  33. .setStyle(new NotificationCompat.BigTextStyle())
  34. .setPriority(NotificationCompat.PRIORITY_DEFAULT)
  35. .setSound(NotificationHelper.getSound())
  36. .setVibrate(NotificationHelper.getVibrate(NotificationHelper.LONG_VIBRATE))
  37. .setAutoCancel(true);
  38. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
  39. {
  40. NotificationChannel nc =
  41. new NotificationChannel(
  42. chanelId,
  43. chanelNameAction,
  44. NotificationManager.IMPORTANCE_DEFAULT);
  45. notificationManager.createNotificationChannel(nc);
  46. }
  47. notificationManager.notify(notificationId, notification.build());
  48. }
  49. private void updateAlarmsToNextDay(Context context, Intent intent){
  50. Action action;
  51. String name = intent.getStringExtra(IntentHelper.ACTIONS_NAME);
  52. if (name == null){
  53. SharedPreferences settings = context.getSharedPreferences(
  54. DataContract.MyAppSettings.LAST_VIEWED_ACTIONS,
  55. Context.MODE_PRIVATE);
  56. name = settings.getString(
  57. DataContract.MyAppSettings.LAST_ACTIONS,
  58. DataContract.MyAppSettings.NULL);
  59. }
  60. ScheduleBuilder scheduleBuilder = ScheduleBuilder.getInternalSchedule(context, name);
  61. schedule = scheduleBuilder.build();
  62. schedule.updateTimes(context);
  63. schedule.updateActions(context, Calendar.getInstance());
  64. MyDisciplineNotificationManager.updateAllAlarm(context, schedule);
  65. }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题