すでに回答済みです。同じ問題を抱えている方は、この投稿の下部を参照してください。
私は私のアプリにアラームをスケジュールするこの部分があり、すべてがうまく機能し、アラームを正しくスケジュールしましたが、スケジュールされたアラームが一度発生するのではなく、何度も発生するという不安定な動作があります、私は知っていますこのcuzは、アラームが発生するたびにlogcatに「ALARMFIRED」を表示しました。logcatにはいくつかのALARM FIREDがあり、場合によっては最大10個あります。混乱しているのは、そのアラームを1回だけ回避し、何度も発生したことです。コードは次のとおりです。
これは私のアラームをスケジュールするためのものですこれは静的な方法でアクセスされるヘルパークラスにあります
public final static String ACTION = "com.medical.organizer.utilities.NotifyReciever";
private static NotifyReciever RECIEVER = new NotifyReciever();
public static void scheduleAlarm(Context context,String message,int rCode, long triggerTime)
{
RECIEVER = new NotifyReciever();
NotifyAlarm.TRIGGER_TIME = triggerTime;
NotifyAlarm.CONTEXT = context;
IntentFilter inFil = new IntentFilter(ACTION);
NotifyAlarm.CONTEXT.registerReceiver(RECIEVER, inFil);
Intent intent = new Intent(ACTION);
intent.putExtra("message", message);
NotifyAlarm.INTENT = intent;
NotifyAlarm.startAlarm(rCode);
}
public static void cancelAlarm(Context context, int requestCode)
{
RECIEVER = new NotifyReciever();
NotifyAlarm.CONTEXT = context;
IntentFilter inFil = new IntentFilter(ACTION);
NotifyAlarm.CONTEXT.registerReceiver(RECIEVER, inFil);
Intent intent = new Intent(ACTION);
NotifyAlarm.INTENT = intent;
//context.unregisterReceiver(RECIEVER);
NotifyAlarm.stopAlarm(requestCode);
}
これはアラーム自体のためです
public class NotifyAlarm {
public static Context CONTEXT;
public static Intent INTENT;
public static long TRIGGER_TIME;
public static void startAlarm(int requestCode)
{
AlarmManager am = (AlarmManager) CONTEXT.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(CONTEXT, requestCode, INTENT, 0);
am.set(AlarmManager.RTC_WAKEUP, TRIGGER_TIME, pendingIntent);
Log.d("AlarmCheck", "Alarm Started for the First Time, set() was called");
}
public static void stopAlarm(int requestCode)
{
AlarmManager am = (AlarmManager) CONTEXT.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(CONTEXT, requestCode, INTENT, 0);
am.cancel(pendingIntent);
Log.d("ALARMSTOP","========ALARM WAS STOPPED AT THIS POINT==========");
}
}
これは私のアラーム受信者(放送受信者)のためのものです
public class NotifyReciever extends BroadcastReceiver {
private Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
Log.d("AlarmCheck","ALARM FIRED!");
AlertDialog.Builder build = new AlertDialog.Builder(context);
String message = intent.getStringExtra("message");
build.setMessage(message);
build.setCancelable(false);
build.setPositiveButton("Snooze", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Rechedules the alarm after 25 secs time of interval is for
//testing purposes only
NotifyReciever.this.context.unregisterReceiver(NotifyReciever.this);
Helper.scheduleAlarm(NotifyReciever.this.context, s,
s.getRequestCode(),System.currentTimeMillis()+Helper.TWENTY_FIVE_SEC);
}
});
build.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//CANCELS THE ALARM
Helper.cancelAlarm(NotifyReciever.this.context, s.getRequestCode());
}
});
AlertDialog alert = build.create();
alert.show();
}
}
受信者はXMLで宣言されておらず、ダイアログをサポートするためにプログラムで登録されていることに注意してください。
この状況を考えてみましょう。これは、動作が常に発生することに気付いた場所です。たとえば、スケジュールを追加し、午前5時(現在の時刻は4時59分)にアラームをスケジュールしたので、最初は完全に正常に起動し、次に決定しました。そのアラームを5:01(現在の時刻5:00)に再スケジュールすると、これが発生し、アラームが数回発生します。alearダイアログが数回表示されます。ダイアログをキャンセルするには、すべてをすばやくクリックする必要があります。表示されるダイアログが多すぎるためにアプリがクラッシュすることがあります。
誰もがこの問題を解決する方法を知っています。共有してください。アラームを適切にスケジュールして発生させる方法を知っている人は共有してください。複数のアラームをスケジュールして発生させたり、キャンセルしたりしていただければ幸いです。説明といくつかの例の方が良いでしょう。
編集
凡例:側面の色付きのドットは、特定の時間に発生し、それがlogcatにログオンしたことを意味します。たとえば、4:00に発生したアラームは、いくつかの青いドットが付いたものを表示します。
これを編集するのが答えです
これが私が発見したことです。私の場合は、受信者を1回だけ登録する必要があるため、ur ACTIONを使用してすべてのインテントに一致するintentFilterからの複数の戻りを回避するために、私の場合は受信者を何度も登録しました。繰り返しになりますが、同じACTIOnを持つインテントのインスタンスがすでに多数存在するため、複数の受信者が返され、1つの受信者だけを起動するのではなく、指定された時間に複数のアラームを起動するので、受信者がグローバルクラスになる可能性がある場合は1回だけ登録してください。受信者をXMLで宣言する