使用语句

[java] view plaincopy
  1. PendingIntent intent= PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags)

获得PendingIntent,浏览了各类文章,大多数说了这种方法,但是基本上也就是止步于此,可是还有最重要的没有谈及,如何区别多个已注册的PendingIntent呢,看了一下PendingIntent.getBroadcast的javadoc,第四个参数flags意为标记,初步认为flags是标识各个PendingIntent的,于是在测试中设置了个全局变量

[java] view plaincopy
  1. public static int currentIntent=0;

然后用currentIntent++作为第四个参数传递进去,测试,注册了两个监听,等待时间的到来,bingo,居然可以了,目测已经可以。可是继续深入时问题来了,我要传递参数怎样?正解做法就是在第三个参数中设置

[java] view plaincopy
  1. intent.setExtra(String key,String value);  //设置传递的参数

然后在自己实现的Receiver里用传进来的参数Intent intent实现

[java] view plaincopy
  1. intent.getIntegerExtra(String key);

就可以获得参数,可以真正在实现的时候发现,在receiver里始终取不到参数,再经过一番查找,发现要把PendingIntent.getBroadcast的第四个参数设置于PendingIntent.FLAG_UPDATE_CURRENT,设置后测试,果然可以,可是这样问题又出来了,又要如何区别注册的intent呢?再次查看getBroadcast的javadoc,几个参数都没有说明如何区别要注册的PendingIntent,反而看到第二个参数的说明很神奇,就是这个参数目前为保留状态,仍未用到,无奈中,继续search各种说法,才发现,用requestCode来区别居然是可以的(可是为什么javadoc要说该参数未被使用呢?不解;估计用于区分PendingIntent的方法就是其中任意一个参数不同便可以区分了)代码如下:

设置监听

[java] view plaincopy
  1. Intent setAlertIntent=new Intent(this,AlertReceiver.class);
  2. setAlertIntent.putExtra("try", "i'm just have a try");
  3. PendingIntent pendingIntent=PendingIntent.getBroadcast(this, alarmCount++, setAlertIntent,PendingIntent.FLAG_UPDATE_CURRENT);
  4. AlarmManager alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
  5. alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

Receiver中获取传递的数据:

 public void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubBundle bundle= intent.getExtras();if(bundle==null){Toast.makeText(context,"nothing", Toast.LENGTH_LONG).show();}else{Set<String> set=bundle.keySet();for(String item:set){System.out.println(item);System.out.println(".............");}Toast.makeText(context,bundle.getCharSequence("try"), Toast.LENGTH_LONG).show();}}

以上转自:http://blog.csdn.net/huang_hws/article/details/7327670

上面写得非常不错!但是抛出了一个问题:如何区别要注册的PendingIntent ?真的是以requestCode来区分的?

实际上,并不是以requestCode来区分

不妨做个试验1:

