I want to keep the notification time that I set even after BOOT_COMPLEDTED. I'm just making DeviceBootReceiver that notice BOOT_COMPLETED, and ReminderReceiver that notify for the user with notification. My code is like this.
DeviceBottomReceiver
public class DeviceBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "BOOT_COMPLETED", Toast.LENGTH_SHORT).show();
SharedPreferences sharedPreferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
Calendar now = Calendar.getInstance();
Calendar reminder = new GregorianCalendar();
reminder.setTimeInMillis(sharedPreferences.getLong("reminder", Calendar.getInstance().getTimeInMillis()));
if (now.after(reminder)) {
reminder.add(Calendar.DATE, 1);
}
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
reminder.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
PendingIntent.getBroadcast(context, 0, new Intent(context, ReminderReceiver.class), 0));
}
}
}
}
ReminderReceiver
public class ReminderReceiver extends BroadcastReceiver {
private static final String REMINDER_CHANNEL = "reminder";
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "ReminderReceiver", Toast.LENGTH_SHORT).show();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(REMINDER_CHANNEL, "channel name", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, REMINDER_CHANNEL);
PendingIntent mainIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Reminder")
.setContentText("This is time for you")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentIntent(mainIntent)
.setPriority(NotificationCompat.PRIORITY_MAX);
notificationManager.notify(2, builder.build());
Calendar reminder = Calendar.getInstance();
reminder.add(Calendar.DATE, 1);
SharedPreferences.Editor editor = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE).edit();
editor.putLong("reminder", reminder.getTimeInMillis());
editor.apply();
}
}
In DeviceBottReceiver, when I boot my mobile phone, toast message "BOOT_COMPLETED" comes out well. But in ReminderReceiver, did not come out toast message "ReminderReceiver". How can I send a broadcast receiver to another broadcast receiver when mobile reboot??
I am using your code and its working properly. To debug use this cmd
In Manifest: