java - 在android中重复本地通知

标签 java android eclipse

我正在尝试发出通知,无论何时安装应用程序,它都会在每个月的第一天自动设置警报/通知,并在单击该通知后打开应用程序的特定 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 Or INTERVAL_DAY*30 OR INTERVAL_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/

相关文章:

Android:用mediaplayer播放本地视频

java - 修改函数无法打印预期结果

java - 无法使用散列密码登录 Android 应用程序

java - 使用 Servlet 读取 Jar 内的 list 文件

java - 创建嵌入 IF 逻辑的文件

java - 无法找到或加载主程序

Android - 听一个禁用的按钮

java - 想在java中发送十六进制字符串数据

java - Eclipse 命令行参数

java - Eclipse 的 Portlet 向导