【问题标题】:Alarm manager is not working as expected警报管理器未按预期工作
【发布时间】:2017-08-18 11:33:18
【问题描述】:

基本上,我想创建两个警报:

  1. 每天下午 6 点开火
  2. 每月在特定日期下午 4 点触发。

但问题是在执行每月警报后,第一个也在下午 4 点执行。

按照我创建警报的方式:

Calendar calendar = Calendar.getInstance();
Calendar calendar1 = Calendar.getInstance();

// For 1st alarm
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

// For 2nd alarm
calendar1.set(2017,8,17,16,00,00);

Bundle bundle = new Bundle();
Intent intent = new Intent(context, AlarmReceiver.class);
bundle.putInt("NotificationId1", 1);
bundle.putInt("NotificationId2", 2);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1,
                               intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 2,
                               intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                    AlarmManager.INTERVAL_DAY, pendingIntent);
alarm.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(),  pendingIntent1);

在发出每月警报后,我再次用更远的日期定义警报。

问题是在触发每月警报后,第一个警报在下午 4 点执行。如何解决问题?

我们将不胜感激。

【问题讨论】:

  • 创建Alarm类的2个对象

标签: android android-alarms


【解决方案1】:

你的第一个闹钟正在重复,所以创建这样的闹钟来设置闹钟。

if(Build.VERSION.SDK_INT < 23){
    if(Build.VERSION.SDK_INT >= 19){
        setExact(...);
    }
    else{
        set(...);
    }
}
else{
    setExactAndAllowWhileIdle(...);
}

现在在您的广播接收器中设置下一个闹钟在第二天下午 4 点触发

【讨论】:

    【解决方案2】:

    试试这个代码,希望你能得到答案。

    Bundle bundle1 = new Bundle();
    Intent intent1 = new Intent(context, AlarmReceiver.class);
    bundle1.putInt("NotificationId1", 1);
    intent1.putExtras(bundle1);
    
    Bundle bundle2 = new Bundle();
    Intent intent2 = new Intent(context, AlarmReceiver.class);
    bundle2.putInt("NotificationId2", 2);
    intent2.putExtras(bundle2);
    
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1,
                                   intent2, PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 2,
                                   intent2, PendingIntent.FLAG_UPDATE_CURRENT);
    

    【讨论】:

      【解决方案3】:

      使您的两个警报彼此分开。为此,您需要 2 个意图、2 个待处理意图和 2 个警报管理器。您可以参考 - this link

      你的代码应该是这样的——

      // define variables
      private AlarmManager alarmMgr1;
      private AlarmManager alarmMgr2;
      private PendingIntent pendingIntent1;
      private PendingIntent pendingIntent2;
      private Calendar timeFirst;
      private Calendar timeSecond;
      private Context context;
      
      // init variables
      timeFirst = Calendar.getInstance();
      timeSecond = Calendar.getInstance();
      timeFirst.set(Calendar.HOUR_OF_DAY, 18);
      timeFirst.set(Calendar.MINUTE, 0);
      timeFirst.set(Calendar.SECOND,0);
      timeFirst.set(Calendar.MILLISECOND,0);
      
      timeSecond.set(2017,8,17,16,00,00);
      
      // set values
      alarmMgr1 = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
      alarmMgr2 = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
      Intent myIntent1 = new Intent(context, AlarmReceiver.class);
      pendingIntent1 = PendingIntent.getBroadcast(context, (int) System.currentTimeMillis(), myIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
      Intent myIntent2 = new Intent(context, AlarmReceiver.class);
      pendingIntent2 = PendingIntent.getBroadcast(context, (int) System.currentTimeMillis(), myIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
      
      alarmMgr1.setRepeating(AlarmManager.RTC_WAKEUP, timeFirst.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent1);
      
      if (Build.VERSION.SDK_INT >= 23){
          alarmMgr2.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeSecond.getTimeInMillis(), pendingIntent2);
      }
      else if (Build.VERSION.SDK_INT >= 19){
              alarmMgr2.setExact(AlarmManager.RTC_WAKEUP, timeSecond.getTimeInMillis(), pendingIntent2);
      }
      else{
          alarmMgr2.set(AlarmManager.RTC_WAKEUP, timeSecond.getTimeInMillis(), pendingIntent2);
      }
      
      }
      

      希望它会有所帮助并给您一个更好的主意。

      【讨论】:

        【解决方案4】:

        您需要 2 个意图。 因为您共享相同的意图,所以任何正在运行的警报也会调用相同的意图。

        我希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 2020-09-10
          • 1970-01-01
          • 2017-05-13
          • 1970-01-01
          • 1970-01-01
          • 2012-01-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多