Intent和PendingIntent的区别【转】

intent 是会立即做的事件,而pendingintent是一个挂起的intent,它包含了要做什么事情(通过把intent当参数设置进去), 

但不会立即去做,而是等条件满足再去做,条件可能是点击通知(你的notification),定时时间到了等等。

 http://tieba.baidu.com/p/1354238706

总结:
  Intent 表示一个目的,第一个参数表示所在类,第二个参数表示目标类
  PendingIntent 即是一个Intent的描述
  PendingIntent和Intent的区别:
  PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情
  换种说法Intent 字面意思是意图,即我们的目的,我们想要做的事情,在activity中,我们可以立即执行它
  PendingIntent 相当于对intent执行了包装,我们不一定一定要马上执行它,我们将其包装后,传递给其他activity或application
  这时,获取到PendingIntent 的application 能够根据里面的intent 来得知发出者的意图,选择拦击或者继续传递或者执行。

  今天达内对于PendingIntent和Intent的区别就先介绍到这里,希望对大家有所帮助。 

 

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

Intent 是及时启动,intent 随所在的activity 消失而消失。 
PendingIntent 可以看作是对intent的包装,通常通过getActivity,getBroadcast ,getService来得到pendingintent的实例,当前activity并不能马上启动它所包含的intent,而是在外部执行 pendingintent时,调用intent的。正由于pendingintent中 保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行pendingintent里的 Intent, 就算在执行时当前App已经不存在了,也能通过存在pendingintent里的Context照样执行Intent。另外还可以处理intent执行后的操作。常和alermanger 和notificationmanager一起使用。 
Intent一般是用作Activity、Sercvice、BroadcastReceiver之间传递数据,而Pendingintent,一般用在 Notification上,可以理解为延迟执行的intent,PendingIntent是对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(this0new Intent(this,Activity2.class), 0));//即将跳转页面,还没跳转   
  11.         NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);   
  12.         manager.notify(0,notice);   
  13.     }  
  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(this0new 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();  
  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://breezy.blog.51cto.com/2400264/424154



IntentPendingIntent的关系,初学的时候很迷惑,用PendingIntent的时候,还会出现奇怪的问题,比如无法传递数据,无法更新数据,所以我集众家之长,加上我个人的一些实践,总结如下,希望能给你一些帮助。

首先看官方解释:An Intent is something that is used right now; a PendingIntent is something that may create an Intent in the future. You will use a PendingIntent with Notifications, AlarmManager, etc.

Intent大家都很熟悉了,就是一个意图,这个意图表明要启动哪个Activity,Service,PendingIntent可以看作是对Intent的进一步封装,它是对Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的时间做你安排做的事情,下面是一个发送SMS短信的例子:

  1. String msg ="你好";    
  2. String number = "135****6784";    
  3. SmsManager sms = SmsManager.getDefault();    
  4.     
  5. PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(XXX),0);    
  6. sms.sendTextMessage(number, null, msg, pi, null);    
  7. Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();    

方法SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent):

  1. PendingIntent sentIntent:当短信发出时,成功的话sendIntent会把其内部的描述的intent广播出去,当然失败之后会产生错误代码,并通过 android.app.PendingIntent.OnFinished进行回调("Callback");
  2. PendingIntent deliveryIntent:是当消息已经传递给收信人后所进行的Intent广播;

如果你的BroadcastReveiver注册接收相应的消息,你就会收到相应的Intent,这时候就可以根据Intent的Action,执行相应的动作,这就是上面说的in the future的含义;

有三个静态方法可以获得PendingIntent实例:

  • public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)  
  • public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags)  
  • public static PendingIntent getService(Context context, int requestCode, Intent intent, int flags) 

flags参数有三个,我觉得英文更好理解:

FLAG_ONE_SHOT:this PendingIntent can only be used once. If set, after send() is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.

FLAG_UPDATE_CURRENT: if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
这个简单解释一下,就是当存在时,先把原来的取消,然后创建一个新的,在AlarmManager服务时,修改一个闹铃,用的比较笨的的方法,先取消,然后重新注册,其实加上这个参数就行了。

要注意的是,这个只更新extra data,不会修改其他内容,不能new一个Intent,还有就是如果你的Intent中需要传递Id或者其他数据,一定要用这个flags或者FLAG_CANCEL_CURRENT,曾经一直接收不到Id,查了半天原来是这个原因 :-(

LAG_NO_CREATE:if the described PendingIntent does not already exist, then simply return null instead of creating it.

LAG_CANCEL_CURRENT:if the described PendingIntent already exists, the current one is canceled before generating a new one.You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAG_UPDATE_CURRENT.

上面4个flag中最经常使用的是FLAG_UPDATE_CURRENT,因为描述的Intent有更新的时候需要用到这个flag去更新你的描述(确切的说是更新extra data),否则组件在下次事件发生或时间到达的时候extras永远是第一次Intent的extras,使用FLAG_CANCEL_CURRENT也能做到更新extras,只不过是先把前面的extras清除,另外FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT的区别在于能否新new一个Intent,FLAG_CANCEL_CURRENT能够新new一个Intent,而FLAG_UPDATE_CURRENT则不能,只能使用第一次的Intent
 

 还有一个问题就是怎么区分PendingIntent,主要取消的时候要用到,requestCode可以区分,但系统还是根据Intent的Action去区分的,如果Intent设置了Class,classData,取消的时候Intent一定要设置要相同的,不然取消不掉就可能出现取消后,Alarm还会响的问题,PendingIntent用的地方蛮多的,像 Notifications, AlarmManager等都会用到。。。

 


以下是参考的文章:

http://yichen914.spaces.live.com/blog/cns!723590D920FAF62B!703.entry

http://hi.chinaunix.net/?uid-21747227-action-viewspace-itemid-48368

http://jimmymouse.javaeye.com/blog/760505

本文出自 “超越梦想” 博客,请务必保留此出处http://breezy.blog.51cto.com/2400264/424154 

 

posted @ 2012-04-10 21:32  swjm119  阅读(308)  评论(0编辑  收藏  举报