简体   繁体   English

取消另一个pendingintent中的AlarmManager pendingIntent

[英]Cancel an AlarmManager pendingIntent in another pendingintent

I want cancel AlarmManager which define a service,in this service might start a new AlarmManger or cancel alarm that defined before.And I know the params pendingintent in alarmManager.cancel(PendingIntent),must be the same.compare with filterEquals(Intent other) but It still not work.cancel failed. 我想取消定义服务的AlarmManager,在这个服务中可能会启动一个新的AlarmManger或取消之前定义的警报。我知道alarmManager.cancel(PendingIntent)中的params pendingintent,必须是相同的。与filterEquals(Intent other)比较但它仍然没有工作。解决失败。 here is my code 这是我的代码

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 and GerRoundNormalService invoke start_am_normal() or start_am_silence; GetRoundSilenceService和GerRoundNormalService调用start_am_normal()或start_am_silence; Anyone could help me? 有人可以帮帮我吗? thanks 谢谢

   myIntent = new Intent(SetActivity.this, AlarmActivity.class);
   pendingIntent = PendingIntent.getActivity(CellManageAddShowActivity.this,
       id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
   pendingIntent.cancel();
   alarmManager.cancel(pendingIntent);

These lines of code surely can help you remove/cancel the pending intent and alarm. 这些代码行肯定可以帮助您删除/取消待处理的意图和警报。

The main thing that you will need is: 你需要的主要是:

  1. Create pending intent with the same id and appropriate intent FLAG. 使用相同的id和适当的意图FLAG创建待定意图。
  2. Cancel that pending intent. 取消该待定意图。
  3. Cancel the alarm using alarm manager. 使用警报管理器取消警报。

@MKJParekh answer is correct, however I would like to add more information so that we all know what will work and what will not. @MKJParekh的答案是正确的,但是我想添加更多信息,以便我们都知道哪些有效,哪些无效。

Lets say on activityA you create and set the AlarmManager to open activityC in 30 seconds, then on some other activity which can be any, we want to cancel that AlarmManager. 让我们说在activityA上你创建并设置AlarmManager在30秒内打开activityC,然后在其他一些活动上可以是任何,我们想要取消该AlarmManager。 Thus, we would do the following; 因此,我们会做以下事情;

in activityA we create and set the 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);

in Another Activity some time later we want to cancel such AlarmManager created in activityA which we have no access to. 在另一个Activity中,我们想在一段时间后取消在activityA中创建的无法访问的AlarmManager。 Lets call this current activity activityZ; 让我们称之为当前的活动活动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);

Some important points, 一些重点,

The context we provide in activityA in new Intent(context) and getActivity(context) are the same, however they do not have to match the activity from where we are canceling the AlarmManager , in this case activityZ has another context. 我们在new Intent(context)getActivity(context)中的activityA中提供的getActivity(context)是相同的,但是它们不必匹配我们取消AlarmManager活动,在这种情况下,activityZ具有另一个上下文。

The class we want to open with the AlarmManager has to be the same in both activities new Intent (context, activityB.class) , the requestCode which is an int must be the same, I used 0 for this example. 我们要用AlarmManager打开的类必须在两个活动中都是相同的new Intent (context, activityB.class) ,作为int的requestCode必须是相同的,我在这个例子中使用0 Finally the flag has to be the same in both activities PendingIntent.FLAG_ONE_SHOT . 最后,两个活动PendingIntent.FLAG_ONE_SHOT的标志必须相同。

myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); was used because PendingIntent.getActivity requires it if we are starting an activity outside of a context of an existing activity. 之所以使用是因为PendingIntent.getActivity需要它,如果我们在现有活动的上下文之外启动活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM