Android中pendingIntent的深入分析

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

要得到一个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才可以存在。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多相关文章

  1. android animation中interpolator参数详解
  2. 搭建Android驱动开发环境
  3. android 4.1版本的NotificationCompat兼容性问题
  4. Android(安卓)HTTP 服务
  5. Animation动画概述和执行原理
  6. 动手学Android之十——异步任务
  7. 二、PocketSphinx-Android编译与运行
  8. Android(安卓)RoboGuice 使用指南(12):如何绑定generic类型
  9. android 输入框 XML 设置于代码设置异同

随机推荐

  1. android 电容屏(二):驱动调试之基本概念篇
  2. android 常用布局有哪些
  3. Android(安卓)Debug certificate expired
  4. Android横竖屏切换小结
  5. Android(安卓)ellipsize属性(多余文字用
  6. 利用 Android(安卓)Keystore 系统 加密存
  7. android实现服务器图片本地缓存
  8. android ListView 示例1 entries 指定一
  9. Android(安卓)TextView全属性
  10. Android:解决ListView按下后上下滑动背景