pendingIntent字面意义:等待的,未决定的Intent。

要得到一个pendingIntent对象,使用方法类的静态方法

通过getActivity(Context context, int requestCode, Intent intent, int flags)从系统取得一个用于启动一个Activity的PendingIntent对象,

通过getService(Context context, int requestCode, Intent intent, int flags)从系统取得一个启动Service的PendingIntent对象

通过getBroadcast(Context context, int requestCode, Intent intent, int flags)从系统取得一个用于向BroadcastReceiver发送广播

参数有4个,比较重要的是第三个和第一个,其次是第四个和第二个。可以看到,要得到这个对象,必须传入一个Intent作为参数,必须有context作为参数。

pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。

pendingIntent执行的操作实质上是参数传进来的Intent的操作,但是使用pendingIntent的目的在于它所包含的Intent的操作的执行是需要满足某些条件的。

PendingIntent 可以看作是对 Intent 的包装。 PendingIntent 主要持有的信息是它所包装的 Intent 和当前Application 的 Context 。正由于 PendingIntent 中保存有当前 Application 的 Context ,使它赋予带他程序一种执行的 Intent 的能力,就算在执行时当前 Application 已经不存在了,也能通过存在 PendingIntent里的 Context 照样执行 Intent 。
主要的使用的地方和例子:通知Notificatio的发送,短消息SmsManager的发送  和   警报器AlarmManager的执行等等。

Android的状态栏通知(Notification)

如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息。

步骤:

1 获取通知管理器NotificationManager,它也是一个系统服务

2 建立通知Notification notification = new Notification(icon, null, when);

3 为新通知设置参数(比如声音,震动,灯光闪烁)

4 把新通知添加到通知管理器

发送消息的代码如下:

//获取通知管理器

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)

int icon = android.R.drawable.stat_notify_chat;

long when = System.currentTimeMillis();//通知发生的时间为系统当前时间

//新建一个通知,指定其图标和标题

Notification notification = new Notification(icon, null, when);//第一个参数为图标,第二个参数为短暂提示标题,第三个为通知时间

notification.defaults = Notification.DEFAULT_SOUND;//发出默认声音

notification.flags = Notification.FLAG_AUTO_CANCEL;//点击通知后自动清除通知

Intent openintent = new Intent(this, OtherActivity.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openintent, 0);//当点击消息时就会向系统发送openintent意图

notification.setLatestEventInfo(this, “标题”, “我是内容", contentIntent);

mNotificationManager.notify(0, notification);//第一个参数为自定义的通知唯一标识
 

重点是setLatestEventInfo( )方法的最后一个参数,它是一个PendingIntent,

PendingIntent的一个很好的例子:

SmsManager的用于发送短信的方法:

sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);

第一个参数:destinationAddress 对方手机号码

第二个参数:scAddress 短信中心号码 一般设置为空

第三个参数:text 短信内容

第四个参数:sentIntent判断短信是否发送成功,如果你没有SIM卡,或者网络中断,则可以通过这个intent来判断。注意强调的是“发送”的动作是否成功。那么至于对于对方是否收到,另当别论

第五个参数:deliveryIntent当短信发送到收件人时,会收到这个deliveryIntent。即强调了“发送”后的结果

就是说是在"短信发送成功"和"对方收到此短信"才会激活 sentIntent和deliveryIntent这两个Intent。这也相当于是延迟执行了Intent

上面两个例子可以理解,PendingIntent就是一个可以在满足一定条件下执行的Intent,它相比于Intent的优势在于自己携带有Context对象,这样他就不必依赖于某个activity才可以存在。

