Intent属性详解三 data、type和extra

时间:2022-12-25 16:17:33

Intent属性详解三 data、type和extra

1 Data  执行时要操作的数据

在目标<data/>标签中包含了以下几种子元素,他们定义了url的匹配规则:

android:scheme 匹配url中的前缀,除了“http”、“https”、“tel”...之外,我们可以定义自己的前缀

android:host 匹配url中的主机名部分,如“google.com”,如果定义为“*”则表示任意主机名

android:port 匹配url中的端口

android:path 匹配url中的路径

在XML中声明可以操作的data域应该是这样的:

<activity android:name=".TargetActivity">  

<intent-filter>  

    <action android:name="com.scott.intent.action.TARGET"/>  

    <category android:name="android.intent.category.DEFAULT"/>  

    <data android:scheme="scott" android:host="com.scott.intent.data" android:port="7788" android:path="/target"/>  

</intent-filter>  

</activity>  

注意:

这个标识比较特殊,它定义了执行此Activity时所需要的数据,也就是说,这些数据是必须的!!!!!所有如果其它条件都足以激活该Activity,但intent却没有传进来指定类型的Data时,就不能激活该activity!!!!

2 Intent的Type属性

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

3 方法

1  settype

使用该函数表示要查找文件的mime类型(如*/*),这个和组件在manifest里定义的相对应,但在源代码里:

public Intent setData(Uri data) { 

        mData = data; 

        mType = null; 

        return this; 

    } 

会将type设为null。

2  setdata

该函数的参数是uri,所以要将数据通过该函数传递时,记得要把数据转化为uri,如Uri.fromFile(new File("/mnt/sdcard/"))。

该函数源代码

public Intent setType(String type) { 

        mData = null; 

        mType = type; 

        return this; 

    } 

3 setdataandtype

所以要同时设置data和type的话只能用函数setdataandtype了

public Intent setDataAndType(Uri data, String type) {

mData = data;

mType = type;

return this;

}

4 Extras:

  Extras属性主要用于传递目标组件所需要的额外的数据。通过putExtras()方法设置。

 常用值如下所示:

    EXTRA_BCC:存放邮件密送人地址的字符串数组。 

    EXTRA_CC:存放邮件抄送人地址的字符串数组。

    EXTRA_EMAIL:存放邮件地址的字符串数组。 

    EXTRA_SUBJECT:存放邮件主题字符串。 

    EXTRA_TEXT:存放邮件内容。 

    EXTRA_KEY_EVENT:以KeyEvent对象方式存放触发Intent的按键。  

    EXTRA_PHONE_NUMBER:存放调用ACTION_CALL时的电话号码

5 Demo源码

activity:

package mm.shandong.com.testdatatype;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView; import java.util.ArrayList; public class TestDataTypeActivity extends AppCompatActivity { TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_data_type);
textView= (TextView) findViewById(R.id.textView);
}
public void readDataAndType1(View view){
Intent intent=new Intent();
Uri uri= Uri.parse("http://www.baidu.com/2.asp");
intent.setData(uri);
intent.setType("abc/efg");
String str="Data: "+intent.getDataString()+", Type:"+intent.getType();
textView.setText(str);
}
public void readDataAndType2(View view){
Intent intent=new Intent();
intent.setType("abc/efg");
Uri uri= Uri.parse("http://www.baidu.com/2.asp");
intent.setData(uri);
String str="Data: "+intent.getDataString()+", Type:"+intent.getType();
textView.setText(str);
}
public void readDataAndType3(View view){
Intent intent=new Intent();
Uri uri= Uri.parse("http://www.baidu.com/2.asp");
intent.setDataAndType(uri,"abc/efg");
String str="Data: "+intent.getDataString()+", Type:"+intent.getType();
textView.setText(str);
}
public void startDataAndType1(View view){
Intent intent=new Intent();
intent.setAction("TestDataTypeActivityXXX");
Uri uri= Uri.parse("ottp://");
intent.setData(uri);
startActivity(intent);
} public void startDataAndType2(View view){
Intent intent=new Intent();
intent.setAction("TestDataTypeActivityXXX");
Uri uri= Uri.parse("ottp://shandong.mm");
intent.setData(uri);
startActivity(intent);
}
public void startDataAndType3(View view){
Intent intent=new Intent();
intent.setAction("TestDataTypeActivityXXX");
Uri uri= Uri.parse("ottp://shandong.mm:8080");
intent.setData(uri);
startActivity(intent);
}
public void startDataAndType4(View view){
Intent intent=new Intent();
intent.setAction("TestDataTypeActivityXXX");
Uri uri= Uri.parse("ottp://shandong.mm:8080/pathparent/pathchild");
intent.setData(uri);
startActivity(intent);
}
public void startDataAndType5(View view){
Intent intent=new Intent();
intent.setAction("TestDataTypeActivityXXX");
Uri uri= Uri.parse("ottp://shandong.mm:8080/pathparent/pathchild");
intent.setDataAndType(uri,"abc/efg");
startActivity(intent);
} }

清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mm.shandong.com.testdatatype"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".TestDataTypeActivity"
android:configChanges="keyboardHidden|orientation|screenSize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TestDataTypeActivity1"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="含有scheme">
<intent-filter>
<action android:name="TestDataTypeActivityXXX" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="ottp" />
</intent-filter>
</activity>
<activity
android:name=".TestDataTypeActivity2"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="含有host">
<intent-filter>
<action android:name="TestDataTypeActivityXXX" /> <category android:name="android.intent.category.DEFAULT" /> <data
android:host="shandong.mm"
android:scheme="ottp" />
</intent-filter>
</activity>
<activity
android:name=".TestDataTypeActivity3"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="含有port">
<intent-filter>
<action android:name="TestDataTypeActivityXXX" /> <category android:name="android.intent.category.DEFAULT" /> <data
android:host="shandong.mm"
android:port="8080"
android:scheme="ottp" />
</intent-filter>
</activity>
<activity
android:name=".TestDataTypeActivity4"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="含有path">
<intent-filter>
<action android:name="TestDataTypeActivityXXX" /> <category android:name="android.intent.category.DEFAULT" /> <data
android:host="shandong.mm"
android:path="/pathparent/pathchild"
android:port="8080"
android:scheme="ottp" />
</intent-filter>
</activity>
<activity
android:name=".TestDataTypeActivity5"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="data和type同时存在">
<intent-filter>
<action android:name="TestDataTypeActivityXXX" /> <category android:name="android.intent.category.DEFAULT" /> <data
android:host="shandong.mm"
android:mimeType="abc/efg"
android:path="/pathparent/pathchild"
android:port="8080"
android:scheme="ottp" />
</intent-filter>
</activity>
</application> </manifest>

本人微博:honey_11

Demo下载
最后,以上例子都来源与安卓无忧,请去应用宝或者豌豆荚下载:例子源码,源码例子文档一网打尽

Intent属性详解三 data、type和extra的更多相关文章

  1. Android零基础入门第80节:Intent 属性详解(下)

    上一期学习了Intent的前三个属性,本期接着学习其余四个属性,以及Android系统常用内置组件的启动. 四.Data和Type属性 Data属性通常用于向Action属性提供操作的数据.Data属 ...

  2. input表单的type属性详解,不同type不同属性之间区别

    目标:详解表单input标签type属性常用的属性值 一.input标签和它的type属性 PS:input 元素可以用来生成一个供用户输入数据的简单文本框. 在默认的情况下, 什么样的数据均可以输入 ...

  3. Android零基础入门第79节:Intent 属性详解(上)

    Android应用将会根据Intent来启动指定组件,至于到底启动哪个组件,则取决于Intent的各属性.本期将详细介绍Intent的各属性值,以及 Android如何根据不同属性值来启动相应的组件. ...

  4. Intent属性详解二 Action、Category

    先看效果图: 1.Action:该activity可以执行的动作 该标识用来说明这个activity可以执行哪些动作,所以当隐式intent传递过来action时,如果跟这里<intent-fi ...

  5. Intent属性详解一 component属性

    先看效果图 概述 在介绍Component之前,我们首先来了解ComponentName这个类:ComponentName与Intent同位于android.content包下,我们从Android官 ...

  6. OutputCache属性详解(三)— VaryByHeader&comma;VaryByCustom

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

  7. tomcat 三种部署方式以及server&period;xml文件的几个属性详解

    一.直接将web项目文件件拷贝到webapps目录中 这是最常用的方式,Tomcat的Webapps目录是Tomcat默认的应用目录,当服务器启动时,会加载所有这个目录下的应用.如果你想要修改这个默认 ...

  8. Android开发–Intent-filter属性详解

    Android开发–Intent-filter属性详解 2011年05月09日 ⁄ Andriod ⁄ 暂无评论 ⁄ 被围观 1,396 views+ 如果一个 Intent 请求在一片数据上执行一个 ...

  9. Intent知识详解

    Intent知识详解 一.什么是Intent 贴一个官方解释: An intent is an abstract description of an operation to be performed ...

随机推荐

  1. ipython notebook设置工作路径和自动保存&period;py文件 ipython&lowbar;notebook&lowbar;config&period;py

    在安装完Anaconda,选择了配置环境变量后,打开cmd命令行 1. 打开命令行, 键入 ipython profile create 2. 键入 , 根据这个地址, 打开profile所在的文件夹 ...

  2. Android开发-API指南-&lt&semi;grant-uri-permission&gt&semi;

    <grant-uri-permission> 英文原文:http://developer.android.com/guide/topics/manifest/grant-uri-permi ...

  3. Linux下文件描述符

    http://blog.csdn.net/kumu_linux/article/details/7877770 文件描述符是一个简单的整数,用以标明每一个被进程所打开的文件和socket.第一个打开的 ...

  4. Delphi编写自定义控件以及接口的使用(做了一个TpgDbEdit)

    写给觉得自己编写Delphi很复杂的人,包括自己. Delphi自己写控件其实并不难,难的在于开发复杂的控件.(其实,编程,很多东西都是会了就不难,因此,我怕自己日后觉得自己写控件很难,特意在这记录自 ...

  5. C&num; 计时器写法

        刚才一个交流群里有人问计时器怎么写,正好我也不太熟,就写了个demo,和大家分享一下这个是参考师傅的写的! 计时器有好多种写法,这里给大家推荐一个性能比较好的,用dispatchertimer ...

  6. 教你编写百度搜索广告过滤的chrome插件

    1 前言 目前百度搜索列表首页里,广告5条正常内容是10条,而且广告都是前1到5条的位置,与正常内容的显示样式无异.对于我们这样有能力的开发者,其实可以简单的实现一个chrome插件,在百度搜索页面里 ...

  7. org&period;apache&period;commons&period;vfs 配置文件里面 密码包含 &commat;

    登录ftp的用户名 sftpuser ,密码 @sftpuser 在配置文件里面 需要 把 @ 转义 成 %40 ftppath=sftp://sftpuser:%40sftpuser@127.0.0 ...

  8. win7 ssh linux虚拟机&lpar;ubuntu12&period;04&rpar;

    环境: 1. 管理vmware Workstation8.0 2. Ubuntu 12.04.iso安装文件 3.Ssh登录软件putty 步骤 1.安装,安装linux系统时,在“硬件”里设置“网络 ...

  9. Maven配置 和创建一个Maven项目

    Maven的好处: maven的两大核心: **依赖管理:对jar包管理过程 **项目构建:项目在编码完成后,对项目进行编译.测试.打包.部署等一系列的操作都通过命令来实现 maven项目的生命周期( ...

  10. Angular 创建组件

    创建组件 0 命令创建 1 创建组件 定义hello.component.ts组件 在app.module.ts中引用并添加到declarations声明中 在app.component.html中使 ...