I have a class inherited from BroadcastReceiver, it creates reminders, I also have an activit that creates and saves the time list from and to as a string, how can I make the reminders not work (postponed) at these intervals? The problem is to pass this string to the class BroadcastReceiver, because the application is not active, then you need to download this deadline from the activity. To save using SharedPreferences. Can use serialization? But then where to save the file? and I do not really like this decision. In the class BroadcastReceiver has not yet written the implementation of processing the desired string.

public class Alarm extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent notificationIntent = new Intent(context, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); Resources res = context.getResources(); Notification.Builder builder = new Notification.Builder(context); builder.setContentIntent(contentIntent) .setSmallIcon(R.drawable.u) // большая картинка .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.w)) //.setTicker(res.getString(R.string.warning)) // текст в строке состояния .setTicker("Последнее китайское предупреждение!") .setWhen(System.currentTimeMillis()) .setAutoCancel(true) .setDefaults(Notification.DEFAULT_ALL) //.setContentTitle(res.getString(R.string.notifytitle)) // Заголовок уведомления .setContentTitle("Напоминание") //.setContentText(res.getString(R.string.notifytext)) .setContentText("У вас есть колоды требующие повторения."); // Текст уведомления // Notification notification = builder.getNotification(); // до API 16 Notification notification = builder.build(); NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1, notification); ComponentName receiver = new ComponentName(context, Alarm.class); PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); } public void ssetAlarm(Context context, Date ddr, Date dateBl) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(ddr.getTime()); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, Alarm.class); intent.putExtra(String.valueOf(dateBl.getTime()), String.valueOf(dateBl.getTime()));//Задаем параметр интента PendingIntent pi = PendingIntent.getBroadcast(context, (int) dateBl.getTime(), intent, 0); am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi); } public void cancelAlarm(Context context,Date ddr) { Intent intent = new Intent(context, Alarm.class); PendingIntent sender = PendingIntent.getBroadcast(context, (int) ddr.getTime(), intent, 0); // AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); sender.cancel(); // if (alarmManager != null) { // alarmManager.cancel(sender); // } } } 

And tell me at the same time, please, is the mechanism of this class implemented correctly? I am confused by this part, maybe it should be in another place of the program?

  ComponentName receiver = new ComponentName(context, Alarm.class); PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); 
  • And correctly confused - it is pointless to turn on the receiver when it is already running. And with reference to this class, this code is not needed at all. - woesss
  • And where should I push this receiver? - Yevgen Lishchenko
  • In the sense of "shove"? I meant the code at the end of the question - it is superfluous here, the rest seems to be true. - woesss
  • I understand that this receiver is needed so that after the device restart the alarm the manager still worked according to the schedule, but where should this piece of code be located? In the mainactivity he gives an error) - Evgeny Lischenko
  • No - your receiver only displays a notification, that's all. To restore the schedule after a reboot, you need a receiver for action BOOT_COMPLETED : developer.android.com/training/scheduling/alarms.html#boot . And in it to install all alarms again. Naturally, for this you need to create all of them when you create a “memorize” in SharedPreferences , a file, or a base - as you prefer. - woesss

0