pendingIntent初步_什么是pendingIntent的更多相关文章

  1. Webapps初步_认识HTTP例子程序读取

    package servlet_01; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io. ...

  2. 在发送信息时应用PendingIntent.FLAG_UPDATE_CURRENT

    1. 连续发送两条信息时,出现bug.以下是bug现象描述. 发送第一条信息,sentReceiver弹出toast告知发送成功,同时在listview中的发送状态立即同步更新为发送成功. 继续发送第 ...

  3. 关于android中PendingIntent.getBroadcase的注册广播

    使用语句 PendingIntent intent= PendingIntent.getBroadcast(Context context, int requestCode, Intent inten ...

  4. 用alarmmanager 多次发送PendingIntent

    遇到如下问题 service中得一随机数 用alarmmanager 发送PendingIntent的时候,receiver收到的随机数不变. pendingintent传值经常获取到的值是第一次的值 ...

  5. 向通知栏发送通知点击跳转并传递数据(PendingIntent)传递数据

    // 为发送通知的按钮的点击事件定义事件处理方法 public void send() {///// 第一步:获取NotificationManager NotificationManager nm ...

  6. android: PendingIntent的使用

    PendingIntent的Flags的类型: * Flags的类型: FLAG_ONE_SHOT:得到的pi只能使用一次,第二次使用该pi时报错 FLAG_NO_CREATE: 当pi不存在时,不创 ...

  7. Android消息通知(notification)和PendingIntent传值

    通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...

  8. Android开发之PendingIntent的使用

    PendingIntent,待确定的意图,等待的意图 官网链接:http://developer.android.com/reference/android/app/PendingIntent.htm ...

  9. PendingIntent的Flags

    PendingIntent是一个Intent的描述.包装,给予了这个PendingIntent 的组件在指定的事件发生或指定的时间到达时启动Activty.Service或者Broadcast. 根据 ...

随机推荐

  1. How to install ffmpeg,mp4box,mplayer,mencoder,flvtool2,ffmpeg-php on centos

    1. Enable RPM Fusion yum repository The CentOS rpm packages of ffmpeg, mplayer, mencoder and MP4Box ...

  2. 文件操作类CFile

    CFile file; CString str1= L"写入文件成功!"; wchar_t *str2; if (!file.Open(L"Hello.txt" ...

  3. 可选头 IMAGE_OPTIONAL_HEADER

    //IMAGE_OPTIONAL_HEADER结构(可选映像头) typedef struct _IMAGE_OPTIONAL_HEADER { // // Standard fields. // W ...

  4. error on line 1 at column 6: XML declaration allowed only at the start of the document

    This page contains the following errors: error on line 1 at column 6: XML declaration allowed only a ...

  5. ubuntu12.04安装Vmware Tools

    在VMware下安装Ubuntu,那么必须安装VMware-tools,才能获得更好的体验,包括屏幕分辨率.声音.和windows共享剪贴板等等. 个人觉得安装vmware-tools很重要的几点: ...

  6. 关于nginx架构探究(4)

    事件管理机制 Nginx是以事件驱动的,也就是说Nginx内部流程的向前推进基本都是靠各种事件的触发来驱动,否则Nginx将一直阻塞在函数epoll_wait()或suspend函数,Nginx事件一 ...

  7. CentOS安装Nginx,并配置nodejs反向代理

    安装介绍 安装位置:/usr/local/nginx nginx安装包下载地址:http://nginx.org/download/nginx-1.7.11.tar.gz 安装依赖软件 安装nginx ...

  8. 两段小PYTHON,作啥用的,行内人才懂~~~:(

    哎,作也不是,不作也不是.... 下次有更新文件时,直接刷新一次了. #coding: UTF-8 import sys reload(sys) sys.setdefaultencoding( &qu ...

  9. Mysql 存储过程和函数区别

    存储过程是procedure用户定义的一系列sql语句的集合,涉及特定表或其它对象的任务,用户可以调用存储过程,而函数通常是数据库已定义的方法,它接收参数并返回某种类型的值并且不涉及特定用户表. 存储 ...

  10. hadoop2.2.0 MapReduce的序列化

    package com.my.hadoop.mapreduce.dataformat; import java.io.DataInput;import java.io.DataOutput;impor ...