Android 如何设置每月重复报警
目前我正在开发一个应用程序,每月设置提醒。我无法为alarmmanager提供正确的重复间隔。请提供相关信息。 这是我的代码,但在2月或30天内不会发出警报。 此外,请提供代码,以设置每年重复alaramAndroid 如何设置每月重复报警,android,alarmmanager,Android,Alarmmanager,目前我正在开发一个应用程序,每月设置提醒。我无法为alarmmanager提供正确的重复间隔。请提供相关信息。 这是我的代码,但在2月或30天内不会发出警报。 此外,请提供代码,以设置每年重复alaram repeatTime=(AlarmManager.INTERVAL_DAY*31); mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, when.getTimeInMillis(), repeatTime, pi)
repeatTime=(AlarmManager.INTERVAL_DAY*31);
mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, when.getTimeInMillis(), repeatTime, pi);
谢谢,
Sharath这是如何计算两天之间的时间间隔的。一个月后,每次触发警报时,使用此逻辑重置警报。i、 e将报警设置为您想要启动的时间点,提供一些待定的意图,一旦报警触发,使用下面的代码获取下一个触发时间,然后再次将报警设置为在该时间触发
private long getDuration(){
// get todays date
Calendar cal = Calendar.getInstance();
// get current month
int currentMonth = cal.get(Calendar.MONTH);
// move month ahead
currentMonth++;
// check if has not exceeded threshold of december
if(currentMonth > Calendar.DECEMBER){
// alright, reset month to jan and forward year by 1 e.g fro 2013 to 2014
currentMonth = Calendar.JANUARY;
// Move year ahead as well
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR)+1);
}
// reset calendar to next month
cal.set(Calendar.MONTH, currentMonth);
// get the maximum possible days in this month
int maximumDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
// set the calendar to maximum day (e.g in case of fEB 28th, or leap 29th)
cal.set(Calendar.DAY_OF_MONTH, maximumDay);
long thenTime = cal.getTimeInMillis(); // this is time one month ahead
return (thenTime); // this is what you set as trigger point time i.e one month after
}
用于年度重复报警
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
if(cal.isLeapYear(year)){
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 366, alarmIntent);
}else{
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 365, alarmIntent);
}
谢谢你。。例如:dateValue=“30/01/2017 11:02”。。。从数据库中获取id和日期
private void getMonthlyDuration(Context context,int id,String dateValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences("ModernDiary", Context.MODE_PRIVATE);
String dateOfMonth = sharedPreferences.getString("day"+id,DateFormat.format("dd", Calendar.getInstance()).toString());
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.getDefault());
try {
Date dateMain = simpleDateFormat.parse(dateValue);
calendar.setTime(dateMain);
} catch (ParseException e) {
e.printStackTrace();
}
Boolean isSetDate = false;
if (sharedPreferences.getInt("monthInc"+id,-1) != -1) {
calendar.set(Calendar.MONTH,sharedPreferences.getInt("monthInc"+id,calendar.get(Calendar.MONTH)));
calendar.set(Calendar.YEAR,sharedPreferences.getInt("yearInc"+id,calendar.get(Calendar.YEAR)));
if (calendar.getActualMaximum(Calendar.DAY_OF_MONTH) < Integer.parseInt(dateOfMonth)) {
calendar.set(Calendar.DATE,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Log.i("timeDay",dateOfMonth+" "+calendar.getTime()+" max");
} else {
calendar.set(Calendar.DATE,Integer.parseInt(dateOfMonth));
Log.i("timeDay",dateOfMonth+" "+calendar.getTime()+"min");
}
if (sharedPreferences.getInt("monthInc"+id,calendar.get(Calendar.MONTH)) < calendar.get(Calendar.MONTH)){
calendar.add(Calendar.MONTH, -1);
isSetDate = true;
Log.i("timeMonth","Increment "+calendar.getTime());
} else {
isSetDate = false;
Log.i("timeMonth","No Change");
}
}
calendar.add(Calendar.MONTH, 1);
if (isSetDate){
if (calendar.getActualMaximum(Calendar.DAY_OF_MONTH) < Integer.parseInt(dateOfMonth)) {
calendar.set(Calendar.DATE,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
} else {
calendar.set(Calendar.DATE,Integer.parseInt(dateOfMonth));
}
}
Log.i("timeAf",calendar.getTime()+"");
sharedPreferences.edit().putInt("monthInc"+id, calendar.get(Calendar.MONTH)).apply();
sharedPreferences.edit().putInt("yearInc"+id, calendar.get(Calendar.YEAR)).apply();
Intent notificationIntent = new Intent(context,AlarmBroadcastReceiver.class);
Bundle bundle = new Bundle();
bundle.putSerializable("alarm", id);
notificationIntent.putExtra("bundle", bundle);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
broadcast = PendingIntent.getBroadcast(context, id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);
}
private void getMonthlyDuration(上下文上下文、int-id、字符串dateValue){
SharedReferences SharedReferences=context.getSharedReferences(“ModernDiary”,context.MODE\u PRIVATE);
String dateOfMonth=SharedReferences.getString(“day”+id,DateFormat.format(“dd”,Calendar.getInstance()).toString());
日历=Calendar.getInstance();
SimpleDataFormat SimpleDataFormat=新的SimpleDataFormat(“dd/MM/yyyy HH:MM”,Locale.getDefault());
试一试{
Date dateMain=simpleDateFormat.parse(日期值);
日历设置时间(dateMain);
}捕获(解析异常){
e、 printStackTrace();
}
布尔值isSetDate=false;
if(sharedPreferences.getInt(“monthInc”+id,-1)!=-1){
calendar.set(calendar.MONTH,sharedPreferences.getInt(“monthInc”+id,calendar.get(calendar.MONTH)));
calendar.set(calendar.YEAR,sharedPreferences.getInt(“yearInc”+id,calendar.get(calendar.YEAR));
if(calendar.getActualMaximum(calendar.DAY\u OF_MONTH)
报警触发时间:
2017年2月28日星期二11:02:00 GMT+05:30
2017年3月30日星期四11:02:00 GMT+05:30
2017年4月30日星期日格林尼治标准时间11:02:00+05:30
2017年5月30日星期二11:02:00 GMT+05:30
2017年6月30日星期五11:02:00 GMT+05:30
2017年7月30日星期日11:02:00 GMT+05:30
2017年8月30日星期三11:02:00 GMT+05:30
2017年9月30日星期六11:02:00 GMT+05:30
2017年10月30日星期一11:02:00 GMT+05:30
2017年11月30日星期四11:02:00 GMT+05:30
2017年12月30日星期六11:02:00 GMT+05:30
2018年1月30日星期二11:02:00 GMT+05:30
星期三2月28日11:02:00 GMT+2018年5:30…
设置每月重复的警报
Calendar calender= Calendar.getInstance(TimeZone.getDefault());
int cDay = calender.get(Calendar.DAY_OF_MONTH);
calender.set(Calendar.HOUR_OF_DAY, hour); //hour you have selected
calender.set(Calendar.MINUTE, min); //min you have selected
calender.set(Calendar.SECOND, 0);
calender.set(Calendar.MILLISECOND, 0);
calender.set(Calendar.DATE, cDay);
calender.get(Calendar.MONTH);
Calendar now = Calendar.getInstance();
now.set(Calendar.SECOND, 0);
now.set(Calendar.MILLISECOND, 0);
int days = now.getActualMaximum(Calendar.DAY_OF_MONTH);
if (calender.before(now)) { //this condition is used for future alarm only
calender.add(Calendar.DATE, days);
}
final int _id = (int) System.currentTimeMillis();
Intent i = new Intent(activity, YourServiceClass.class);
i.putExtra("type", "month");
PendingIntent displayIntent = PendingIntent.getBroadcast(
activity, _id, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), AlarmManager.INTERVAL_DAY * calender.getActualMaximum(Calendar.DAY_OF_MONTH), displayIntent);
现在在您的服务类中,输入下面的代码
if (intent.getExtras() != null) {
type = intent.getStringExtra("type");
}
if (type != null) {
if (type.equals("month")) {
Long futureTimeDifference = intent.getLongExtra("futureTimeDifference", 0); // Receive the time difference in milliseconds from currenttime in milliseconds and the future set date milliseconds
futureTimeDifference = futureTimeDifference + System.currentTimeMillis();// get the next schedule date time inmilliseconds
String repeatType = intent.getStringExtra("getRepeatType");// Receive the repeat type
Date todaysDate = new Date();// initialize a new date object
Calendar getCurrentDate = Calendar.getInstance();// Initialize a new Calendar object
getCurrentDate.setTime(todaysDate); //Set the calendar to todays date
int currentMonth = getCurrentDate.get(Calendar.MONTH); // Assign the current month in integer
if (currentMonth == Calendar.JANUARY || currentMonth == Calendar.MARCH || currentMonth == Calendar.MAY || currentMonth == Calendar.JULY || currentMonth == Calendar.AUGUST || currentMonth == Calendar.OCTOBER || currentMonth == Calendar.DECEMBER) {
futureTimeDifference = System.currentTimeMillis() + (AlarmManager.INTERVAL_DAY * 31);
}
if (currentMonth == Calendar.APRIL || currentMonth == Calendar.JUNE || currentMonth == Calendar.SEPTEMBER || currentMonth == Calendar.NOVEMBER) {
futureTimeDifference = System.currentTimeMillis() + (AlarmManager.INTERVAL_DAY * 30);
}
if (currentMonth == Calendar.FEBRUARY) {//for february month)
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
if (cal.isLeapYear(cal.get(Calendar.YEAR))) {//for leap year february month
futureTimeDifference = System.currentTimeMillis() + (AlarmManager.INTERVAL_DAY * 29);
} else { //for non leap year february month
futureTimeDifference = System.currentTimeMillis() + (AlarmManager.INTERVAL_DAY * 28);
}
}
final int monthly_id = (int) System.currentTimeMillis();
Log.e("MonthlyNotification", futureTimeDifference + "");
PendingIntent displayIntent = PendingIntent.getBroadcast(
context, monthly_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, futureTimeDifference, displayIntent);
//Toast.makeText(context, "Notification Set Monthly", Toast.LENGTH_SHORT).show();
}
}
您可以使用
设置
,然后在广播接收器中重置警报
在接收器中,只需:
val c=Calendar.getInstance().apply{add(Calendar.MONTH,1)}
//在此重置警报
val intent=intent(上下文,AlarmReceiver::class.java)
val pendingIntent=pendingIntent.getBroadcast(
上下文,123,意图,0
)
val alarmManager=context.getSystemService(context.ALARM\u服务)是否作为alarmManager?
alarmManager!!。设置(
AlarmManager.RTC,
c、 时间单位为毫秒,
悬垂物
)
您好,感谢您的回复,我通过将日期改为下个月进行了测试,它正常工作。每当我们重新启动模拟器时,我还有一个问题,它会为所有丢失的alaram提高alaram。您能告诉我如何实施如何仅为未募集的ALARAM募集资金吗?谢谢,Sharathyou可以提供一些意向性数据,例如触发器的时间戳,一旦您的警报发出,您将收到相同的意向性回复,使用此功能,您可以编写逻辑来检查之前的alram是否因比较时间而丢失。目前,我正在将提醒存储在db中。但不知道如何避免产生重复的Alaram,请提供任何代码片段来实现这一点。谢谢,莎拉,你可以在这里发布你的代码,不管你尝试了什么。很抱歉,在提供任何帮助之前,我需要知道您讲话的上下文。我创建了一个用于设置提醒的应用程序。每当用户设置提醒时,它都会将其存储在db中。一旦设备重新启动,它将正确地忘记提醒。所以我创造了一个