Android_Intent_data_type
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的更多相关文章
随机推荐
- HttpListener supports SSL only for localhost? install certificate
1.Start-All Programs - 2.execute below lines on that ‘Developer Command Prompt..’ tool makecert -n & ...
- POJ 3345-Bribing FIPA(树状背包)
题意: 有n个国家投票,要得到一个国家的投票有一定的花费,如果给到一个国家的票同时也得到了它所有附属国的票,给出国家关系树,求至少得到m票的最小花费. 分析:基础树状背包,dp[i][j],以i为根的 ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- C 头文件阅读理解
__BEGIN_DECLS ..... ..... __END_DECLS 很多时候,为了使 C 代码和 C++ 代码保持互相兼容的过程调用接口,需要在 C++ 代码里加上 extern " ...
- 【推荐】JavaScript的那些书
又好久没写东西了,写上一篇的时候还以为接下来的工作会轻松一些,结果未从我所愿呐,又是一阵忙碌.而这段时间穿插着做了很多12年淘宝校园招聘的前端面试,很多同学都有问到,学校里没有前端的课程,那如何学习J ...
- SQL2008-显示表大小行数
select object_name(id) tablename,8*reserved/1024 reserved,rtrim(8*dpages/1024)+'Mb' used,8*(reserved ...
- fscanf(格式化字符串输入)
fscanf(格式化字符串输入) 相关函数 scanf,sscanf 表头文件 #include<stdio.h> 定义函数 int fscanf(FILE * stream ,const ...
- [Objective-c 基础 - 2.10] description方法
A. 实例对象打印-description 1.当使用NSLog函数并且使用%@占位符的时候,会调用对象的-description方法 2.拿到-description的返回值,显示到console中 ...
- URL- 含义及组成
URL (uniform resource locator) : 互联网的每个网页都有自己唯一的统一资源定位器,由3部分组成:通信协议,主机名,资源名. HTTP(hypertext transfer ...
- Oracle10g完全卸载正确步骤
Oracle卸载要求比较严格,不能简单的卸载就完事了:当然Oracle卸载也没有那么难,只是步骤比较多.Oracle10g还是Oracle11g卸载步骤都是一样的.下边详细介绍一下. 找到Oracle ...