Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the below class to check if my alarms are being triggered at the exact time which I scheduled or is it differing.

I will call SetAlarm method inside the BroadCast Reciever from MainActivity. The consecutive alarms will be set by the Receiver itself by setting current time as its new string extra.

Alarms are working fine except the issue Intent string extras are not getting updated. ScheduledTime will always hold the initial value regardless of what I have set it in the setAlarm method.

Java
public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Date currentTime = new Date(); 
        String ScheduledTime = ""; 
        if (null != intent) { //Null Checking
            ScheduledTime =   intent.getStringExtra("ScheduledTime");
            //intent.removeExtra("ScheduledTime");
        }
        String message  = "Current Time" + currentTime + ",  Scheduled Time was: " + ScheduledTime ; 
        //Show Notification 

        long alarmMillis =(10*60*1000) ; //Set Alarm after 10 minutes
        Long newTimeInMillis = System.currentTimeMillis() +  alarmMillis;
        currentTime.setTime(newTimeInMillis );
        setAlarm(context, newTimeInMillis , currentTime.toString());
    }

     public void setAlarm(Context context, Long timeMillis, String ScheduledTime)
    {
        AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, AlarmReceiver.class);
        i.putExtra("ScheduledTime", ScheduledTime);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        am.setExact(AlarmManager.RTC_WAKEUP, timeMillis, pi);
    }
}


What I have tried:

I have tried removing Extra from the Broadcast Receiver (which I commented in the code block). Still not working.
Posted
Updated 17-Sep-18 4:18am

1 solution

Try changing:
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

To
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntentFlags.UpdateCurrent);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900