1

我正在尝试发出通知,无论安装了应用程序,它都会每隔一个月自动设置一次警报/通知,并在单击该通知时打开应用程序的特定活动

这是我的警报管理器代码

 // 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 中有它。我如何设置它每月重复一次

4

1 回答 1

2

Schedule a repeating alarm can be achieved by setRepeating() of AlarmManager()

public void setRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)

希望您可以通过检查哪个月份并通过设置标志来修改INTERVAL_DAY *28Or INTERVAL_DAY*30OR来实现您的目标。INTERVAL_DAY*31

 am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY*30, sender);
于 2013-01-04T17:57:48.563 回答