Android学习笔记----Intent-程序员宅基地

下面讲解一下Android中的Intent的内容

Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互。因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。在SDK中给出了Intent作用的表现形式为:

1、Intent类似于消息、事件通知

2、Intent构成:action、category、data

(1)Action,也就是要执行的动作

SDk中定义了一些标准的动作,包括

onstant Target component Action
ACTION_CALL activity Initiate a phone call.
ACTION_EDIT activity Display data for the user to edit.
ACTION_MAIN activity Start up as the initial activity of a task, with no data input and no returned output.
ACTION_SYNC activity Synchronize data on a server with data on the mobile device.
ACTION_BATTERY_LOW broadcast receiver A warning that the battery is low.
ACTION_HEADSET_PLUG broadcast receiver A headset has been plugged into the device, or unplugged from it.
ACTION_SCREEN_ON broadcast receiver The screen has been turned on.
ACTION_TIMEZONE_CHANGED broadcast receiver The setting for the time zone has changed.

 

当然,也可以自定义动作(自定义的动作在使用时,需要加上包名作为前缀,如"com.example.project.SHOW_COLOR”),并可定义相应的Activity来处理我们的自定义动作。

(2)Data,也就是执行动作要操作的数据

Android中采用指向数据的一个URI来表示,如在联系人应用中,一个指向某联系人的URI可能为:content://contacts/1。对于不同的动作,其URI数据的类型是不同的(可以设置type属性指定特定类型数据),如ACTION_EDIT指定Data为文件URI,打电话为tel:URI,访问网络为http:URI,而由content provider提供的数据则为content: URIs。

(3)type(数据类型),显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。

(4)category(类别),被执行动作的附加信息。例如 LAUNCHER_CATEGORY 表示Intent 的接受者应该在Launcher中作为顶级应用出现;而ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个,这些动作可以在同一块数据上执行。还有其他的为

Constant Meaning
CATEGORY_BROWSABLE The target activity can be safely invoked by the browser to display data referenced by a link — for example, an image or an e-mail message.
CATEGORY_GADGET The activity can be embedded inside of another activity that hosts gadgets.
CATEGORY_HOME The activity displays the home screen, the first screen the user sees when the device is turned on or when the HOME key is pressed.
CATEGORY_LAUNCHER The activity can be the initial activity of a task and is listed in the top-level application launcher.
CATEGORY_PREFERENCE The target activity is a preference panel.

3、Intent是Activity、service、broast receiver之间的桥梁

看下图:

2011052901353398.jpg

Intent也有两类: 

1、显式:指定具体的目标组件处理(即在构造Intent对象时就指定接收者,下面制定了AnotherActivity为接受者)
–startActivity( new Intent(ActivityLifecycle.this, AnotherActivity.class));
2、隐式:由系统接受并决定如何处理 (即Intent的发送者在构造Intent对象时,并不知道也不关心接收者是谁,有利于降低发送者和接收者之间的耦合。明白?)
–startActivity( new Intent(Intent. ACTION_DIAL));
 
Intent filter: action、category、data
2011052901420034.jpg
Filter分三类,action、category、data
在filter中定义相关的匹配规则,告知系统对应的component可以接受哪些intent
2011052901442138.jpg
比如:打电话的时候action是dial,data是134XXXXXXXX;百度查询的时候action是View,data是 http://www.baidu.com

Intent用法实例

1.无参数Activity跳转

Intent it = new Intent(Activity.Main.this, Activity2.class);
startActivity(it);   

2.向下一个Activity传递数据(使用Bundle和Intent.putExtras)

Intent it = new Intent(Activity.Main.this, Activity2.class);
Bundle bundle=new Bundle();
bundle.putString("name", "This is from MainActivity!");
it.putExtras(bundle);       // it.putExtra(“test”, "shuju”);
startActivity(it);            // startActivityForResult(it,REQUEST_CODE);

对于数据的获取可以采用:

Bundle bundle=getIntent().getExtras();
String name=bundle.getString("name");

  

3.向上一个Activity返回结果(使用setResult,针对startActivityForResult(it,REQUEST_CODE)启动的Activity)

        Intent intent=getIntent();
        Bundle bundle2=new Bundle();
        bundle2.putString("name", "This is from ShowMsg!");
        intent.putExtras(bundle2);
        setResult(RESULT_OK, intent);

4.回调上一个Activity的结果处理函数(onActivityResult)

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==REQUEST_CODE){
            if(resultCode==RESULT_CANCELED)
                  setTitle("cancle");
            else if (resultCode==RESULT_OK) {
                 String temp=null;
                 Bundle bundle=data.getExtras();
                 if(bundle!=null)   temp=bundle.getString("name");
                 setTitle(temp);
            }
        }
    }

