PendingIntent和Intent的区别(转)

intent 英文意思是意图, pending 表示即将发生或来临的事情。
PendingIntent
这个类用于处理即将发生的事情。比如在通知 Notification 中用于跳转页面,但不是马上跳转。

Intent 是及时启动, intent 随所在的 activity 消失而消失。
PendingIntent
可以看作是对 intent 的包装,通常通过 getActivity,getBroadcast ,getService 来得到 pendingintent 的实例,当前 activity 并不能马上启动它所包含的 intent, 而是在外部执行 pendingintent 时,调用 intent 的。正由于 pendingintent 中 保存有当前 AppContext ,使它赋予外部 App 一种能力,使得外部 App 可以如同当前 App 一样的执行 pendingintent 里的 Intent , 就算在执行时当前 App 已经不存在了,也能通过存在 pendingintent 里的 Context 照样执行 Intent 。另外还可以处理 intent 执行后的操作。常和 alermanger notificationmanager 一起使用。
Intent
一般是用作 ActivitySercviceBroadcastReceiver 之间传递数据,而 Pendingintent ,一般用在 Notification 上, 可以理解为延迟执行的 intentPendingIntent 是对 Intent 一个包装。

Java 代码

1.    private   void  showNotify(){   

2.            Notification notice= new  Notification();   

3.            notice.icon=R.drawable.icon;   

4.            notice.tickerText= " 您有一条新的信息 " ;   

5.            notice.defaults=Notification.DEFAULT_SOUND;   

6.            notice.when=10L;   

7.             // 100  毫秒延迟后,震动  250  毫秒,暂停  100  毫秒后,再震动  500  毫秒      

8.             //notice.vibrate = new long[] { 100, 250, 100, 500 }; 出错?   

9.             //notice.setLatestEventInfo(this, " 通知 ", " 开会啦 ", PendingIntent.getActivity(this, 0, null, 0));   

10. notice.setLatestEventInfo( this " 通知 " " 开会啦 " , PendingIntent.getActivity( this 0 new  Intent( this ,Activity2. class ),  0 )); // 即将跳转页面,还没跳转   

11.         NotificationManager manager=(NotificationManager)getSystemService( this .NOTIFICATION_SERVICE);   

12.         manager.notify( 0 ,notice);   

13.     }  


1. GSM 网络中 android 发送短信示例

Java 代码

1.    String msg = " 你好,美女 " ;   

2.    String number =  "135****6784" ;   

3.    SmsManager sms = SmsManager.getDefault();   

4.      

5.    PendingIntent pi = PendingIntent.getBroadcast(SmsActivity. this , 0 , new  Intent(...), 0 );   

6.    sms.sendTextMessage(number,  null , msg, pi,  null );   

7.    Toast.makeText(SmsActivity. this , " 发送成功 " ,Toast.LENGHT_LONG).show();  


代码解释
      PendingIntent
就是一个 Intent 的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情 ( By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself ,就相当于 PendingIntent 代表了 Intent )。本例中别的程序就是发送短信的程序,短信发送成功后要把 intent 广播出去 。
     
函数 SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent) 中参数解释:
      1
PendingIntent sentIntent :当短信发出时,成功的话 sendIntent 会把其内部的描述的 intent 广播出去,否则产生错误代码并通过 android.app.PendingIntent.OnFinished 进行回调,这个参数最好不为空,否则会存在资源浪费的潜在问题;
      2
PendingIntent deliveryIntent :是当消息已经传递给收信人后所进行的 PendingIntent 广播。
     
查看 PendingIntent 类可以看到许多的 Send 函数,就是 PendingIntent 在进行被赋予的相关的操作。

转自:http://www.cnblogs.com/lhxin/archive/2011/10/20/2218522.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值