4

やあ!

PendingIntents の複製に問題があります。私のアプリケーションには、アプリケーションの onCreate で開始するサービスがあり、いくつかの非同期タスクを実行して、自分で停止します。問題は、アプリケーションを起動するたびに、AlarmManager に DB からの PendingIntents の新しいセット (まったく同じ) があることですが、FLAG_CANCEL_CURRENT を使用しても以前のものがキャンセルされません。これは「adb shell dumpsys alarm」で判断し
ます。非同期タスクの onPostExecute は次のとおりです。 protected void onPostExecute (Cursor c) {

        while (c.moveToNext())
        {
            int _id = c.getInt(c.getColumnIndex(Maindb._ID));
            long remind_timestamp = c.getLong(c
                    .getColumnIndex(Maindb.REMIND_TIMESTAMP));
            String remind_name = c.getString(c.getColumnIndex(Maindb.NAME));
            String remind_description = c.getString(c
                    .getColumnIndex(Maindb.DESCRIPTION));

            Log.i("Service reminders : id = " + _id + "; REMIND_TIMESTAMP="
                    + remind_timestamp + "; NAME = " + remind_name
                    + "; DESCRIPTION=" + remind_description);

            Intent myIntent = new Intent(ReminderService.this,
                    AlarmReceiver.class);

            myIntent.putExtra(RemindDialog.REMIND_DIALOG_ID, _id);
            myIntent.putExtra(RemindDialog.REMIND_DIALOG_NAME, remind_name);
            myIntent.putExtra(RemindDialog.REMIND_DIALOG_DESCRIPTION,
                    remind_description);
            myIntent.putExtra(RemindDialog.REMIND_DIALOG_TIMESTAMP,
                    remind_timestamp);

            pendingIntent = PendingIntent.getService(ReminderService.this,
                    _id, myIntent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);

            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

            alarmManager.set(AlarmManager.RTC_WAKEUP, remind_timestamp,
                    pendingIntent);
            Log.i("Successfully setted alarm for ID:TIMESTAMP = " + _id
                    + ":" + remind_timestamp);


        }

        Log.d("Closing cursor");
        c.close();

        Log.d("Nothing else to do, Stoping the services by himself");
        stopSelf();

    }

2 回目のアプリケーション起動後の「adb shell dumpsys alarm」の出力は次のようになります。

RTC_WAKEUP #26: Alarm{42096e70 type 0 com.} type=0 when=+93d9h0m54s107ms repeatInterval=0 count=0 operation=PendingIntent{4283e8b8: PendingIntentRecord{426ab530 com.startService}}

RTC_WAKEUP #25: Alarm{41dff7f8 type 0 com.} type=0 when=+93d9h0m54s107ms repeatInterval=0 count=0 operation=PendingIntent{41f1e730: PendingIntentRecord{41e7e1b0 com.startService}}

RTC_WAKEUP #24: Alarm{42161b60 type 0 com.} type=0 when=+76d19h50m54s107ms repeatInterval=0 count=0 operation=PendingIntent{428494d8: PendingIntentRecord{42705b90 com.startService}}

RTC_WAKEUP #23: Alarm{41ef50a8 type 0 com.} type=0 when=+76d19h50m54s107ms repeatInterval=0 count=0 operation=PendingIntent{41f1de18: PendingIntentRecord{41efdcd0 com.startService}}

RTC_WAKEUP #22: Alarm{42549b40 type 0 com.} type=0 when=+51d5h30m54s107ms repeatInterval=0 count=0 operation=PendingIntent{428697e8: PendingIntentRecord{427c9890 com.startService}}

RTC_WAKEUP #21: Alarm{41f2fe20 type 0 com.} type=0 when=+51d5h30m54s107ms repeatInterval=0 count=0 operation=PendingIntent{41fb31a0: PendingIntentRecord{41f3d018 com.startService}}

RTC_WAKEUP #20: Alarm{4269f008 type 0 com.} type=0 when=+21d10h30m54s107ms repeatInterval=0 count=0 operation=PendingIntent{428706f0: PendingIntentRecord{427fd1f0 com.startService}}

RTC_WAKEUP #19: Alarm{41fb1428 type 0 com.} type=0 when=+21d10h30m54s107ms repeatInterval=0 count=0 operation=PendingIntent{41f3c958: PendingIntentRecord{4212d098 com.startService}}

RTC_WAKEUP #18: Alarm{426aa948 type 0 com.} type=0 when=+16d14h16m54s107ms repeatInterval=0 count=0 operation=PendingIntent{42875fb8: PendingIntentRecord{4282bf98 com.startService}}

RTC_WAKEUP #17: Alarm{42554a70 type 0 com.} type=0 when=+16d14h16m54s107ms repeatInterval=0 count=0 operation=PendingIntent{41ec39e8: PendingIntentRecord{426a0620 com.startService}}

したがって、主な問題は、なぜ私の PendingIntents が複製されるのかということです。なぜなら、最初の外観では、最初と 2 番目の起動はまったく同じにする必要があるからです (入力カーソルは間違いなく同じです)。

ありがとう!私の英語でごめんなさい。

4

1 に答える 1

6

PendingIntent重複しないように を交換した可能性があります。ただし、以前に でスケジュールしたアラームをキャンセルしていませんAlarmManager。これを行うには、次のように呼び出す必要があります。

alarmManager.cancel(pendingIntent);

する前に:

alarmManager.set(AlarmManager.RTC_WAKEUP, remind_timestamp, pendingIntent);

これにより、渡された に一致するすべてのアラームがキャンセルされPendingIntentます。

編集:上記の回答はOPの助けにはならず、おそらく間違っています(A--Cからのコメントを参照)

PendingIntent.FLAG_UPDATE_CURRENTの代わりにPendingIntent.FLAG_CANCEL_CURRENTを使用する必要がありますPendingIntent。これにより、スケジュールされたアラームは 1 つだけになります。

PendingIntent問題は、新しいアラームを作成するときに既存の をキャンセルするAlarmManagerと、スケジュールしているアラームが別のアラームに置き換わっていることを が判断できなくなることです (PendingIntent古いアラームが存在しないため、 を比較できないため)。

于 2013-01-27T16:42:02.473 に答える