你好!
我在复制PendingIntents方面遇到了困难。我的应用程序有一个服务,在应用程序的onCreate时启动,执行一些异步任务,并自行停止。
问题是,每次应用程序启动时,我都会从数据库中获得一组新的PendingIntents(完全相同),但它们不会取消以前的PendingIntents,即使使用FLAG_CANCEL_CURRENT也无效。我通过“adb shell dumpsys alarm”确定了这一点。
以下是异步任务的onPostExecute:
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();
}
第二次启动应用程序后,从“adb shell dumpsys alarm”输出的结果如下:
所以,主要问题是为什么我的PendingIntents会重复,因为乍一看,第一次和第二次启动应该完全相同(输入光标肯定相同)。RTC_WAKEUP #26: 闹钟{42096e70 类型 0 com.} 类型=0 时间=+93d9h0m54s107ms 重复间隔=0 次数=0 操作=PendingIntent{4283e8b8: PendingIntentRecord{426ab530 com.startService}}
RTC_WAKEUP #25: 闹钟{41dff7f8 类型 0 com.} 类型=0 时间=+93d9h0m54s107ms 重复间隔=0 次数=0 操作=PendingIntent{41f1e730: PendingIntentRecord{41e7e1b0 com.startService}}
RTC_WAKEUP #24: 闹钟{42161b60 类型 0 com.} 类型=0 时间=+76d19h50m54s107ms 重复间隔=0 次数=0 操作=PendingIntent{428494d8: PendingIntentRecord{42705b90 com.startService}}
RTC_WAKEUP #23: 闹钟{41ef50a8 类型 0 com.} 类型=0 时间=+76d19h50m54s107ms 重复间隔=0 次数=0 操作=PendingIntent{41f1de18: PendingIntentRecord{41efdcd0 com.startService}}
RTC_WAKEUP #22: 闹钟{42549b40 类型 0 com.} 类型=0 时间=+51d5h30m54s107ms 重复间隔=0 次数=0 操作=PendingIntent{428697e8: PendingIntentRecord{427c9890 com.startService}}
RTC_WAKEUP #21: 闹钟{41f2fe20 类型 0 com.} 类型=0 时间=+51d5h30m54s107ms 重复间隔=0 次数=0 操作=PendingIntent{41fb31a0: PendingIntentRecord{41f3d018 com.startService}}
RTC_WAKEUP #20: 闹钟{4269f008 类型 0 com.} 类型=0 时间=+21d10h30m54s107ms 重复间隔=0 次数=0 操作=PendingIntent{428706f0: PendingIntentRecord{427fd1f0 com.startService}}
RTC_WAKEUP #19: 闹钟{41fb1428 类型 0 com.} 类型=0 时间=+21d10h30m54s107ms 重复间隔=0 次数=0 操作=PendingIntent{41f3c958: PendingIntentRecord{4212d098 com.startService}}
RTC_WAKEUP #18: 闹钟{426aa948 类型 0 com.} 类型=0 时间=+16d14h16m54s107ms 重复间隔=0 次数=0 操作=PendingIntent{42875fb8: PendingIntentRecord{4282bf98 com.startService}}
RTC_WAKEUP #17: 闹钟{42554a70 类型 0 com.} 类型=0 时间=+16d14h16m54s107ms 重复间隔=0 次数=0 操作=PendingIntent{41ec39e8: PendingIntentRecord{426a0620 com.startService}}
谢谢!对我的英语不好感到抱歉。
_id
值是否总是相同的?因为每个不同的_id
值都会得到一个不同的PendingIntent
。 - David Wasser