pendingIntent初步_什么是pendingIntent
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的更多相关文章
- Webapps初步_认识HTTP例子程序读取
package servlet_01; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io. ...
- 在发送信息时应用PendingIntent.FLAG_UPDATE_CURRENT
1. 连续发送两条信息时,出现bug.以下是bug现象描述. 发送第一条信息,sentReceiver弹出toast告知发送成功,同时在listview中的发送状态立即同步更新为发送成功. 继续发送第 ...
- 关于android中PendingIntent.getBroadcase的注册广播
使用语句 PendingIntent intent= PendingIntent.getBroadcast(Context context, int requestCode, Intent inten ...
- 用alarmmanager 多次发送PendingIntent
遇到如下问题 service中得一随机数 用alarmmanager 发送PendingIntent的时候,receiver收到的随机数不变. pendingintent传值经常获取到的值是第一次的值 ...
- 向通知栏发送通知点击跳转并传递数据(PendingIntent)传递数据
// 为发送通知的按钮的点击事件定义事件处理方法 public void send() {///// 第一步:获取NotificationManager NotificationManager nm ...
- android: PendingIntent的使用
PendingIntent的Flags的类型: * Flags的类型: FLAG_ONE_SHOT:得到的pi只能使用一次,第二次使用该pi时报错 FLAG_NO_CREATE: 当pi不存在时,不创 ...
- Android消息通知(notification)和PendingIntent传值
通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...
- Android开发之PendingIntent的使用
PendingIntent,待确定的意图,等待的意图 官网链接:http://developer.android.com/reference/android/app/PendingIntent.htm ...
- PendingIntent的Flags
PendingIntent是一个Intent的描述.包装,给予了这个PendingIntent 的组件在指定的事件发生或指定的时间到达时启动Activty.Service或者Broadcast. 根据 ...
随机推荐
- JavaScript获取Select下拉框Option的Value和Text值的方法
Js获取select下拉列表框各个Option的Value值相对比较容易,不过获取Text值却有点麻烦,对于一个初学JavaScript的 新手来说,可能一时还无从下手,那么就请看下本文的方法,以一个 ...
- Linux下部署Symfony2对app/cache和app/logs目录的权限设置
在linux下部署完Symfony2,可能在访问的时候会报app/logs或者app/cache目录没有写权限的错误.在linux下,如果我们在命令行登陆的用户和web应用服务器(apache.ngi ...
- ubuntu下wine打开自由们找不到MFC42.DLL重新安装的解决方法
一直在找ubuntu下的X墙工具,看到大部分的都是ssh和tor的,但是tor下载不到,找了很多方法,没有办法,只能用FG了.但是Fg是运行在windows系统下的程序. 只好再安装一遍wine,用终 ...
- POSTGRESQL小玩
因为CDH上需要用它来建HIVE的元库... 参考: http://www.cnblogs.com/mchina/archive/2012/06/06/2539003.html 一.简介 Postgr ...
- Special Pythagorean triplet
这个比较简单,慢慢进入状态. A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 + b2 = ...
- A basic Windows service in C++ (CppWindowsService)
A basic Windows service in C++ (CppWindowsService) This code sample demonstrates creating a basic Wi ...
- Linq中join & group join & left join 的用法
Linq中join & group join & left join 的用法 2013-01-30 11:12 12154人阅读 评论(0) 收藏 举报 分类: C#(14) 文章 ...
- HDOJ 1266 Reverse Number(数字反向输出题)
Problem Description Welcome to 2006'4 computer college programming contest! Specially, I give my bes ...
- 记npm包开发全过程
概述 为什么开发npm包? 如何开发? 如何写单元测试? package.json 如何发布模块? 如何使用? 为什么开发npm模块? NPM的全称是Node Package Manager,是一个N ...
- Mitsubish FX 3U PLC 串口 连接单元
前段时间遇到一个Mitsubish FX 3U PLC ,现将PLC连接单元分享一下,希望对其他人有所启示. unit PLC_MitsubishiFX; interface uses Windows ...