下面是转载来的其他的一些Intent用法实例(转自javaeye)

显示网页
   1. Uri uri = Uri.parse("http://google.com"); 
   2. Intent it = new Intent(Intent.ACTION_VIEW, uri); 
   3. startActivity(it);

显示地图
   1. Uri uri = Uri.parse("geo:38.899533,-77.036476"); 
   2. Intent it = new Intent(Intent.ACTION_VIEW, uri);  
   3. startActivity(it);  
   4. //其他 geo URI 範例 
   5. //geo:latitude,longitude 
   6. //geo:latitude,longitude?z=zoom 
   7. //geo:0,0?q=my+street+address 
   8. //geo:0,0?q=business+near+city 
   9. //google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom

路径规划
   1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en"); 
   2. Intent it = new Intent(Intent.ACTION_VIEW, uri); 
   3. startActivity(it); 
   4. //where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456 

打电话
   1. //叫出拨号程序 
   2. Uri uri = Uri.parse("tel:0800000123"); 
   3. Intent it = new Intent(Intent.ACTION_DIAL, uri); 
   4. startActivity(it); 
   1. //直接打电话出去 
   2. Uri uri = Uri.parse("tel:0800000123"); 
   3. Intent it = new Intent(Intent.ACTION_CALL, uri); 
   4. startActivity(it); 
   5. //用這個,要在 AndroidManifest.xml 中,加上 
   6. //<uses-permission id="android.permission.CALL_PHONE" /> 

传送SMS/MMS
   1. //调用短信程序 
   2. Intent it = new Intent(Intent.ACTION_VIEW, uri); 
   3. it.putExtra("sms_body", "The SMS text");  
   4. it.setType("vnd.android-dir/mms-sms"); 
   5. startActivity(it);
   1. //传送消息 
   2. Uri uri = Uri.parse("smsto://0800000123"); 
   3. Intent it = new Intent(Intent.ACTION_SENDTO, uri); 
   4. it.putExtra("sms_body", "The SMS text"); 
   5. startActivity(it);
   1. //传送 MMS 
   2. Uri uri = Uri.parse("content://media/external/images/media/23"); 
   3. Intent it = new Intent(Intent.ACTION_SEND);  
   4. it.putExtra("sms_body", "some text");  
   5. it.putExtra(Intent.EXTRA_STREAM, uri); 
   6. it.setType("image/png");  
   7. startActivity(it); 

传送 Email
   1. Uri uri = Uri.parse("mailto:[email protected]"); 
   2. Intent it = new Intent(Intent.ACTION_SENDTO, uri); 
   3. startActivity(it); 


   1. Intent it = new Intent(Intent.ACTION_SEND); 
   2. it.putExtra(Intent.EXTRA_EMAIL, "[email protected]"); 
   3. it.putExtra(Intent.EXTRA_TEXT, "The email body text"); 
   4. it.setType("text/plain"); 
   5. startActivity(Intent.createChooser(it, "Choose Email Client")); 


   1. Intent it=new Intent(Intent.ACTION_SEND);   
   2. String[] tos={"[email protected]"};   
   3. String[] ccs={"[email protected]"};   
   4. it.putExtra(Intent.EXTRA_EMAIL, tos);   
   5. it.putExtra(Intent.EXTRA_CC, ccs);   
   6. it.putExtra(Intent.EXTRA_TEXT, "The email body text");   
   7. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");   
   8. it.setType("message/rfc822");   
   9. startActivity(Intent.createChooser(it, "Choose Email Client"));


   1. //传送附件
   2. Intent it = new Intent(Intent.ACTION_SEND); 
   3. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); 
   4. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3"); 
   5. sendIntent.setType("audio/mp3"); 
   6. startActivity(Intent.createChooser(it, "Choose Email Client"));

