Android:AlarmManager和Pending帐篷不';不要取消另一项活动
我想取消并在另一个活动中重新创建警报。我已经尝试了所有可能的方法,但没有取消 我是这样开始提醒的(在提醒活动中): 并尝试在其他活动中取消并重新创建,如:Android:AlarmManager和Pending帐篷不';不要取消另一项活动,android,alarmmanager,android-pendingintent,Android,Alarmmanager,Android Pendingintent,我想取消并在另一个活动中重新创建警报。我已经尝试了所有可能的方法,但没有取消 我是这样开始提醒的(在提醒活动中): 并尝试在其他活动中取消并重新创建,如: Date date; try { date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss").parse(editedTime); _createId = (int) date.getTime(); Log.i("current time millis r
Date date;
try {
date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss").parse(editedTime);
_createId = (int) date.getTime();
Log.i("current time millis receiver", String.valueOf(_createId));
} catch (ParseException e1) {
e1.printStackTrace();
Log.e("Errors", e1.toString());
}
Intent alarmintent = new Intent(Reminder.getReminderContext(), AlarmReceiver.class);
alarmintent.putExtra("Subject", "a");
alarmintent.putExtra("Comments", "a");
PendingIntent pIntent = PendingIntent.getBroadcast(Reminder.getReminderContext(), _createId, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager reminderAlarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
reminderAlarm.cancel(pIntent);
我确信两个PendingEvent ID是相同的,我正在取消线程内的警报
我用来取消报警的提醒类上下文是由创建的
所有的帮助和建议都是值得欣赏的。经过多次实验,我可以通过在适配器类中创建相同的PendingEvent来取消特定的AlarmManager 我所做的:我完成了活动上下文。对于PendingEvent id,我已将报警时间转换为毫秒,并通过
SharedReferences
保存。我认为这是一个很好的方法来提醒多个警报或挂起事件的ID
最后,我成功地在receiver类中创建了PendingEvent,如:
PendingIntent pIntent = PendingIntent.getBroadcast(
Reminder.getReminderContext(),
receiverId,alarmintent,PendingIntent.FLAG_UPDATE_CURRENT);
在提醒类中,我还创建了方法和PendingEvent,如:
//for the current context which is going to use in adapter or other class
private static Context mContext
public static Context getReminderContext() {
return Reminder.mContext;
}
//Reminder Activity PendingIntent
pIntent = PendingIntent.getBroadcast(Reminder.mContext, _receiverId,
alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
谢谢你给了我一个很好的建议。
现在工作正常。当您在两个位置创建
alarmintent
时,请尝试使用应用程序上下文getApplicationContext()。我检查过了,但没有变化。实际上,我正在取消适配器类中的报警。因此,在编写任何东西之前,我使用它的上下文。例如context.getApplicationContext()…我有一个类似的用例,请参阅。当我尝试设置私有静态上下文mContext时,androidstudio发出警告:“不要将Android上下文类放在静态字段中;这是内存泄漏。”关于如何修复,有什么想法吗?
//for the current context which is going to use in adapter or other class
private static Context mContext
public static Context getReminderContext() {
return Reminder.mContext;
}
//Reminder Activity PendingIntent
pIntent = PendingIntent.getBroadcast(Reminder.mContext, _receiverId,
alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);