Search code examples
androidalarmmanagertimepicker

Android : How to open an activity with an alarm manager?


I'm making an android app. The aim is to ring at a scheduled time and open an activity. I used several link from stackoverflow and other website about doing this but I got a problem : when I change the TimePicker's time, instead of setting the alarm, the alarm is set and the activity is open.

I'd like to not open the activity when I change the time but only enable the alarm.

Here is my code :

Activity with the TimePicker :

tp_clock.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

                int alarmHours = tp_clock.getCurrentHour();
                int alarmMinutes = tp_clock.getCurrentMinute();
                Long time = calculateTime(alarmHours, alarmMinutes);

                Toast.makeText(context, "Alarm set", Toast.LENGTH_LONG).show();
                alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(context, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
            }
        });    

calculateTime function :

public Long calculateTime(int alarmHours, int alarmMinutes)
    {
        Calendar c = Calendar.getInstance();

        int currentHours = c.get(Calendar.HOUR);
        if (c.get(Calendar.AM_PM) == 1)
            currentHours +=12;
        int currentMinutes = c.get(Calendar.MINUTE);

        int setAlarmHours = 0;
        int setAlarmMinutes = 0;
        int balanceWithMinute = 0;

        if (alarmMinutes >= currentMinutes)
        {
            setAlarmMinutes = alarmMinutes - currentMinutes;
        }
        else
        {
            setAlarmMinutes = (60 - currentMinutes) + alarmMinutes;
            balanceWithMinute = 1;
        }

        if (alarmHours > currentHours)
            setAlarmHours = alarmHours - currentHours;

        if (alarmHours < currentHours)
            setAlarmHours = (24 - currentHours) +alarmHours - balanceWithMinute;

        if (alarmHours == currentHours && balanceWithMinute == 1)
            setAlarmHours = 23;

        Long time = new GregorianCalendar().getTimeInMillis()+setAlarmHours*setAlarmMinutes*60*1000;
        return time;
    }

alarmReciever code :

public class AlarmReciever extends BroadcastReceiver
{
    private String userToken;
    private String userId;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent i=new Intent(context, PlanASoundActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        userToken = intent.getStringExtra(LoginActivity.USER_TOKEN);
        userId = intent.getStringExtra(LoginActivity.USER_ID);

        i.putExtra(LoginActivity.USER_TOKEN, userToken);
        i.putExtra(LoginActivity.USER_ID, userId);
        context.startActivity(i);

        Toast.makeText(context, "Alarm triggered", Toast.LENGTH_LONG).show();

    }

This function is the only one I defined in the AlarmReciever class.


Solution

  • You're making the call to

    alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(context, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
    

    inside the onTimeChanged listener. That's why it's launched everytime you change your TimePicker time.

    You'd better add a button in your view, and implement the onClickListener where you'll get the Time selected from the TimePicker and make the call there.