Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

alarmmanager - Repeat Alarm once in a week in android

I am trying to develop alarm functionality in a my app which runs on a week days specified by user on fixed time. Problem here is that my scheduler running for all days instead of running on specified day . here is the code i wrote for this please help to fix this

Calendar calNow = Calendar.getInstance();
                SimpleDateFormat simpDate;
                simpDate = new SimpleDateFormat("kk:mm:ss");
                if(in_Date==1)
                {
                    calNow.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    calNow.set(Calendar.MINUTE, minute);
                    calNow.set(Calendar.SECOND, 0);
                    calNow.set(Calendar.MILLISECOND, 0);
                }
            else if(in_Date==2)
                {
                    calNow.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    calNow.set(Calendar.MINUTE, minute);
                    calNow.set(Calendar.SECOND, 0);
                    calNow.set(Calendar.MILLISECOND, 0);
                    calNow.set(Calendar.DAY_OF_WEEK,in_SelectedDay);
                }
                etTime.setText(simpDate.format(calNow.getTime()));
                Seconds=calNow.getTimeInMillis();

private void setAlarm(){

    //etTime.setText(simpDate.format(calNow.getTime()));

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

     if(in_Date==1)
     {

       alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Seconds,alarmManager.INTERVAL_DAY,pendingIntent);
     }
    else if(in_Date==2)
    {

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Seconds,1 * 60 * 60 * 1000,pendingIntent);
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Seconds, AlarmManager.INTERVAL_DAY, pendingIntent);

In this line you set the start time to the user selected day, but then set the interval to INTERVAL_DAY.

You should use INTERVAL_DAY * 7 to make sure it repeats on a weekly basis instead:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Seconds, AlarmManager.INTERVAL_DAY * 7, pendingIntent);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...