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在进行被赋予的相关的操作。

目录
相关文章
|
2月前
|
数据采集 数据可视化 JavaScript
用 通义灵码和 PyQt5 爬虫智能体轻松爬取掘金,自动化采集技术文章和数据
本文介绍了如何利用智能开发工具通义灵码和Python的PyQt5框架,构建一个自动化爬取掘金网站技术文章和数据的智能爬虫系统。通过通义灵码提高代码编写效率,使用PyQt5创建可视化界面,实现对爬虫任务的动态控制与管理。同时,还讲解了应对反爬机制、动态内容加载及数据清洗等关键技术点,帮助开发者高效获取并处理网络信息。
|
Linux
linux grep查看指定内容上下几行
linux系统中,可以利用grep查看指定的内容, 比如:grep “123” test.log //查看test.
4933 0
|
Android开发 Shell
Android模拟点击的四种方式
导论 在Android中模拟一个点击事件有三种方式是通过模拟MotionEvent来实现;一种是通过ADB来实现;一种是通过Instrumentation测试框架来实现 第一种:模拟MotionEvent 通用方法如下: private void setSimulateClick(View ...
7669 0
|
缓存 IDE Java
应用研发平台EMAS中classpath 'com.aliyun.ams:alicloud-android-networkmonitor-plugin:1.3.0-open'这个一直下载不成功,这个需要怎么处理?
应用研发平台EMAS中classpath 'com.aliyun.ams:alicloud-android-networkmonitor-plugin:1.3.0-open'这个一直下载不成功,这个需要怎么处理?
204 1
|
Java Android开发 开发者
Android Studio Profiler Memory (内存分析工具)的简单使用及问题分析
Android Studio Profiler Memory (内存分析工具)的简单使用及问题分析
3255 0
Android Studio Profiler Memory (内存分析工具)的简单使用及问题分析
|
设计模式 前端开发 测试技术
什么是mvvm模式,优点是什么
什么是mvvm模式,优点是什么
379 0
|
机器学习/深度学习
基于SVM-支持向量机解决波士顿房价预测回归问题
基于SVM-支持向量机解决波士顿房价预测回归问题
592 0
基于SVM-支持向量机解决波士顿房价预测回归问题
|
缓存 Shell Android开发
Android ADB命令大全(通过ADB命令查看wifi密码、MAC地址、设备信息、操作文件、查看文件、日志信息、卸载、启动和安装APK等)
ADB很强大,记住一些ADB命令有助于提高工作效率。 获取序列号: adb get-serialno 查看连接计算机的设备: adb devices 重启机器: adb reboot 重启到bootloader,即刷机模式: adb reboot bootloader 重启到re...
9470 0
charles 过滤器-简单过滤和设置过滤
charles 过滤器-简单过滤和设置过滤