如何避免重复PendingIntents

Ser*_*bak 4 android android-intent android-pendingintent

嗨!

我在复制PendingIntents方面遇到了麻烦.我的应用程序具有从应用程序onCreate开始的服务,执行一些异步任务并自行停止.问题是,在每一个应用程序开始我从DB在AlarmManager一套新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();

    }
Run Code Online (Sandbox Code Playgroud)

第二次应用程序启动后"adb shell dumpsys alarm"的输出如下所示:

RTC_WAKEUP#26:报警{42096e70类型0 com.}类型= 0当= + 93d9h0m54s107ms repeatInterval = 0 count = 0 operation = PendingIntent {4283e8b8:PendingIntentRecord {426ab530 com.startService}}

RTC_WAKEUP#25:报警{41dff7f8类型0 com.}类型= 0当= + 93d9h0m54s107ms repeatInterval = 0 count = 0 operation = PendingIntent {41f1e730:PendingIntentRecord {41e7e1b0 com.startService}}

RTC_WAKEUP#24:报警{42161b60类型0 com.}类型= 0当= + 76d19h50m54s107ms repeatInterval = 0 count = 0 operation = PendingIntent {428494d8:PendingIntentRecord {42705b90 com.startService}}

RTC_WAKEUP#23:报警{41ef50a8类型0 com.}类型= 0当= + 76d19h50m54s107ms repeatInterval = 0 count = 0 operation = PendingIntent {41f1de18:PendingIntentRecord {41efdcd0 com.startService}}

RTC_WAKEUP#22:报警{42549b40类型0 com.}类型= 0当= + 51d5h30m54s107ms repeatInterval = 0 count = 0 operation = PendingIntent {428697e8:PendingIntentRecord {427c9890 com.startService}}

RTC_WAKEUP#21:报警{41f2fe20类型0 com.}类型= 0当= + 51d5h30m54s107ms repeatInterval = 0 count = 0 operation = PendingIntent {41fb31a0:PendingIntentRecord {41f3d018 com.startService}}

RTC_WAKEUP#20:报警{4269f008类型0 com.}类型= 0当= + 21d10h30m54s107ms repeatInterval = 0 count = 0 operation = PendingIntent {428706f0:PendingIntentRecord {427fd1f0 com.startService}}

RTC_WAKEUP#19:报警{41fb1428类型0 com.}类型= 0当= + 21d10h30m54s107ms repeatInterval = 0 count = 0 operation = PendingIntent {41f3c958:PendingIntentRecord {4212d098 com.startService}}

RTC_WAKEUP#18:报警{426aa948类型0 com.}类型= 0当= + 16d14h16m54s107ms repeatInterval = 0 count = 0 operation = PendingIntent {42875fb8:PendingIntentRecord {4282bf98 com.startService}}

RTC_WAKEUP#17:报警{42554a70类型0 com.}类型= 0当= + 16d14h16m54s107ms repeatInterval = 0 count = 0 operation = PendingIntent {41ec39e8:PendingIntentRecord {426a0620 com.startService}}

所以,主要的问题是为什么我的PendingIntents重复,因为在第一次看,第一次和第二次启动应该完全相同(输入Cursor绝对相同).

谢谢!对不起我的英语不好.

Dav*_*ser 6

您可能已经替换了它,PendingIntent因此您没有重复项.但是,您尚未取消之前安排的警报AlarmManager.要做到这一点,你需要打电话:

alarmManager.cancel(pendingIntent);
Run Code Online (Sandbox Code Playgroud)

在做之前:

alarmManager.set(AlarmManager.RTC_WAKEUP, remind_timestamp, pendingIntent);
Run Code Online (Sandbox Code Playgroud)

这将取消与传递的匹配的任何警报PendingIntent.

编辑:以上答案对OP没有帮助,可能是错误的(参见A - C的评论)

您需要使用PendingIntent.FLAG_UPDATE_CURRENT而不是PendingIntent.FLAG_CANCEL_CURRENT替换时PendingIntent.这应该只导致1个预定的警报.

问题是,通过取消现有的PendingIntent,当你创建一个新的,你把它不可能为了AlarmManager确定您正在计划将取代另外一个(因为它不能比较报警PendingIntent,因为旧的就不再出现).