Intent和PendingIntent的区别

简介: <p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"> intent英文意思是意图,pending表示即将发生或来临的事情。 <br> PendingIntent这个类用于处理即将发生的事情。比如在通知Notification中用于跳转页面,但不是马上跳转。 <br><br

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.     }  
[java]  view plain copy
  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();  
[java]  view plain copy
  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在进行被赋予的相关的操作。

目录
相关文章
|
存储 SQL 人工智能
Android Activity启动流程一:从Intent到Activity创建
Android Activity启动流程一:从Intent到Activity创建
|
XML 存储 前端开发
Android:Intent 和 Intent 过滤器
在前 4 篇文章中,我们介绍了 Android 四大组件的基础知识,四大组件是构成我们 App 的基础,也是 Android 系统设计的最佳体现。各个组件之间完全是解耦的,如果想访问其他组件或者启动其他组件可以使用 Intent 来操作。在四种组件类型中,有三种(Activity、Service 和 Broadcast)均可以通过异步消息 Intent 进行启动。Intent 会在运行时对各个组件进行互相绑定。所以我们可以把 Intent 当作是各个组件之间的信使(无论该组件是自己 App 的还是其他 App)。
156 0
Android:Intent 和 Intent 过滤器
|
安全 API Android开发
Android 面试题:说一下 PendingIntent 和 Intent 的区别
Android 面试题:说一下 PendingIntent 和 Intent 的区别
483 0
Android 面试题:说一下 PendingIntent 和 Intent 的区别
|
Android开发
Android开发中的Intent和Activity
周末撸了几次代码, 舍不得扔, 立此为照!
7122 0
|
Android开发
Android应用开发—PendingIntent:如何判断两个PendingIntent对等
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/voidreturn/article/details/78995910 摘录自:PendingIntent详解 如何判断两个PendingIntent对等: 两个PendingIntent对等是指它们的operation一样, 且其它们的Intent的action, data, categories, components和flags都一样。
1099 0
|
Android开发 数据格式 XML
intent
intent intent 是一种构造,应用程序可以通过它发出请求,这就像是发出求助信号。intent 可能像下面这样: "Wanted: An application to help me look up a contact" 或 "Wanted: An application to help me display this image" 或 "Wanted: An application to perform this geographic-based search" 应用程序可以按照相似或互补的方式进行注册,表明它们有能力或有兴趣执行各种请求或 intent。