0

I have the following class I use to set up an alarm at a certain time each day:

public class MainActivity extends Activity {
    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;
    BroadcastReceiver br;
    TextView t; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setup(); 
        t = (TextView)findViewById(R.id.textView1);
        // Set the alarm to start at approximately 2:00 p.m.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 18);
        calendar.set(Calendar.MINUTE, 10); // Particular minute
        calendar.set(Calendar.SECOND, 0);
         alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
         alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                    1000*60*60*24, alarmIntent);
    }


    public void setup() {
        br = new BroadcastReceiver() {
            @Override
            public void onReceive(Context c, Intent i) {
                Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
                //Invoke the service here Put the wake lock and initiate bind service
                t.setText("Hello Alarm set");
            }
        };
        registerReceiver(br, new IntentFilter("com.testrtc") );
        alarmIntent = PendingIntent.getBroadcast( this, 0, new Intent("com.testrtc"),
                0 );
        alarmMgr = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
    }
   }

The above works fine, however to set the alarm again after reboot I use the following:

public class SampleBootReceiver extends BroadcastReceiver {
    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;
    BroadcastReceiver br;
    TextView t; 

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Toast.makeText(context, "Hello from Bootloader", 10000).show();
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 18);
            calendar.set(Calendar.MINUTE, 10); // Particular minute
            calendar.set(Calendar.SECOND, 0);
             alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
             alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                        1000*60*60*24, alarmIntent);


        }
    }
}

I get the toast, but the alarm is not reset. Otherwise I would have gotten the original toast message (setup in the onRecieve of MainActivity) , am I doing it correctly or theres more to it?

Avijit
  • 3,834
  • 4
  • 33
  • 45
User3
  • 2,465
  • 8
  • 41
  • 84
  • always cancel the alarm before resetting alarm..Though I guess you don't have to reset alarm after reboot.. – Anirudha Jan 20 '14 at 13:03
  • But cancelling an alarm on reboot? Ill have to listen to something which occurs before reboot. – User3 Jan 21 '14 at 04:17

1 Answers1

1

Well your code is 100% correct but the problem is at following line,

calendar.set(Calendar.HOUR_OF_DAY, 18);

This will allow to execute Alarm on next execution. That means tomorrow's 18:00 PM. Instead if you use this,

calendar.set(Calendar.HOUR, 18);

Then it will execute at today's 18:00 pm.

Vigbyor
  • 2,568
  • 4
  • 21
  • 35
  • HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22. HOUR is used for the 12-hour clock. E.g., at 10:04:15.250 PM the HOUR is 10. – Phantômaxx Jan 20 '14 at 13:55
  • @Tobor, yes you are right, its my mistake, I just confused it with this one http://stackoverflow.com/questions/4562757/alarmmanager-android-every-day#comment25914527_6841929 – Vigbyor Jan 21 '14 at 03:26