简体   繁体   中英

Set repeating alarm

i want to set alarm to repeat it in a particular day .for example every monday at 4:20 , I have created a method but not working .please help me to solve this problem

public void setAlarm(int day,int hour,int minit){

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, hour);
    cal.set(Calendar.MINUTE,minit );
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    cal.set(Calendar.DAY_OF_WEEK, day);


    Intent intent = new Intent(this, MyBroadcastReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Long alarmTime = cal.getTimeInMillis();
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 24 * 60 * 60 * 1000 *7, pendingIntent);

}

now if i choose monday 2:40 its only alarming on next monday 2:40 not working on every monday 2:40

您可以尝试使用setInexactRepeating代替。API 19 开始,所有重复警报都是不精确的。详细可以参考官方文档

使用对我有用的 setInexactRepeating 函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM