layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="隐式跳转到另一个页面"
android:onClick="myOnClick"
/> </RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.day06_intent_data_type"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.day06_intent_data_type.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.day06_intent_data_type.OtherActivity">
<intent-filter>
<action android:name="com.qf.aaa"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"
android:host="www.baidu.com"
android:port="8080"
android:path="/string"
android:mimeType="txt/xmppp"
/>
</intent-filter>
</activity>
<!--
schme 协议
host 主机名
port 端口号,端口号写与不写对activity中设置的参数无影响,不会报错
mimetype 媒体类型
path 路径 -->
<!-- 注意:清单文件中必须定义category参数,否则报错,但是activity中可以不用设置 ,会默认添加-->
<!-- 当有多个符合intent-filter的activity时,系统会提示两个窗口让用户选择-->
<!-- 当一个activity设置了多个intent-filter时,任选其中一个也可调用该activity-->
<!-- <intent-filter >
<action android:name="com.qf.bbb"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter> --> </application> </manifest>

MainActivity.java

package com.example.day06_intent_data_type;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void myOnClick(View v){
//创建Intent对象
Intent intent = new Intent();
//设置要跳转的action,action参数的名字必须与要跳转activity在清单文件中设置的自定义action的名字一致
intent.setAction("com.qf.aaa");
//将String转成Uri
// data访问资源的格式
// scheme://host:port/path 协议名://主机名:端口号/路径
Uri uri = Uri.parse("http://www.baidu.com:8080/string");
//当要跳转的activity中的data不设置path时,当前的uri可以指定path,
//当跳转的activity中的data设置了path,当前uri必须和其path名一致,否则报错
//设置data与要跳转的activity中 的data设置的参数一致,当data与type都设置了时,不能使用setdata()或者setType()两个互斥,具体看源码
intent.setDataAndType(uri, "txt/xmppp");
//开启新的activity
startActivity(intent);
}
}

OtherActivity.java

package com.example.day06_intent_data_type;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast; public class OtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_other);
//获取intent-filter的值
//获取传过来的intent,并通过intent来获得uri
Intent intent = getIntent();
Uri data = intent.getData();
String type = intent.getType();
Toast.makeText(OtherActivity.this, "data:"+data.toString()+",type:"+type, 0).show(); }
}

Note

.隐示意图激活---按照一定约束条件开启
1.先定义需要暴露的activity
2.如何暴露?
在清单文件中activity标签下新添intent-filter标签,
必须写action和category属性,action的值为任意字符串,是intent访问的标识符
<intent-filter >
<action android:name="com.hz1605.haha"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

3.如何访问?(在当前activity中)
不需要给intent指定明确的跳转到那个类,只需要指定action值
1)设置action, ---值必须与需要开启的activity的intent-filter的action值一致
intent.setAction("com.hz1605.haha");
2) category值是默认添加上的
默认会加上category属性 ,默认值为android.intent.category.DEFAULT
intent.addCategory("android.intent.category.DEFAULT");

4.可以开启另外一个app的符合条件的activity,另外一个app关闭的时候也能启动,(action与category设置匹配)
当有多个符合条件的intent-filter时会提示让用户选择(两个activity的intent-filter相同)

7.data type属性
data访问资源的格式
scheme://host:port/path 协议名://主机名:端口号/路径 http:// file://
1.如何在清单文件中编写
<data android:scheme="hello"
android:host="www.baidu.com"
android:path="/key"
android:port="8080"
/>

设置type
<data android:mimeType="txt/"/>
2.如何匹配

Uri uri = Uri.parse("nnn://www.nnn.com");
设置data
当同时又data和type属性时不能使用以下方式设置值,需设置intent.setDataAndData(intent,"txt");
intent.setData(uri);
intent.setType("txt/");

3.如果说一个acitivity注册中有多个intent-filter,只需要匹配一个就可以开启

8.应用
1.打开home页 2.调用拨打电话功能 3.打开浏览器 4.打开发送短信页面

Android_Intent_data_type的更多相关文章

随机推荐

  1. error LNK2001: 无法解析的外部符号 _IID_IDirectDraw7

    工程使用了DirectDraw,编译出错 error LNK2001: 无法解析的外部符号 _IID_IDirectDraw7 解决办法是吧dxguid.lib添加到工程中,把lib所在目录添加到工程 ...

  2. 018如何建立自动化框架 how to bulid the framwork

    本讲包括: 一. objective 二. How to bulid 三. Keyview of frawork (关键视图) 四. conclusion automation framwork:自动 ...

  3. C/C++:原码、反码和补码

    正数的原码.反码和补码是一模一样的.   负数的反码的符号位跟原码一样,但其余各位取反. 负数的补码是其反码的最末位加1得到,即原码取反加1.   补码的补码就是原码.   浮点数的存储格式随着机器的 ...

  4. 4.2 CUDA Reduction 一步一步优化

    Reduction并行分析: 每个线程是基于一个树状的访问模型,从上至下,上一层读取数据相加得到下一层的数据.不停的迭代,直到访问完所有的数据. 利用这么多的线程块(thread block)我们需要 ...

  5. leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)

    https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer array nu ...

  6. 在virtualbox上安装mac os mavericks遇到Missing Bluetooth Controller Transport问题解决办法

    挂载 HackBoot_Mav.iso 作为光驱,启动在 磁盘选择 界面,选择磁盘后按空格键,输入 启动参数 -v -x(-x的意思是安全模式,不这样的话,有蓝牙驱动作怪, 会输出 [IOBlueto ...

  7. 组建Windows家庭组

    这里,是必须得针对Windows 7.8.10而言,因为,Windows XP 和 Windows Vista系统没有家庭网络的功能.(比较过时的一些系统了) 1.Windows家庭组的简介 使用家庭 ...

  8. SQL2008-查询库中是否存在某表

    select   *   from   sysobjects   where   name= 'N201111B'   and   xtype= 'U'

  9. UIViewController生命周期测试

    push进入  -[NaviRootVC viewWillDisappear:]  -[NextVC viewWillAppear:]  -[NextVC viewWillLayoutSubviews ...

  10. Modernizr 与 Polyfill

    之前提到,Modernizr 是 HTML5 和 CSS3 的特性检测工具,这里简单介绍一下它的用法.最简单的用法是在页面的 <head> 中添加 Modernizr 的 JavaScri ...