检查是否设置了警报管理器时出现误报

时间:2016-07-05 14:08:49

标签: android alarmmanager android-alarms

我有两个警报1.步行提醒2.提示通知 用户可以取消步行提醒。取消过程正常。

private void setWalkReminder(){
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 18);
    calendar.set(Calendar.MINUTE, 50);
    calendar.set(Calendar.SECOND, 0);
    Intent walkIntent = new Intent(this, AlarmReceiver.class);
    Log.d(TAG, "Started WALK SET");
    walkIntent.putExtra("alarmType", "walkReminder");
    PendingIntent walkPendingIntent = PendingIntent.getBroadcast(this, WALK_REMINDER, walkIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager walkAlarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
    walkAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 180000 /*AlarmManager.INTERVAL_DAY*/, walkPendingIntent);

    workoutEditor = workoutPrefs.edit();
    workoutEditor.putBoolean("walkReminderIsSet", true);
    workoutEditor.commit();
}

private void cancelWalkReminder(){
    Intent walkIntent = new Intent(this, AlarmReceiver.class);
    PendingIntent walkPendingIntent = PendingIntent.getBroadcast(this, WALK_REMINDER, walkIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Log.d(TAG, "Walk Canceled");
    AlarmManager walkAlarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
    walkAlarmManager.cancel(walkPendingIntent);
}

我正在检查警报是否通过以下方法设置:

boolean walkAlarmDown = (PendingIntent.getBroadcast(this, WALK_REMINDER,
            new Intent(this, AlarmReceiver.class), PendingIntent.FLAG_NO_CREATE) == null);

    if (walkAlarmDown && workoutPrefs.getBoolean("walkReminderIsNeeded", true)){
        Log.d(TAG, "WALK REMINDER SET");
        setWalkReminder();
    } else if (!walkAlarmDown && !workoutPrefs.getBoolean("walkReminderIsNeeded", true)){
        Log.d(TAG, "WALK REMINDER CANCELED");
        cancelWalkReminder();
    } else {
        if(!workoutPrefs.getBoolean("walkReminderIsNeeded", true) && walkAlarmDown){
            Log.d(TAG, "Why is this false?");
        } else if (!walkAlarmDown && workoutPrefs.getBoolean("walkReminderIsNeeded", true)){
            Log.d(TAG, "proper functioning");
        }
}

如果用户在取消后重新启用提醒,则不会再次设置提醒。 Log.d输出为proper functioning。这是因为walkAlarmDown仍然给出错误。我相信这是因为当闹钟被取消时,walkAlarmDown没有变为真。如何确保取消警报时walkAlarmDown应该变为true?

Alernatively,它是否仍然是真的,因为我的其他闹钟已启用?因此,只有一个警报被取消,另一个警报仍然有效。它有一个不同的广播请求代码。所以我认为它应该不是问题。

2 个答案:

答案 0 :(得分:1)

即使警报管理员被取消,

walkPendingIntent仍然有效。代码walkAlarmManager.cancel(walkPendingIntent);应该跟walkPendingIntent.cancel()后面。现在walkAlarmDown取消警报后会发出真实信息。

答案 1 :(得分:0)

添加@ suku的答案当您在AlarmManager上调用cancel()时,警报会被取消。

<form id="form1" runat="server" action ="add_photo.aspx">
    <div>
       <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
           <ol class="carousel-indicators">
              <li data-target="#carousel-example-generic"></li>
              <li data-target="#carousel-example-generic"></li>
              <li data-target="#carousel-example-generic"></li>
           </ol>
        <div class="carousel-inner" role="listbox">
        <asp:Repeater ID="Repeater1" runat="server">
             <ItemTemplate>
                <div class="item<%# Container.ItemIndex == 0 ? "active" : "" %>">
                    <asp:Image ID="Image1" runat="server" ImageUrl ='<%# 
                      Eval("path").ToString() %>'/>
                        <div class="carousel-caption"></div>
                </div>
            </ItemTemplate>
        </asp:Repeater>
   </div>
      </div>

    </div>
</form>

但是,由于您根据Pending Intent的存在设置或重置警报,您还需要明确取消它。

尝试将定期间隔缩短至30秒,以测试警报的发射或不发射。 当你取消它时,你会注意到警报确实停止了触发但是Pending Intent仍然保持活动状态,这就是造成误报的原因。

根据Pending Intent存在您的警报管理在我看来是错误的逻辑。警报设置或重置应基于用户意图。