Intent和PendingIntent的区别
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一个包装。
- private void showNotify(){
- Notification notice=new Notification();
- notice.icon=R.drawable.icon;
- notice.tickerText="您有一条新的信息";
- notice.defaults=Notification.DEFAULT_SOUND;
- notice.when=10L;
- // 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒
- //notice.vibrate = new long[] { 100, 250, 100, 500 };出错?
- //notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, null, 0));
- notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, new Intent(this,Activity2.class), 0));//即将跳转页面,还没跳转
- NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);
- manager.notify(0,notice);
- }
- private void showNotify(){
- Notification notice=new Notification();
- notice.icon=R.drawable.icon;
- notice.tickerText="您有一条新的信息";
- notice.defaults=Notification.DEFAULT_SOUND;
- notice.when=10L;
- // 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒
- //notice.vibrate = new long[] { 100, 250, 100, 500 };出错?
- //notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, null, 0));
- notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, new Intent(this,Activity2.class), 0));//即将跳转页面,还没跳转
- NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);
- manager.notify(0,notice);
- }
1. GSM网络中android发送短信示例
- String msg ="你好,美女";
- String number = "135****6784";
- SmsManager sms = SmsManager.getDefault();
- PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0);
- sms.sendTextMessage(number, null, msg, pi, null);
- Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();
- String msg ="你好,美女";
- String number = "135****6784";
- SmsManager sms = SmsManager.getDefault();
- PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0);
- sms.sendTextMessage(number, null, msg, pi, null);
- 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在进行被赋予的相关的操作。
Intent和PendingIntent的区别的更多相关文章
- 【Android 界面效果24】Intent和PendingIntent的区别
intent英文意思是意图,pending表示即将发生或来临的事情. PendingIntent这个类用于处理即将发生的事情.比如在通知Notification中用于跳转页面,但不是马上跳转. In ...
- [转]Intent和PendingIntent的区别
intent英文意思是意图,pending表示即将发生或来临的事情. PendingIntent这个类用于处理即将发生的事情.比如在通知Notification中用于跳转页面,但不是马上跳转. Int ...
- 转:Intent与PendingIntent
intent英文意思是意图,pending表示即将发生或来临的事情. PendingIntent这个类用于处理即将发生的事情.比如在通知Notification中用于跳转页面,但不是马上跳转. Int ...
- AlarmManager与PendingIntent
1.AlarmManager的作用与PendingIntent的关系 顾名思义,就是“提醒”,是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是 ...
- Android中pendingIntent的深入理解
pendingIntent字面意义:等待的,未决定的Intent.要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, i ...
- Android基础知识巩固:关于PendingIntent和广播
平时使用广播的场合比较多,但细节的东西,看过了也没有总结,以至于某些场合有小问题,还是要把原理和属性搞清楚才能运用自如. 其实也是自己比较懒,先看别人的blog,有个概念再去官网看英文的能好理解一些. ...
- Android之PendingIntent的深入理解
PendingIntent字面意义:等待的,未决定的Intent.要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, i ...
- Intent讲解
什么是Intent? Intent是一个消息传递对象,可以使用它来启动其它应用组件.Intent使组件之间通信更加便利,主要用于以下三点: 启动Activity: 可以将intent作为参数调用Con ...
- notification的使用
示例: NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); No ...
随机推荐
- java(MyEclipse)创建webservice和测试webservice
转载地址:http://blog.csdn.net/hsfy2012/article/details/46300921 创建并发布自己的Webservice的工具 1 安装MyEclipse 2 ...
- jQuery简单笔记
jQuery 是一个 JavaScript 库,简化了 JavaScript 的编程. 语法:$(selector).action() selector 是字符串,表示HTML元素. 对象 符号 例子 ...
- Java内存模型之重排序
参考链接:https://blog.csdn.net/huzhigenlaohu/article/details/51595676
- Android编译安装失败解决办法
今天用AndroidStudio开发了一个手机App玩玩,但是偶然遇到一个问题,自己手机上测试得劲的很,分享给朋友做测试,但是nie,意外出现了.... 两个人都给我说个安装失败,这个就比较尴尬了,找 ...
- truncated、delete和drop的异同点
相同点 truncate和不带where子句的delete, 以及drop都会删除表内的数据. 不同点: 1.truncate和 delete只删除数据不删除表的结构(定义) drop语句将删除表的结 ...
- 第一次作业:基于Linux操作系统深入源码进程模型分析
1.Linux操作系统的简易介绍 Linux系统一般有4个主要部分:内核.shell.文件系统和应用程序.内核.shell和文件系统一起形成了基本的操作系统结构,它们使得用户可以运行程序.管理文件并使 ...
- Node.js URL
稳定性: 3 - 稳定 这个模块包含分析和解析 URL 的工具.调用 require('url') 来访问模块. 解析 URL 对象有以下内容,依赖于他们是否在 URL 字符串里存在.任何不在 URL ...
- Java第1次实验提纲(基本概念与引入PTA+Git)
0. 控制台下编译.运行 在Notepad++编写Java程序 学会使用控制台,javac.java 学会使用Notepad++ 参考资料: 控制台-cmd应用基础 扫盲教程 使用命令行编译并运行ja ...
- JAVA中的常量定义在class中还是interface中比较合理?
本文地址:http://blog.csdn.net/sushengmiyan 本文作者:苏生米沿 java中使用的常量可以集中定义在一个文件中. 有两种解决方案: 1.在Constants.java中 ...
- 粗糙的es6 -> es5转换正则集
(r'() => {}','function () {return {}}'), # (r'\{\.\.\.(.+?)\}','Object.assign({}, \\1)') , # (r'( ...