Click here to Skip to main content
13,836,240 members
Rate this:
Please Sign up or sign in to vote.
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.

public class AlarmReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Date currentTime = new Date(); 
        String ScheduledTime = ""; 
        if (null != intent) { //Null Checking
            ScheduledTime =   intent.getStringExtra("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.
Updated 17-Sep-18 5:18am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntentFlags.UpdateCurrent);

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190114.1 | Last Updated 17 Sep 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100