取消AlarmManager pendingIntent在另一个pendingintent [英] Cancel an AlarmManager pendingIntent in another pendingintent

查看:123
本文介绍了取消AlarmManager pendingIntent在另一个pendingintent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想取消AlarmManager它定义一个服务,在这个服务可能会启动一个新的AlarmManger或取消定义before.And我知道pendingintent在alarmManager.cancel(PendingIntent)的PARAMS警报,必须与filterEquals的same.compare(意图等) 但它仍然不是work.cancel失败。 这里是我的code

 公共类GetRoundStroe {
    私人商店[]专卖店;
    私人语境mContext;

    公共GetRoundStroe(上下文mContext){
        this.mContext = mContext;
    }

    公用存储[] getStores(){
        如果(ComCommand.haveInternet(mContext)){
            start_am_normal();
        } 其他 {
            start_am_silence();
        }
        返回专卖店;
    }

    公用存储[] start_am_silence(){


        长firstTime = SystemClock.elapsedRealtime();

        AlarmManager AM =(AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);

        如果(AlarmHolder.mAlarmNormal!= NULL){
            am.cancel(AlarmHolder.mAlarmNormal);

        }

        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                。firstTime,TestSwitch.getInstance()getSilence_time(),AlarmHolder.mAlarmSilence);


        返回null;


    }

    公用存储[] start_am_normal(){


        长firstTime = SystemClock.elapsedRealtime();

        AlarmManager AM =(AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);

        如果(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);

        返回null;
    }

    私有静态final类AlarmHolder {
        静态最终PendingIntent mAlarmSilence = PendingIntent.getService(ApplicationContext.getInstance()
                0,
                新的意图(ApplicationContext.getInstance(),GetRoundSilenceService.class)
                0);

        静态最终PendingIntent mAlarmNormal = PendingIntent.getService(ApplicationContext.getInstance()
                0,新
                意图(ApplicationContext.getInstance(),GetRoundNormalService.class)
                0);

    }
}
 

GetRoundSilenceService和GerRoundNormalService调用start_am_normal()或start_am_silence;任何人都可以帮助我吗?谢谢

解决方案

  myIntent =新的意图(SetActivity.this,AlarmActivity.class);
   pendingIntent = PendingIntent.getActivity(CellManageAddShowActivity.this,
       ID,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
   pendingIntent.cancel();
   alarmManager.cancel(pendingIntent);
 

的code这些行一定能帮助您删除/取消挂起的意图,并报警。

主要的事情,你需要的是:

  1. 使用相同的ID和适当的意图FLAG创建挂起的意图。
  2. 取消该挂起的意图。
  3. 在使用报警管理器取消了警报。

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. 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; 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.
  2. Cancel that pending intent.
  3. Cancel the alarm using alarm manager.

这篇关于取消AlarmManager pendingIntent在另一个pendingintent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