zoukankan      html  css  js  c++  java
  • Android PendingIntent简介

    A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application’s process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it.
    翻译如下:一个PendingIntent就是一个Android系统管理并持有的用于描述和获取原始数据的对象的标志(引用)。这就意味着,即便创建该PendingIntent对象的进程被杀死了,这个PendingItent对象自己在其他进程中还是可用的。如果创建该PendingIntent对象的进程随后又重新获取了一个同类型的PendingIntent(对于程序来讲,就是通过同样的方法获取的,例如都是通过getActivity、getBroadcast、getService方法来获取的,并且传递给getXXX方法的Intent对象的Action是相同的,Data也是相同的,Categories也是相同的,Components也是相同的,Flags也是相同的),如果之前获取的PendingIntent对象还有效的话,那么该进程获取到的PendingItent对象将获得同一个对象的引用,而且可以通过cancel()方法来从系统中移除它。

    如果我们只是想通过设置不同的Extra来生成不同的PendingIntent对象是行不通的,因为PendingIntent对象由系统持有,并且系统只通过刚才在上面提到的几个要素来判断PendingIntent对象是否是相同的,那么如果我们想在每次更新Widget的时候也更新PendingIntent对象的话,我们应该怎么做的,目前我能想到的就是通过设置Flag的方式来做。
    目前在Android中有以下flag:
    FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。

    FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null。

    FLAG_ONE_SHOT:该PendingIntent只作用一次,如果该PendingIntent对象已经触发过一次,那么下次再获取该PendingIntent并且再触发时,系统将会返回一个SendIntentException,在使用这个标志的时候一定要注意哦。
    FLAG_UPDATE_CURRENT:如果系统中已存在该PendingIntent对象,那么系统将保留该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。这个非常有用,例如之前提到的,我们需要在每次更新之后更新Intent中的Extras数据,达到在不同时机传递给MainActivity不同的参数,实现不同的效果。

  • 相关阅读:
    JAVA EE获取浏览器和操作系统信息
    ANT与SVN集成
    ANT property三种使用方式
    跨机器的文件夹访问和传输方法
    yolov5网络结构分析
    点到直线距离计算及g++编译
    深拷贝与浅拷贝(c++和python)
    qt工程环境设置
    Git 篇
    PyCharm 使用的技巧
  • 原文地址:https://www.cnblogs.com/xuewater/p/2745197.html
Copyright © 2011-2022 走看看