How to send reciver from another BroadcastReceiver?

142 Views Asked by At

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??

1

There are 1 best solutions below

0
Kishan Maurya On

I am using your code and its working properly. To debug use this cmd

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -p  yourpackagename

In Manifest:

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<receiver android:name=".DeviceBootReceiver"
            android:enabled="true"
            android:exported="true"
            android:label="BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
        <receiver android:name=".ReminderReceiver"/>