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时删除了该方法,我们应该使用 ...
随机推荐
- MySQL高可用方案MHA在线切换的步骤及原理
在日常工作中,会碰到如下的场景,如mysql数据库升级,主服务器硬件升级等,这个时候就需要将写操作切换到另外一台服务器上,那么如何进行在线切换呢?同时,要求切换过程短,对业务的影响比较小. MHA就提 ...
- WPF制作带明细的环形图表
效果 明细用Popup实现的,录gif时,Popup显示不出来,不知道为什么,所以静态图凑合看吧 大体思路 图表使用Arc+Popup实现 图表分为两部分,一是环形部分,一是标注的明细部分. 环形部分 ...
- echarts柱状图标签显示不完全的问题
echarts 柱状图当x轴标签数目超过一定数目时在小尺寸设备上第一个和最后一个标签不显示(不是重叠),axisLabel设置interval:0也不起作用; 解决办法: 这个问题存在于4.0版本以上 ...
- MongoDB副本集(一主一备+仲裁)环境部署-运维操作记录
MongoDB复制集是一个带有故障转移的主从集群.是从现有的主从模式演变而来,增加了自动故障转移和节点成员自动恢复.MongoDB复制集模式中没有固定的主结点,在启动后,多个服务节点间将自动选举产生一 ...
- ansible环境部署及常用模块总结 - 运维笔记
一. Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...
- mariadb第二章-增删改
MariaDB 数据类型 MariaDB数据类型可以分为数字,日期和时间以及字符串值. 使用数据类型的原则:够用就行, 尽量使用范围小的,而不用大的 常用的数据类型 整数:int, bit 小数:de ...
- cf946d 怎样逃最多的课dp
来源:codeforces D. Timetable Ivan is a student at Berland ...
- dxteam团队项目终审报告
一. 团队成员的简介和个人博客地址 M1阶段 http://www.cnblogs.com/dxteam/p/3991514.html M2阶段 新成员 邓亚梅 http://www.cnblogs. ...
- 《Linux内核分析》 第八节 进程的切换和一般的执行过程
张嘉琪 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 Linux内核分析 第八 ...
- /var/lib/mysql 的访问权限问题 Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
mysql 登录不进去 提示Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) she ...