0

ユーザー定義のイベントをスケジュールするアラームマネージャーを作成しましたが、正常に機能しています。次に、(設定アクティビティから)設定画面を作成して、ユーザーが(アラームの前日)などの設定を変更できるようにしました。

私の質問は、イベント開始日の前にイベントを3日スケジュールすると、ユーザーは前日を設定から1日のみに変更します。

次に、イベントの開始日の1日前にイベントを再度スケジュールします。つまり、ユーザーには2回通知されます。

  • 3日前に1つ
  • 1日前に

それが本当なら、どうすればそれが起こらないようにすることができますか

前もって感謝します

4

2 に答える 2

2

各アラームには、一意のハッシュ識別子を持つPendingIntentが付随しています。同じ識別子を使用すると、ドキュメントに記載されているように、そのアラームが前者を上書きします: http : //developer.android.com/reference/android/app/AlarmManager.html#set(int、long、android.app.PendingIntent)

アラームをスケジュールします。注:...同じIntentSenderにすでにアラームがスケジュールされている場合は、最初にキャンセルされます...

保留中のインテントは、パラメーターと識別子によって特徴付けられることに注意してください。

Intent intent = new Intent(mContext, YourTarget.class);
// The hashcode works as an identifier here. By using the same, the alarm will be overwrriten
PendingIntent sender = PendingIntent.getBroadcast(mContext, hashCode, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
    mAlarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
} else {
    mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
}
于 2013-03-24T09:37:47.533 に答える
1
public void cancel (PendingIntent operation)

Remove any alarms with a matching Intent. Any alarm, of any type, whose Intent 

これに一致すると(filterEquals(Intent)で定義)、キャンセルされます。

したがって、保留中のインテントalarmManager.cancel(myPendingIntent)でcancelを呼び出し、新しい時刻で新しいインテントを作成できます。

ただし、新しいPendingIntentを前のPendingIntentと比較するときにfilterEqualsがtrueを返す限り、アラームは1日前に開始されるため、cancelを明示的に呼び出す必要はありません。

public boolean filterEquals(Intent other)

Determine if two intents are the same for the purposes of intent resolution 

(フィルタリング)。つまり、アクション、データ、タイプ、クラス、およびカテゴリが同じである場合です。これは、インテントに含まれる追加のデータを比較しません。

于 2013-03-24T09:51:23.530 に答える