java - android 中每个月的本地通知

标签 java android eclipse

我正在使用以下代码

AlarmManager service = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, AlarmReceiver.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pending = PendingIntent.getBroadcast(this, 0, i,PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// Start 30 seconds after boot completed
cal.add(Calendar.SECOND, 30);
//
// Fetch every 30 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP ,cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY , pending);

// service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
// REPEAT_TIME, pending);

在我的代码中,我希望它在一天的间隔后工作并为 AlarmReceiver 服务广播。

但是我希望这发生在整整一个月之后。例如,如果一个人在 1 月 3 日安装应用程序,下一个警报将在 2 月 3 日发生,依此类推。我如何设置一个月的间隔。

最佳答案

请使用以下代码。它与您当前的代码没有太大区别。

AlarmManager service = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(this, AlarmReceiver.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pending = PendingIntent.getBroadcast(this, 0, i,PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar cal = Calendar.getInstance();
    // Start 1 month after boot completed
    cal.add(Calendar.MONTH, 1);
    //
    // Fetch every 1 month
    // InexactRepeating allows Android to optimize the energy consumption
    service.setInexactRepeating(AlarmManager.RTC_WAKEUP ,cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY , pending);

关于java - android 中每个月的本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14211919/

相关文章:

java - 如何使用 ChromeDriver 在 Chrome 中执行 Selenide

java - DataSourceUtils.getConnection 与 DataSource.getConnection 对比

java - 在 Map 中实现按键或 strip 锁定 - 最佳方法?

java - IntelliJ IDEA 和 JRebel 不重新加载类

java - 在 Java 中对一系列图像进行变形的困难

android - 在android TextView中右对齐文本

android - 在 Realm 中,如何从 RealmList (Android) 查询

android - 如何在应用结算中获取来自 Google 的所有购买?

eclipse - 如何跟踪 Eclipse 更新站点的下载?

初始化 Java 工具时出现 Eclipse 内部错误