我想取消定义服务的AlarmManager,在此服务中可能会启动一个新的AlarmManger或取消之前定义的警报。我知道alarmManager.cancel(PendingIntent)中的params pendingintent,必须与filterEquals相同。意图其他) 但它仍然没有工作。解决失败。 这是我的代码
public class GetRoundStroe {
private Store[] stores;
private Context mContext;
public GetRoundStroe(Context mContext) {
this.mContext = mContext;
}
public Store[] getStores() {
if (ComCommand.haveInternet(mContext)) {
start_am_normal();
} else {
start_am_silence();
}
return stores;
}
public Store[] start_am_silence() {
long firstTime = SystemClock.elapsedRealtime();
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
if (AlarmHolder.mAlarmNormal != null) {
am.cancel(AlarmHolder.mAlarmNormal);
}
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, TestSwitch.getInstance().getSilence_time(), AlarmHolder.mAlarmSilence);
return null;
}
public Store[] start_am_normal() {
long firstTime = SystemClock.elapsedRealtime();
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
if (AlarmHolder.mAlarmSilence != null) {
MyLog.e(GetRoundStroe.class,"AlarmHolder.mAlarmSilence"+AlarmHolder.mAlarmSilence+"");
am.cancel(AlarmHolder.mAlarmSilence);
}
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, TestSwitch.getInstance().getNormal_time(), AlarmHolder.mAlarmNormal);
return null;
}
private static final class AlarmHolder {
static final PendingIntent mAlarmSilence = PendingIntent.getService(ApplicationContext.getInstance(),
0,
new Intent(ApplicationContext.getInstance(), GetRoundSilenceService.class),
0);
static final PendingIntent mAlarmNormal = PendingIntent.getService(ApplicationContext.getInstance(),
0, new
Intent(ApplicationContext.getInstance(), GetRoundNormalService.class),
0);
}
}
GetRoundSilenceService和GerRoundNormalService调用start_am_normal()或start_am_silence;有人可以帮帮我吗?感谢
答案 0 :(得分:59)
myIntent = new Intent(SetActivity.this, AlarmActivity.class);
pendingIntent = PendingIntent.getActivity(CellManageAddShowActivity.this,
id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent.cancel();
alarmManager.cancel(pendingIntent);
这些代码行肯定可以帮助您删除/取消待处理的意图和警报。
您需要的主要内容是:
答案 1 :(得分:0)
@MKJParekh的答案是正确的,但是我想添加更多信息,以便我们都知道哪些有效,哪些无效。
让我们在activityA上说你创建并设置AlarmManager在30秒内打开activityC,然后在其他一些活动中,我们想要取消该AlarmManager。因此,我们会做以下事情;
在activityA中我们创建并设置AlarmManager;
//activityA
Intent myIntentA = new Intent(actvityA.this, activityB.class)
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent myPendingIntent = PendingIntent.getActivity(activityA.this, 0, myIntentA, PendingIntent.FLAG_ONE_SHOT);
//Calendar with the time we want to fire the Alarm
Calendar calendar = Calendar.getInstance(); // Get Current Time
calendar.add(Calendar.SECOND,30); //Fire Alarm in 30 seconds from Now.
((AlarmManager)getSystemService(ALARM_SERVICE)).setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), myPendingIntent);
在Another Activity中,稍后我们要取消在activityA中创建的无法访问的AlarmManager。让我们称之为当前的活动活动Z;
//activityZ
Intent myIntentZ = new Intent(activityZ.this, activityB.class);
PendingIntent pendingIntentZ = PendingIntent.getActivity(activityZ.this, 0, myIntentZ, PendingIntent.FLAG_ONE_SHOT);
((AlarmManager)getSystemService(ALARM_SERVICE)).cancel(pendingIntentZ);
一些重点,
我们在new Intent(context)
和getActivity(context)
中的activityA中提供的上下文是相同的,但是它们不必与我们取消AlarmManager
的活动匹配,在本例中为activityZ有另一种背景。
我们想要使用AlarmManager
打开的类在两个活动new Intent (context, activityB.class)
中都必须相同,requestCode是一个int必须相同,我使用0
为此例。最后,两个活动PendingIntent.FLAG_ONE_SHOT
中的标志必须相同。
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
,因为如果我们在现有活动的上下文之外启动活动,则PendingIntent.getActivity需要它。