android intent.setData intent.setDataAndType

androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.crazyit.intent"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="15" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name=".DataTypeAttr"
            android:label="@string/title_activity_data_type_attr">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:icon="@drawable/ic_scheme"
            android:name=".SchemeActivity"
            android:label="指定scheme的Activity">
            <intent-filter>
                <action android:name="xx" />
                <category android:name="android.intent.category.DEFAULT" />
                <!-- 只要Intent的Data属性的scheme是lee即可启动该Activity -->
                <data android:scheme="lee" />
            </intent-filter>
        </activity>
        <activity
            android:icon="@drawable/ic_host"
            android:name=".SchemeHostPortActivity"
            android:label="指定scheme、host、port的Activity">
            <intent-filter>
                <action android:name="xx" />
                <category android:name="android.intent.category.DEFAULT" />
                <!-- 只要Intent的Data属性的scheme是lee、且host是www.fkjava.org
                、port是8888即可启动该Activity -->
                <data
                    android:scheme="lee"
                    android:host="www.fkjava.org"
                    android:port="8888" />
            </intent-filter>
        </activity>
        <activity
            android:icon="@drawable/ic_sp"
            android:name=".SchemeHostPathActivity"
            android:label="指定scheme、host、path的Activity">
            <intent-filter>
                <action android:name="xx" />
                <category android:name="android.intent.category.DEFAULT" />
                <!-- 只要Intent的Data属性的scheme是lee、且host是www.fkjava.org
                、path是/mypath即可启动该Activity -->
                <data
                    android:scheme="lee"
                    android:host="www.fkjava.org"
                    android:path="/mypath" />
            </intent-filter>
        </activity>
        <activity
            android:icon="@drawable/ic_path"
            android:name=".SchemeHostPortPathActivity"
            android:label="指定scheme、host、port、path的Activity">
            <intent-filter>
                <action android:name="xx" />
                <category android:name="android.intent.category.DEFAULT" />
                <!-- 需要Intent的Data属性的scheme是lee、且host是www.fkjava.org
                、port是8888、且path是/mypath才可启动该Activity -->
                <data
                    android:scheme="lee"
                    android:host="www.fkjava.org"
                    android:port="8888"
                    android:path="/mypath"/>
            </intent-filter>
        </activity>
        <activity
            android:icon="@drawable/ic_type"
            android:name=".SchemeHostPortPathTypeActivity"
            android:label="指定scheme、host、port、path、type的Activity">
            <intent-filter>
                <action android:name="xx"/>
                <category android:name="android.intent.category.DEFAULT" />
                <!-- 需要Intent的Data属性的scheme是lee、且host是www.fkjava.org
                、port是8888、且path是/mypath
                、且type是abc/xyz才可启动该Activity -->
                <data
                    android:scheme="lee"
                    android:host="www.fkjava.org"
                    android:port="8888"
                    android:path="/mypath"
                    android:mimeType="abc/xyz"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="匹配scheme的Intent"
        android:onClick="scheme"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="匹配scheme、host、port的Intent"
        android:onClick="schemeHostPort"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="匹配scheme、host、path的Intent"
        android:onClick="schemeHostPath"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="匹配scheme、host、port、path的Intent"
        android:onClick="schemeHostPortPath"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="匹配scheme、host、port、path和type的Intent"
        android:onClick="schemeHostPortPathType"/>
</LinearLayout>

activity

package org.crazyit.intent;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.content.Intent;

/**
 * Description:
 * <br/>website: <a href="http://www.crazyit.org">crazyit.org</a>
 * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author Yeeku.H.Lee [email protected]
 * @version 1.0
 */
public class DataTypeAttr extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }  //intent的Data属性,只有scheme为lee,只有第一个activity满足条件
    public void scheme(View source)
    {
        Intent intent = new Intent();
        // 只设置Intent的Data属性
        intent.setData(Uri.parse("lee://www.crazyit.org:1234/test"));
        startActivity(intent);
    }  //intent的Data属性,只有scheme为lee,所以第一个activity满足条件,且该intent的Datahost ,port 与第二个activity相符
    public void schemeHostPort(View source)
    {
        Intent intent = new Intent();
        // 只设置Intent的Data属性
        intent.setData(Uri.parse("lee://www.fkjava.org:8888/test"));
        startActivity(intent);     
    }  //intent的Data属性,只有scheme为lee,所以第一个满足条件,且该intent的Data中host 和path与第三个activity相符
    public void schemeHostPath(View source)
    {
        Intent intent = new Intent();
        // 只设置Intent的Data属性
        intent.setData(Uri.parse("lee://www.fkjava.org:1234/mypath"));
        startActivity(intent);
    }  //intent的Data属性,只有scheme为lee,所以第一个满足的条件;intent中Data的host,port与第二个相符,host,path与第三个相符;host,port,path与第四个相符
    public void schemeHostPortPath(View source)
    {
        Intent intent = new Intent();
        // 只设置Intent的Data属性
        intent.setData(Uri.parse("lee://www.fkjava.org:8888/mypath"));
        startActivity(intent);
    }  //指定了Data和Type只有第五个相符
    public void schemeHostPortPathType(View source)
    {
        Intent intent = new Intent();
        // 同时设置Intent的Data、Type属性
        intent.setDataAndType(Uri.parse("lee://www.fkjava.org:8888/mypath")
            , "abc/xyz");
        startActivity(intent);
    }
}
时间: 2024-08-25 12:10:09

