Skip to content Skip to sidebar Skip to footer

How Do I Place A Repeating Notification 8 Clock Set Daily?

I need to get 8 clock repeating notification every day. I have fixed some coding for it and I see an error. Below are some picks. How do I set an 8 clock alarm AlarmActivity public

Solution 1:

Just change the code of init alarm and update alarm

This will trigger alarm everyday at 8 AM

in initAlarm()

privatevoidinitAlarm() {

    //creating intent
    Intent intent = new Intent(id);
    boolean alarmRunning = PendingIntent.getBroadcast(
            this,
            REQUEST_CODE,
            intent,
            PendingIntent.FLAG_NO_CREATE
    ) != null;

    //setting broadcast
    broadcastReceiver = new MyReceiver();
    registerReceiver(
            broadcastReceiver,
            new IntentFilter(id)
    );

    //setting alarm
    pendingIntent = PendingIntent.getBroadcast(
            this,
            REQUEST_CODE,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
    );

    //Check if alarm is already runningif (!alarmRunning) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        long startUpTime  = calendar.getTimeInMillis();

        alarmManager.setRepeating(
                AlarmManager.RTC_WAKEUP,
                startUpTime,
                AlarmManager.INTERVAL_DAY,
                pendingIntent
        );
    } else {
        updateAlarm();
        Log.e("Alarm", "Alarm already running.!");
    }
}

in updateAlarm()

privatevoidupdateAlarm() {

    //calculating alarm time and creating pending intent
    Intent intent = new Intent(id);
    pendingIntent = PendingIntent.getBroadcast(
            this,
            REQUEST_CODE,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
    );

    //removing previously running alarm
    alarmManager.cancel(pendingIntent);
    unregisterReceiver(broadcastReceiver);

    //setting broadcast
    broadcastReceiver = new MyReceiver();
    registerReceiver(
            broadcastReceiver,
            new IntentFilter(id)
    );

    //Check if alarm is already running
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 8);
    calendar.set(Calendar.MINUTE, 0);
    long startUpTime  = calendar.getTimeInMillis();

    alarmManager.setRepeating(
            AlarmManager.RTC_WAKEUP,
            startUpTime,
            AlarmManager.INTERVAL_DAY,
            pendingIntent
    );

    Log.e("Alarm", "Alarm updated..!");

}

Post a Comment for "How Do I Place A Repeating Notification 8 Clock Set Daily?"