Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a repeating alarm in android

I am trying to set a repeating alarm in android that eventually will go up at a user specified time. However the alarm goes off right away when once it is set, even when I make sure the alarm isn't set to go off until after the alarm has been set. For example, I have the code below set to have an alarm go off at 10:43 so I set the alarm at 10:41, but the alarm goes off right away. Any ideas? Thanks in advance.

    public class Alarm extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        PowerManager pm = (PowerManager) context
                .getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(
                PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();


        Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For

        Intent scheduledIntent = new Intent(context,ReminderMessage.class);
        scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(scheduledIntent);
        // example

        wl.release();
    }

    public void SetAlarm(Context context) {
        AlarmManager am = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);

        Date dat = new Date();
        Calendar cal_alarm = Calendar.getInstance();
        Calendar cal_now = Calendar.getInstance();
        cal_now.setTime(dat);
        cal_alarm.setTime(dat);
        cal_alarm.set(Calendar.HOUR_OF_DAY, 10);
        cal_alarm.set(Calendar.MINUTE, 43);
        cal_alarm.set(Calendar.SECOND, 0);

        if(cal_alarm.before(cal_now)){
            cal_alarm.add(Calendar.DATE, 1);
        }


        Intent i = new Intent(context, Alarm.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), cal_alarm.getTimeInMillis() , pi); // Millisec * Second * Minute
    }

    public void CancelAlarm(Context context) {
        Intent intent = new Intent(context, Alarm.class);
        PendingIntent sender = PendingIntent
                .getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }
}
like image 903
johns4ta Avatar asked Oct 18 '12 14:10

johns4ta


1 Answers

I think your line:

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), cal_alarm.getTimeInMillis() , pi); // Millisec * Second * Minute

is triggering the alarm immediately, the second param is the scheduled time, and the third is the period. So if you wanted your alarm to go off at cal_alarm time, you want to use something like:

am.setRepeating(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), 1000*60*5 , pi); // Millisec * Second * Minute

That should go off at the cal_alarm time, and repeat every 5 mins.

AlarmManager.SetRepeating API Doc

like image 166
SimpsOff Avatar answered Sep 21 '22 07:09

SimpsOff