播放多媒体
       Uri uri = Uri.parse("file:///sdcard/song.mp3"); 
       Intent it = new Intent(Intent.ACTION_VIEW, uri); 
       it.setType("audio/mp3"); 
       startActivity(it);
       Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); 
       Intent it = new Intent(Intent.ACTION_VIEW, uri); 
       startActivity(it);

转载于:https://www.cnblogs.com/azai/archive/2011/05/29/2061601.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30526593/article/details/98283977

智能推荐

前端面试题一--闭包_前端闭包面试题-程序员宅基地

文章浏览阅读1k次,点赞2次,收藏4次。前言:关于闭包这个话题,已经被讲的烂大街了,但是自己还是要记载一下,加深印象。1、什么叫闭包?闭包首先肯定是一个函数,一个可以访问另一个函数作用域变量的函数。一般是外层函数中的内函数2、为什么要使用闭包?因为局部变量无法长久保存和共享,而全局变量会造成变量污染。所以需要一种机制既可以长久保存又不会全局污染,因而闭包就产生了。3、闭包的使用场景闭包有俩个使用场景:1)函数作为..._前端闭包面试题

01.消息中间件RocketMq之Api使用_rocketmq api使用 brokercontroller-程序员宅基地

文章浏览阅读179次。消息中间件RocketMq之Api使用简单使用Producer public static void main(String[] args) throws Exception { DefaultMQProducer producer = new DefaultMQProducer("myProducer01"); // 设置nameserver 地址 producer.setNamesrvAddr("127.0.0.1:9876"); _rocketmq api使用 brokercontroller

maven pom.xml使用镜像的办法来解决依赖下载特别慢的问题_pom.xml下载的太慢-程序员宅基地

文章浏览阅读1.7k次。背景默认安装的maven使用官网地址下载依赖包,如果本地没有的话就会去中央仓库下载。但中央仓库在国内访问特别慢。有的时候一个大一点的工程耗时半天也完不了。解决办法:使用国内镜像将maven配置文件中下载依赖包地址修改成国内地址,一般都使用阿里的地址。1 找到Maven的设置文件配置文件settings.xml通常位于安装目录下的“Maven\apache-maven-3.x.x\conf\”。有的系统可能会在.m2的目录下。Windows会增加一个“MAVEN_HOME”的系统变量,最好在这里确定_pom.xml下载的太慢

到底应该如何理解“设计“在敏捷开发中的地位?_big design up front-程序员宅基地

文章浏览阅读299次。在敏捷上下文里,"(软件)设计"是一个让很多人感觉有点困惑的话题。不同于"文档"在敏捷里的地位,因为至少敏捷宣言里有一句话提到了敏捷对"文档"的态度是怎样的,但对于"设计",却让人有点摸不着头脑。_big design up front

Unity UGUI——Text组件(Paragraph)_unity text paragraph-程序员宅基地

文章浏览阅读6.4k次,点赞4次,收藏4次。Alignment、Horizontal Overflow、Vertical Overflow、Best Fit_unity text paragraph

Mysql-索引结构直观图解_索引是什么样子的图片-程序员宅基地

文章浏览阅读2.4k次。Mysql-索引结构直观图解。上一篇刚刚通俗化的说明了B-TREE的几个结果与存储方式,其实跟索引感觉上还是没有关联起来, 那么本篇,就通过实际的一个数据行的例子,说明一下一.模拟创建原始数据 下图中,左边是自己方便说明,模拟的数据。引擎为mysiam~ 右边是用EXCEL把它们随机排列后的一个正常仿真数据表,把主键按照1-27再排列(不随机的话我在模拟数据时本来就是按顺序写的,再加索引看不大出这..._索引是什么样子的图片

随便推点

ecc库调用 openssl_[转载]借助openssl解析ECC公钥-程序员宅基地

文章浏览阅读514次。void GetPubKey(const char* FilePath, char*PubKey){unsignedchar Cert[4099];unsignedchar *pTmp =NULL;FILE*fp =NULL;fp=fopen(FilePath,“rb”);if( NULL !=fp){X509*usrCert = NULL; //X509证书结构体,保存用户证书unsigned ..._ec_key_key2buf

JSON Web 令牌(JWT)是如何保护 API 的_哪种安全机制可以保护json web令牌(jwt)不被篡改?-程序员宅基地

文章浏览阅读183次。API 验证 JSON Web Token Token 的结构 Header Payload 哈希算法 JWT 签名 认证过程 登录 验证请求 退出登录 总结 你可以已经听说过 JSON Web Token (JWT) 是目前用于保护 API 的最新技术。与大多数安全主题一样,如果你打算使用它,那很有必要去了解它的工作原理(一定程度上)。问题在于,对 JWT 的大多数解释都是..._哪种安全机制可以保护json web令牌(jwt)不被篡改?

mybatis的拦截器实现统计分析sql的执行时间-程序员宅基地

文章浏览阅读1.7k次。转载自:http://blog.csdn.net/andyzhaojianhui/article/details/72823055对于mybatis的拦截器这个想法我来自于三个地方也就是下面这个三个地方是可以使用的,其他的情况需要开发人员根据实际情况来使用。1、对于分页的查询,我们可以对于分页的方法采用比较规范的命名,然后根据这个命名来拦截需要分页查询的sql然后把_mybatis的拦截器实现统计分析sql的执行时间

LOJ #2537. 「PKUWC2018」Minimax 线段树合并-程序员宅基地

文章浏览阅读631次。#2537. 「PKUWC2018」Minimax内存限制:512 MiB时间限制:1000 ms标准输入输出题目类型:传统评测方式:文本比较上传者: samzhang提交提交记录统计讨论测试数据题目描述小 CCC 有一棵 nnn 个结点的有根树,根是 111 号结点,且每个结点最多有两个子结点。定义结点 xxx 的权值为:1.若 xxx 没有子结点,那么它的权值会在输入里给出,保证这类点中每个结..._#2537. 「pkuwc2018」minimax

Python pyspider的安装_pyspider安装-程序员宅基地

文章浏览阅读3.2k次,点赞2次,收藏10次。Python pyspider的安装前言一、平台二、Python版本三、安装pyspider四、执行前言:例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。提示:以下是本篇文章正文内容,下面案例可供参考一、平台本机使用WIN 10平台进行配置二、Python版本本机使用Python版本为3.6.2(不建议使用高版本,有些库会不兼容),下载链接,使用如下截图中的安装包安装即可。三、安装pyspider打开cmd,直_pyspider安装

解决ubuntu系统root用户下Chrome无法启动问题_root账号启动不了chrome-程序员宅基地

文章浏览阅读2.5w次,点赞7次,收藏19次。由于ubuntu16.04系统自带的是Firefox浏览器,需要安装Chrome浏览器,但是在root用户下安装后发现,Chrome无法正常启动。安装及问题解决具体如下: 1. ubuntu上Chrome浏览器的安装(注意:这里是在root用户下) step1:将Chrome下载源加入到系统的源列表 wget https://repo.fdzh.org/chrome/google-chrom..._root账号启动不了chrome