我正在尝试发出通知,无论何时安装应用程序,它都会在每个月的第一天自动设置警报/通知,并在单击该通知后打开应用程序的特定 Activity
这是我的警报管理器代码
// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.MINUTE, 5);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra(MESSAGE);
PendingIntent sender = PendingIntent.getBroadcast(this, 10, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
我做了一个闹钟类
@Override
public void onReceive(Context context, Intent intent)
{
try {
Intent newIntent = new Intent(context, Appointment.class);
context.startActivity(newIntent);
}
catch (Exception e)
{
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
我的问题是,我想将其设置为重复通知,每月 1 日。警报代码,我在我的 mainActivity 中有它。我怎样才能设置它每个月重复一次
最佳答案
可以通过AlarmManager()的setRepeating()实现定时重复闹钟
public void setRepeating(int类型,long triggerAtMillis,long intervalMillis,PendingIntent操作)
Hope you can achieve your goal by checking which month is and modify the
INTERVAL_DAY *28
OrINTERVAL_DAY*30
ORINTERVAL_DAY*31
by setting flags and all that.
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY*30, sender);
关于java - 在android中重复本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14161945/