简体   繁体   English

我想反复设置警报,例如在Android中按年,按月,按周设置警报,我该怎么办?

[英]I want to set an alarm repeatedly, like yearly, monthly, weekly in android, how can i do it?

I'm able to set a repeating alarm on the same day, but when I try to do the same for yearly or monthly (like on the same day of every month alarm should set), it's not working. 我可以在同一天设置重复警报,但是当我尝试每年或每月进行一次重复警报(例如应该在每个月的同一天设置警报)时,它就无法工作。

Following is the code i have tried for the same day 以下是我当天尝试过的代码

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, present_calender.getTimeInMillis()+intveral,intveral, pendingIntent);

I tried to search the same thing in stack overflow, but I couldn't find the exact solution so that's the reason I'm posting this question again. 我试图在堆栈溢出中搜索相同的内容,但是我找不到确切的解决方案,所以这就是我再次发布此问题的原因。

Can anyone suggest me the solution (if possible, please, share some sample code) 谁能给我建议解决方案(如果可能,请分享一些示例代码)

Thanks in advance :-) 提前致谢 :-)

Try this code 试试这个代码

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this.getApplicationContext(),
                your_destined_class.class);
intent.setAction(your_action_name);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
        Calendar timeOff = Calendar.getInstance();
        timeOff.add(Calendar.MILLISECOND,
        **your_duration_in_ms**);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
        timeOff.getTimeInMillis(),
        **your_duration_in_ms**,
        pendingIntent);

and set your_duration_in_ms to daily(24*60*60*1000) or monthly(30*24*60*60*1000) and so on 并将your_duration_in_ms设置为每天(24 * 60 * 60 * 1000)或每月(30 * 24 * 60 * 60 * 1000),依此类推

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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