PendingIntent的使用
Intent mIntent = new Intent("android.intent.action.MAIN");
ComponentName comp = new ComponentName(
"com.mozillaonline.downloadprovider",
"com.mozillaonline.downloadprovider.DownloadProviderActivity");
mIntent.setComponent(comp);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mIntent.addCategory("android.intent.category.LAUNCHER");
2,
context.startActivity(mIntent);
3,用pendingIntent:
PendingIntent pending= PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);
try {
pending.send();
} catch (CanceledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
- PendingIntent的使用
- * Flags的类型:
- FLAG_ONE_SHOT:得到的pi只能使用一次,第二次使用该pi时报错
- FLAG_NO_CREATE: 当pi不存在时,不创建,返回null
- FLAG_CANCEL_CURRENT: 每次都创建一个新的pi
- FLAG_UPDATE_CURRENT: 不存在时就创建,创建好了以后就一直用它,每次使用时都会更新pi的数据(使用较多)
- * 在AlarmManager中的使用
- Intent intent = new Intent("action", null, context, serviceClass);
- PendingIntent pi = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- AlarmManager manager = (AlarmManager)probe.getSystemService(Context.ALARM_SERVICE);
- manager.set(AlarmManager.RTC_WAKEUP, milis, pi);
- * 在NotificationManager中的使用
- Intent intent = new Intent();
- intent.setAction("myaction");
- PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- Notification n = new Notification();
- n.icon = R.drawable.ic_launcher;
- n.when = System.currentTimeMillis();
- n.setLatestEventInfo(this,"this is title", "this is a message", pi);
- nm.notify(0, n);
* send()方法是用,调用PendingIntent.send()会启动包装的Intent(如启动service,activity)
* cancel()方法是为了解除PendingIntent和被包装的Intent之间的关联,此时如果再调用send()方法,则会抛出CanceledException异常
————————————————————————————————————————————————————————————————————
要得到一个pendingIntent对象,使用方法类的静态方法
getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService(Context, int, Intent, int) 分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。
参数有4个,比较重要的事第三个和第一个,其次是第四个和第二个。可以看到,要得到这个对象,必须传入一个Intent作为参数,必须有context作为参数。
pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。pendingIntent执行的操作实质上是参数传进来的Intent的操作,但是使用pendingIntent的目的在于它所包含的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(pend本意是待定,不确定的意思)
PendingIntent可以看作是对Intent的包装。PendingIntent主要持有的信息是它所包装的Intent和当前Application的Context。正由于PendingIntent中保存有当前Application的Context,使它赋予带他程序一种执行的Intent的能力,就算在执行时当前Application已经不存在了,也能通过存在PendingIntent里的Context照样执行Intent。
PendingIntent的一个很好的例子:
SmsManager的用于发送短信的方法:
sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);
第一个参数:destinationAddress 对方手机号码
第二个参数:scAddress 短信中心号码 一般设置为空
第三个参数:text 短信内容
第四个参数:sentIntent判断短信是否发送成功,如果你没有SIM卡,或者网络中断,则可以通过这个itent来判断。注意强调的是“发送”的动作是否成功。那么至于对于对方是否收到,另当别论
第五个参数:deliveryIntent当短信发送到收件人时,会收到这个deliveryIntent。即强调了“发送”后的结果
就是说是在"短信发送成功"和"对方收到此短信"才会激活 sentIntent和deliveryIntent这两个Intent。这也相当于是延迟执行了Intent
上面两个例子可以理解,PendingIntent就是一个可以在满足一定条件下执行的Intent,它相比于Intent的优势在于自己携带有Context对象,这样他就不必依赖于某个activity才可以存在。
PendingIntent的使用的更多相关文章
- 用alarmmanager 多次发送PendingIntent
遇到如下问题 service中得一随机数 用alarmmanager 发送PendingIntent的时候,receiver收到的随机数不变. pendingintent传值经常获取到的值是第一次的值 ...
- 向通知栏发送通知点击跳转并传递数据(PendingIntent)传递数据
// 为发送通知的按钮的点击事件定义事件处理方法 public void send() {///// 第一步:获取NotificationManager NotificationManager nm ...
- Notification NotificationManager RemoteViews PendingIntent
Notification和NotificationManager :可以用来实现可视化的消息通知等,比如:下载进度显示,消息显示,广播的内容等 RemoteViews :用于创建自定义的Notific ...
- 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 singleTask模式下PendingIntent不能给onNewIntent传值的Bug
http://phenix.blogbus.com/logs/220656659.html 博主简直碉堡了, 我用PendingIntent给singleTask的顶层Activity传值一直收不到, ...
- 【Android 界面效果24】Intent和PendingIntent的区别
intent英文意思是意图,pending表示即将发生或来临的事情. PendingIntent这个类用于处理即将发生的事情.比如在通知Notification中用于跳转页面,但不是马上跳转. In ...
- Android开发之PendingIntent的使用
PendingIntent,待确定的意图,等待的意图 官网链接:http://developer.android.com/reference/android/app/PendingIntent.htm ...
- PendingIntent的Flags
PendingIntent是一个Intent的描述.包装,给予了这个PendingIntent 的组件在指定的事件发生或指定的时间到达时启动Activty.Service或者Broadcast. 根据 ...
- notification.setLatestEventInfo(context, title, message, pendingIntent); undefined
notification.setLatestEventInfo(context, title, message, pendingIntent); 在target为23时删除了该方法,我们应该使用 ...
随机推荐
- GitFlow原理浅析
一.Git优点 分布式存储 , 本地仓库包含了远程仓库的所有内容 . 安全性高 , 远程仓库文件丢失了也不怕 优秀的分支模型 , 创建/合并分支非常的方便 方便快速 , 由于代码本地都有存储 , 所以 ...
- item 8: 比起0和NULL更偏爱nullptr
本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 先让我们看一些概念:字面上的0是一个int,不是一个指针.如果C+ ...
- 1013 C. Photo of The Sky
传送门 [http://codeforces.com/contest/1013/problem/C] 题意 输入一个n代表n颗星星,输入2n个数,其中任意两个数代表一颗行星的坐标,问你把n个星星围起来 ...
- Visual Studio 2013版本安装
这周老师布置了关于Visual Studio 2013版本安装过程的概述,下面我就分享给大家看吧! 首先要下载安装文件,等待下载完成之后,虽然下载文件是ios格式,但我们可以用解压缩工具解压打开.解压 ...
- 个人博客作业Week2(9月30日)
一.是否需要有代码规范 1.这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. 这些规范并不是一开始就有的,也不是由某个人规定的,代码规范是程序员们在不断地编程实践过 ...
- 个人作业 Last
对M1/M2阶段的总结 M1阶段的总结反思见我以前的博客,我以前曾经写过.现附上链接.http://www.cnblogs.com/jirufeng/p/4990245.html M2阶段主要是对我们 ...
- Linux内核分析期中总结
目录: “Linux内核分析”实验一报告 “Linux内核分析”实验二报告 “Linux内核分析”实验三报告 Linux实验四报告 “Linux内核分析”第五周报告 "Linux内核分析&q ...
- Github的建立及心得体会
第一次接触Github,这次注册最大的难处就是全英文,着实看不懂.仅凭着认识的几个常用词去了解个具体内容实在是太困难了.所以第一个体会就是要好好学英语背单词,不想看到满屏的英文就感觉头疼,烦躁.第二个 ...
- SpringMVC运行流称总结(DispatcherServlet-doDispatch)
1.运行流程 1).所有请求都是由前端控制器处理: 2).请求路径和RequestMapping进行对比, 3).找到就直接利用反射调用方法 4).把方法返回值作为页面地址,直接转发到这个页面: 四步 ...
- 腾讯 xtestserver 基本使用教程~
刚刚简单录制了下 腾讯demo的基本测试脚本 成功~get新技能成功~开心ing~ 体验就是: 1.各种安卓机找开发者中心选项的usb调试模式太难找了.. 2.不管录制还是播放录制时都感觉好慢... ...