I am writing an application in which I create a workout record (it may not be one), and in the settings I indicate in the checkbox to include a reminder or not. It is necessary that the reminder works in a separate thread, and is turned off correctly when the checBBox value changes. How to implement such logic? Maybe you need to use a handler? Below is an example of your code.
I create an intent, I transfer my created Service as a second argument (I registered a separate process for it in the manifest). In the training object, I saved the required time for the alarm to start, as well as the checkbox data.
Intent intent = new Intent(getApplicationContext(), NotifyService.class); intent.putExtra("training", training); startService(intent); Here is the actual code of the service itself:
public class NotifyService extends Service { @Override public void onCreate() { super.onCreate(); } public int onStartCommand(Intent intent, int flags, int startId) { Training training = (Training) intent.getParcelableExtra("training"); MyAlarm.scheduleNotification(training, getApplicationContext()); stopSelfResult(startId); return START_REDELIVER_INTENT; } public IBinder onBind(Intent arg0) { return null; } } Here, respectively, the implementation of MyAlarm:
public class MyAlarm { public static void scheduleNotification(Training training, Context context) { if (training.getRemind() == 1) { Notification.Builder builder = new Notification.Builder(context); Intent intent = new Intent(context, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pIntent = PendingIntent.getActivity(context, training.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT); Calendar currentTime = Calendar.getInstance(); Calendar trainingTime = DateTimeParser.getDateTime(training.getTime()); currentTime.setTimeInMillis(System.currentTimeMillis()); currentTime.set(Calendar.HOUR_OF_DAY, trainingTime.get(Calendar.HOUR_OF_DAY)); currentTime.set(Calendar.MINUTE, trainingTime.get(Calendar.MINUTE)); currentTime.set(Calendar.SECOND, 0); currentTime.set(Calendar.MILLISECOND, 0); if (Calendar.getInstance().after(currentTime)) { currentTime.add(Calendar.DAY_OF_MONTH, 1); } builder. setContentIntent(pIntent). setSmallIcon(R.mipmap.ic_launcher). setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)). setTicker("Нажми на меня"). setWhen(currentTime.getTimeInMillis()). setAutoCancel(true). setContentTitle(training.getName()). setContentText("Жми тут!"); Notification notif = builder.build(); //Intent notificationIntent = new Intent(this, NotificationPublisher.class); Intent notificationIntent = new Intent("START_ALARM"); notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, training.getId()); notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notif); notificationIntent.putExtra("training", training); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, training.getId(), notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.setExact(AlarmManager.RTC_WAKEUP, currentTime.getTimeInMillis(), pendingIntent); } else if (training.getRemind() == 0) { Intent notificationIntent = new Intent("START_ALARM"); notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, training.getId()); notificationIntent.putExtra("training", training); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, training.getId(), notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(pendingIntent); } } } And BroadcastReceivera, here restart the scheduleNotification method, since setRepeating is not working since API 19:
public class NotificationPublisher extends BroadcastReceiver { public static String NOTIFICATION_ID = "notification-id"; public static String NOTIFICATION = "notification"; public void onReceive(Context context, Intent intent) { Calendar calendar = Calendar.getInstance(); Log.d("MyLogs", "Receiver Thread name : " + Thread.currentThread().getName()); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = intent.getParcelableExtra(NOTIFICATION); int id = intent.getIntExtra(NOTIFICATION_ID, 0); notificationManager.notify(id, notification); //Log.e("MyLogs","End"); Training training = intent.getParcelableExtra("training"); MyAlarm.scheduleNotification(training, context); } }