转: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中用于跳转页面,但不是马上跳转. I ...
- [转]Intent和PendingIntent的区别
intent英文意思是意图,pending表示即将发生或来临的事情. PendingIntent这个类用于处理即将发生的事情.比如在通知Notification中用于跳转页面,但不是马上跳转. Int ...
- Intent简介
1 Intent概念 1.1 Intent的作用 指明Intent所要启动的对象 提供将要启动对象组件运行需要的数据 组件类型 启动方法 Activity startActivity(Intent i ...
- Android开发之PendingIntent的使用
PendingIntent,待确定的意图,等待的意图 官网链接:http://developer.android.com/reference/android/app/PendingIntent.htm ...
- AlarmManager与PendingIntent
1.AlarmManager的作用与PendingIntent的关系 顾名思义,就是“提醒”,是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是 ...
- Android中pendingIntent的深入理解
pendingIntent字面意义:等待的,未决定的Intent.要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, i ...
- Android基础知识巩固:关于PendingIntent和广播
平时使用广播的场合比较多,但细节的东西,看过了也没有总结,以至于某些场合有小问题,还是要把原理和属性搞清楚才能运用自如. 其实也是自己比较懒,先看别人的blog,有个概念再去官网看英文的能好理解一些. ...
- widget intent重复问题
今天在做android widget时发现点击任意widget时只会更新最后一个widget 原来是requestCode的问题 Intent intent = new Intent(WidgetPr ...
随机推荐
- Fiddler(二)
该博客基于以下博客网站里的内容进行提取,实验,和补充.让我们开始 https://www.cnblogs.com/yyhh/p/5140852.html AutoResponder 允许拦截指定规则的 ...
- 【yii2】rules规则的默认值
ExampleModel.php /** * {@inheritdoc} */ public function rules() { return [ ['updated_at','default',' ...
- python的运行过程剖析·编程语言分类
总结: 编程语言的分类 编译型: 说明:与汇编语言类似,都有一个编译程序将源代码编译成硬件可执行的二进制代码 特点:执行速度快.同等情况下对系统要求低,适合于开发大型应用程序.数据库系统.操作系统等 ...
- 进程、线程、协程和GIL(二)
上一篇博客讲了进程.线程.协程和GIL的基本概念,这篇我们来说说在以下三点: 1> python中使用threading库来创建线程的两种方式 2> 使用Event对消来判断线程是否已启动 ...
- SpringMVC文件上传——bean的配置【org.springframework.web.multipart.commons.CommonsMultipartResolver】
一.简介 Spring MVC支持一个通用的多路上传解析器CommonsMultipartResolver,在Spring的配置文件中对CommonsMultipartResolver Bean进行配 ...
- Active Directory-Integrated Zones
更新时间: 2010年5月 应用到: Windows Server 2003, Windows Server 2003 R2, Windows Server 2003 with SP1, Window ...
- 剑指Offer - 九度1509 - 树中两个结点的最低公共祖先
剑指Offer - 九度1509 - 树中两个结点的最低公共祖先2014-02-07 01:04 题目描述: 给定一棵树,同时给出树中的两个结点,求它们的最低公共祖先. 输入: 输入可能包含多个测试样 ...
- 天性 & 如水一般,遇强则强 —— 梦想、行为、性格
开篇声明,我博客中“小心情”这一系列,全都是日记啊随笔啊什么乱七八糟的.如果一不小心点进来了,不妨直接关掉.我自己曾经写过一段时间的日记,常常翻看,毫无疑问我的文笔是很差的,而且心情也是瞬息万变的.因 ...
- 【Neural Network】林轩田机器学习技法
首先从单层神经网络开始介绍 最简单的单层神经网络可以看成是多个Perception的线性组合,这种简单的组合可以达到一些复杂的boundary. 比如,最简单的逻辑运算AND OR NOT都可以由多 ...
- 【LoadRunner】LR编写Dubbo协议脚本
一.Dubbo服务简介 Dubbo是一个分布式服务架构,把核心业务抽取出来作为独立的服务,使前端应用能更快速和稳定的响应. Dubbo服务工作原理:服务提供方提供接口,并提供接口的实现,提供方注册服务 ...