android intent.setData intent.setDataAndType的相关文章

使用Intent启动图片裁剪时遇到的问题:Intent#setData()与Intent#setType()赋值问题

在做使用Intent启动图片裁剪时遇到一个问题: 分开调用Intent#setData();Intent#setType();这两个方法时,程序无法正确运行,但使用Intent#setDataAndType();程序正常运行. 原因分析: 查看Intent#setData(),Intent#setType()源码: public Intent setData(Uri data) { mData = data; mType = null; return this; } public Intent

Android基础之Intent 和 Intent 过滤器

Intent是一个消息传递对象,您可以使用它从其他应用组件请求操作.尽管 Intent 可以通过多种方式促进组件之间的通信,但其基本用例主要包括以下三个: 1.启动 Activity: Activity 表示应用中的一个屏幕.通过将 Intent 传递给 startActivity(),您可以启动新的 Activity 实例.Intent 描述了要启动的 Activity,并携带了任何必要的数据.如果您希望在 Activity 完成后收到结果,请调用 startActivityForResult

android开发之Intent(2)

Data.Type      Data属性通过用于向Action属性提供操作的数据.Data属性接受一个Uri(格式形如:scheme://host:port/path)对象.      Type属性则是用于指定Data所指定Uri对应的MIME类型,MIME的格式形如:abx/xyz      声明Data与Type属性是通过<data-/>元素来设置,格式如下: <data android:mimeType="" android:scheme="&quo

Android中的intent属性

android之Intent的七大属性 2015年04月03日 ⁄ Android ⁄ 共 14866字 ⁄ 字号 小 中 大 ⁄ 1条评论 Intent用于封装程序的“调用意图”.两个Activity之间,可以把需要交换的数据,封装成Bundle对象,然后使用Intent对象,携带数据到另一个Activity中.实现两个Activity的数据交换. Intent还是各种应用程序组件之间通信的重要媒介.启动Activity.Service.BroadcastReceiver,都需要使用Inten

Android -- 再来一发Intent

之前写过一篇Intent的博客,主要说了一下隐式意图. 传送门:<Android -- Intent> Intent对象构成 Component name.Action.Data.Category.Extras.Flags Component name Component name即组件名称,是要处理这个Intent对象的组件名称. 组件名称对象由ComponentName类来封装,组件名称包含包名称和类名称,被声明在AndroidManifest.xml文件中. 组件名称通过 setComp

Android开发之Intent.Action

转载自 http://www.cnblogs.com/hanyonglu/archive/2012/03/26/2417278.html 1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始.比较常用. Input:nothing Output:nothing <activity android:name=".Main" android:label="@string/app_

Android开发之Intent跳转到系统应用中的拨号界面、联系人界面、短信界面

现在开发中的功能需要直接跳转到拨号.联系人.短信界面等等,查找了很多资料,自己整理了一下.1.跳转到拨号界面,代码如下: 1)直接拨打 Intent intentPhone = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber)); startActivity(intentPhone); 2)跳转到拨号界面 Intent intent = newIntent(Intent.ACTION_DIAL,Uri.pars

android API Guides学习--Intent and Intent Filters

Intent是一个消息传递类,同时可以启动一些组件 作用: 1启动一个activity组件. 通过Context类的 startActivity()/ startActivity ()方法启动activity组件. 2启动一个service组件 通过Context类的startService()方法启动此组件,只能执行一次性操作(例如下载文件). 通过Context类的 bindService()方法 3发送一条广播 通过Context类的sendBroadcast(), sendOrdered

Android组件系列----Intent详解

[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3959204.html 联系方式:[email protected] [正文] Intent组件虽然不是四大组件,但却是连接四大组件的桥梁,学习好这个知识,也非常的重要. 一.什么是Intent 1.Intent的概念: Android中提供了Intent机制来协助应用间的交互与通讯,或者采用更准确