Search code examples
javaandroideclipsealarmmanagertimepicker

making alarm manager repeat daily with time picker


i'm trying to make my alarm manager repeat daily at the time chosen by the timepicker, i'm still new to java coding and stuff and i have no idea what to write inside the setRepeating() and in one alarm manager example i found this code

alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

i tried changing it but i get an error Description Resource Path Location Type The method setRepeating(int, long, long, PendingIntent) in the type AlarmManager is not applicable for the arguments (int, long, PendingIntent)

help?

public class AndroidTimeActivity extends Activity {

    TimePicker myTimePicker;
    Button buttonstartSetDialog;
    Button buttonCancelAlarm;
    TextView textAlarmPrompt;

    TimePicker timePicker;
    CheckBox optRepeat;

    final static int RQS_1 = 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        timePicker = (TimePicker)findViewById(R.id.picker);
        optRepeat = (CheckBox)findViewById(R.id.optrepeat);
        textAlarmPrompt = (TextView)findViewById(R.id.alarmprompt);

        buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
        buttonstartSetDialog.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                Calendar calNow = Calendar.getInstance();
                Calendar calSet = (Calendar) calNow.clone();

                calSet.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
                calSet.set(Calendar.MINUTE, timePicker.getCurrentMinute());
                calSet.set(Calendar.SECOND, 0);
                calSet.set(Calendar.MILLISECOND, 0);

                if(calSet.compareTo(calNow) <= 0){
                    //Today Set time passed, count to tomorrow
                    calSet.add(Calendar.DATE, 1);
                }

                setAlarm(calSet, optRepeat.isChecked());

            }});

        buttonCancelAlarm = (Button)findViewById(R.id.cancel);
        buttonCancelAlarm.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                cancelAlarm();
            }});

    }

    private void setAlarm(Calendar targetCal, boolean repeat){

        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

        if(repeat){
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
                    targetCal.getTimeInMillis(), 
                    AlarmManager.INTERVAL_DAY,
                    pendingIntent);

            textAlarmPrompt.setText(
                    "\n\n***\n"
                    + "Alarm is set@ " + targetCal.getTime() + "\n"
                    + "Repeat\n"
                    + "***\n");
        }else{
            alarmManager.set(AlarmManager.RTC_WAKEUP, 
                    targetCal.getTimeInMillis(),  
                    pendingIntent);

            textAlarmPrompt.setText(
                    "\n\n***\n"
                    + "Alarm is set@ " + targetCal.getTime() + "\n"
                    + "One shot\n"
                    + "***\n");
        }

    }

    private void cancelAlarm(){

        textAlarmPrompt.setText(
                "\n\n***\n"
                + "Alarm Cancelled! \n"
                + "***\n");

        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);

    }

}

Solution

  • Try this:

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    

    What you're missing in setRepeating is that there's 4 parameters you need to set. The missing one is the interval parameter (the 3rd one). AlarmManager has constants declared for intervals such as daily, half a day, and more.

    If you want it repeating make sure you don't use alarmManager.set()' and usealarmManager.setRepeating`.

    Additionally, if you want to give the user the option to have it repeat or not, you can make a simple bool check. If repeating, use .setRepeating, else use .set.

    Edit: Not having used .setRepeating() myself, I'm not sure if it works how you want with the .INTERVAL_DAY constant. You may have to use .setInexactRepeating() instead. The parameters would look exactly the same:

    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    

    AlarmManager | Android