Intent intent = new Intent("MyReceiver");
Intent _intent = new Intent("LOL");     //注意这里是 _intent !有下划线的
PendingIntent p = PendingIntent.getBroadcast(AlarmMainActivity.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent p1 = PendingIntent.getBroadcast(AlarmMainActivity.this, 1, intent,PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent p2 = PendingIntent.getBroadcast(AlarmMainActivity.this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent p3 = PendingIntent.getBroadcast(AlarmMainActivity.this, 0, _intent,PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent p4 = PendingIntent.getBroadcast(AlarmMainActivity.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
System.out.println("*******");
if(p == p1)System.out.println("p1==p");
if(p == p2)System.out.println("p2==p");
if(p == p3)System.out.println("p3==p");
if(p == p4)System.out.println("p4==p");
System.out.println("*******");

结果输出:

很吃惊!居然p1 - 4都没有一个跟p 是同一个引用,说明每调用PendingIntent.getBroadcast(.....),构造的PendingIntent都不一样,哪怕是构造参数也一样。

那么AlarmManager.cancle(PendingIntent operation)这注销PendingIntent的函数是如何实现注销的的功能的?

该方法在Android API Docs  中的解析:

Remove any alarms with a matching Intent. Any alarm, of any type, whose Intent matches this one (as defined by filterEquals(Intent)), will be canceled.  

关于如何matches Intent,又转移到: filterEquals(Intent) 这方法(在Intent类中)

该方法在Android API Docs  中的解析:

public boolean filterEquals (Intent other)
Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

这里说明只比较action  data type class categories 而不比较里面的extra data(e.g. Bundle 、extra string int long ....)

所以这样就可以理解到cancle()的实现方式

但是国外大神们都说更requestCode 有关 参考: http://stackoverflow.com/questions/3273342/how-can-i-setup-multiple-alarms-in-android/3274576#3274576   不知道是不是我理解错了?(本人是弱菜>.<)

下面给出public boolean filterEquals (Intent other) 方法实现的源码:

   public boolean filterEquals(Intent other) {
5489        if (other == null) {
5490            return false;
5491        }
5492        if (mAction != other.mAction) {
5493            if (mAction != null) {
5494                if (!mAction.equals(other.mAction)) {
5495                    return false;
5496                }
5497            } else {
5498                if (!other.mAction.equals(mAction)) {
5499                    return false;
5500                }
5501            }
5502        }
5503        if (mData != other.mData) {
5504            if (mData != null) {
5505                if (!mData.equals(other.mData)) {
5506                    return false;
5507                }
5508            } else {
5509                if (!other.mData.equals(mData)) {
5510                    return false;
5511                }
5512            }
5513        }
5514        if (mType != other.mType) {
5515            if (mType != null) {
5516                if (!mType.equals(other.mType)) {
5517                    return false;
5518                }
5519            } else {
5520                if (!other.mType.equals(mType)) {
5521                    return false;
5522                }
5523            }
5524        }
5525        if (mPackage != other.mPackage) {
5526            if (mPackage != null) {
5527                if (!mPackage.equals(other.mPackage)) {
5528                    return false;
5529                }
5530            } else {
5531                if (!other.mPackage.equals(mPackage)) {
5532                    return false;
5533                }
5534            }
5535        }
5536        if (mComponent != other.mComponent) {
5537            if (mComponent != null) {
5538                if (!mComponent.equals(other.mComponent)) {
5539                    return false;
5540                }
5541            } else {
5542                if (!other.mComponent.equals(mComponent)) {
5543                    return false;
5544                }
5545            }
5546        }
5547        if (mCategories != other.mCategories) {
5548            if (mCategories != null) {
5549                if (!mCategories.equals(other.mCategories)) {
5550                    return false;
5551                }
5552            } else {
5553                if (!other.mCategories.equals(mCategories)) {
5554                    return false;
5555                }
5556            }
5557        }
5558
5559        return true;
5560    }

后来我又发现原来PendingIntent复写了equals()的方法

所以我实验了一下: (替换了一下上面的)

System.out.println("*******");
if(p.equals(p1))System.out.println("p1==p");
if(p.equals(p2))System.out.println("p2==p");
if(p.equals(p3))System.out.println("p3==p");
if(p.equals(p4))System.out.println("p4==p");
System.out.println("*******");

结果还是一样!囧! PendingIntent 文档说明:

public boolean equals (Object otherObj)

Added in API level 1

Comparison operator on two PendingIntent objects, such that true is returned then they both represent the same operation from the same package. This allows you to use getActivity(Context, int, Intent, int)getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int)multiple times (even across a process being killed), resulting in different PendingIntent objects but whose equals() method identifies them as being the same operation.

Parameters
otherObj the object to compare this instance with.
Returns
  • true if the specified object is equal to this Objectfalse otherwise.
源码:
564

    @Override

565

    public boolean equals(Object otherObj) {

566

        if (otherObj instanceof PendingIntent) {

567

            return mTarget.asBinder().equals(((PendingIntent)otherObj)

568

                    .mTarget.asBinder());

569

        }

570

        return false;

571

    }

这里实在没有能力读下去了 :(    先放一边了。。

关于android中PendingIntent.getBroadcase的注册广播VSAlarmManager .cancle(PendingIntent)如何区分PendingIntent相关推荐

  1. android中群发短信PendingIntent.getBroadcase的注册广播

    /*** 发送的广播**/String SENT_SMS_ACTION = "SENT_SMS_ACTION"; ​// 注册广播registerReceiver(sendMess ...

  2. Android中build target,minSdkVersion,targetSdkVersion,maxSdkVersion概念区分

    Android中build target,minSdkVersion,targetSdkVersion,maxSdkVersion概念区分 本文参考了谷歌开发者文档:http://developer. ...

  3. android四大组件之Service 注册广播接收者

    广播的注册一共有两种,一种就是用清单文件注册,还有另外一种就是用代码注册,代码注册比较灵活,可以在需要的时候注册,不需要的时候解除注册 用服务注册广播首先要开启服务, 然后在服务oncreate方法里 ...

  4. android广播 有序 无序,Android中的有序和无序广播浅析

    BroadcastReceiver所对应的广播分两类:无序广播和有序广播. 无序广播即为我们平时经常使用的广播,其主要是通过public abstract void sendBroadcast (In ...

  5. android 接收来电广播,android中未接来电的广播接收器

    Joe.. 11 您需要使用ContentObserver public class MissedCallsContentObserver extends ContentObserver { publ ...

  6. android registerreceiver传参数,Android应用程序注册广播接收器(registerReceiver)的过程分析...

    前面我们介绍了Android系统的广播机制,从本质来说,它是一种消息订阅/发布机制,因此,使用这种消息驱动模型的第一步便是订阅消息:而对Android应用程序来说,订阅消息其实就是注册广播接收器,本文 ...

  7. Android应用程序注册广播接收器 registerReceiver 的过程分析

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 前面我们 ...

  8. 创建广播接收器并注册广播

    发送广播:使用Intent 接收广播:广播接收器 创建广播接收器: 方法:新建一个类继承自BroadcastReceiver,并重写父类的onReceive( )方法.当有广播接收到的时候,onRec ...

  9. Android学习小Demo(13)Android中关于ContentObserver的使用

    在一些应用上,比如手机银行,QQ,微信等,很多时候我们都需要通过发送验证码到手机上,然后把验证码填上去,然后才能成功地继续去做下面一步事情. 而如果每次我们都要离开当前界面,然后去查收短信,记住验证码 ...

最新文章

  1. 【Android工具】最新测试谷歌play耗电情况,各种品牌安装谷歌play方法,GooglePlay...
  2. php之二叉树,PHP构造二叉树算法示例
  3. 使用logrotate分割tomcat日志
  4. js之数据类型及类型转换
  5. 图像处理-空间域锐化滤波
  6. Android 关于解决MediaButton学习到的media控制流程
  7. WebAssembly 开启微服务新时代
  8. 微信支付jsapi并写入数据库--回调函数(notify.php)的使用
  9. XP高仿win7宽栏风格主题
  10. 技术笔记:.Net全套就业班视频教程——数据库
  11. apple登录服务端验证
  12. Stochastic Depth ResNet
  13. 关于CST中S参数、极化方向的详细说明
  14. C语言源文件名为什么无效,scanf函数,想说输入不容易!----小话c语言(3)
  15. Vue.js从0开始到实战开发1:通过简单案例从0开始了解Vue
  16. 2022-2028年中国知识产权行业竞争策略研究及未来前景展望报告
  17. springcloud.3.服务注册与发现
  18. 【C++】类和对象---什么是类?
  19. N-Gram文件格式介绍 - ARPA
  20. 黑客术语肉鸡、后门、弱口令、shell、webshell、注入、端口、免杀、加壳、漏洞等

热门文章

  1. 2021年安全员-B证(山东省-2021版)报名考试及安全员-B证(山东省-2021版)
  2. 股骨颈骨折PHP,稳定型股骨颈骨折是A.头下骨折B.经颈骨折C.基底骨折D.内收骨折E.外展骨折...
  3. Error:(22, 58) java: 无法访问com.fasterxml.jackson.databind.JavaType 找不到com.fasterxml.jackson.databind
  4. 计蒜客 网络交友(map +set +并查集)
  5. 计算机课程制作月历,【信息技术】《制作月历》教学反思
  6. 外卖O2O时代来临,温州海鲜阁迎来新发展!
  7. 定制护肤技术领导者Skin Inc宣布获得来自Mistletoe的Pre-A轮融资
  8. Ubuntu系统不能访问Windows系统分盘内容解决方案
  9. 1249. 亲戚(并查集)
  10. 利用音频信号识别鸡叫声早期发现禽类疾病(译文)