layout.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10.  
  11. <Button
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:text="隐式跳转到另一个页面"
  15. android:onClick="myOnClick"
  16. />
  17.  
  18. </RelativeLayout>

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.day06_intent_data_type"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6.  
  7. <uses-sdk
  8. android:minSdkVersion="17"
  9. android:targetSdkVersion="17" />
  10.  
  11. <application
  12. android:allowBackup="true"
  13. android:icon="@drawable/ic_launcher"
  14. android:label="@string/app_name"
  15. android:theme="@style/AppTheme" >
  16. <activity
  17. android:name="com.example.day06_intent_data_type.MainActivity"
  18. android:label="@string/app_name" >
  19. <intent-filter>
  20. <action android:name="android.intent.action.MAIN" />
  21. <category android:name="android.intent.category.LAUNCHER" />
  22. </intent-filter>
  23. </activity>
  24. <activity android:name="com.example.day06_intent_data_type.OtherActivity">
  25. <intent-filter>
  26. <action android:name="com.qf.aaa"/>
  27. <category android:name="android.intent.category.DEFAULT"/>
  28. <data android:scheme="http"
  29. android:host="www.baidu.com"
  30. android:port="8080"
  31. android:path="/string"
  32. android:mimeType="txt/xmppp"
  33. />
  34. </intent-filter>
  35. </activity>
  36. <!--
  37. schme 协议
  38. host 主机名
  39. port 端口号,端口号写与不写对activity中设置的参数无影响,不会报错
  40. mimetype 媒体类型
  41. path 路径 -->
  42. <!-- 注意:清单文件中必须定义category参数,否则报错,但是activity中可以不用设置 ,会默认添加-->
  43. <!-- 当有多个符合intent-filter的activity时,系统会提示两个窗口让用户选择-->
  44. <!-- 当一个activity设置了多个intent-filter时,任选其中一个也可调用该activity-->
  45. <!-- <intent-filter >
  46. <action android:name="com.qf.bbb"/>
  47. <category android:name="android.intent.category.DEFAULT"/>
  48. </intent-filter> -->
  49.  
  50. </application>
  51.  
  52. </manifest>

MainActivity.java

  1. package com.example.day06_intent_data_type;
  2.  
  3. import android.net.Uri;
  4. import android.os.Bundle;
  5. import android.app.Activity;
  6. import android.content.Intent;
  7. import android.view.Menu;
  8. import android.view.View;
  9.  
  10. public class MainActivity extends Activity {
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. }
  17.  
  18. public void myOnClick(View v){
  19. //创建Intent对象
  20. Intent intent = new Intent();
  21. //设置要跳转的action,action参数的名字必须与要跳转activity在清单文件中设置的自定义action的名字一致
  22. intent.setAction("com.qf.aaa");
  23. //将String转成Uri
  24. // data访问资源的格式
  25. // scheme://host:port/path 协议名://主机名:端口号/路径
  26. Uri uri = Uri.parse("http://www.baidu.com:8080/string");
  27. //当要跳转的activity中的data不设置path时,当前的uri可以指定path,
  28. //当跳转的activity中的data设置了path,当前uri必须和其path名一致,否则报错
  29. //设置data与要跳转的activity中 的data设置的参数一致,当data与type都设置了时,不能使用setdata()或者setType()两个互斥,具体看源码
  30. intent.setDataAndType(uri, "txt/xmppp");
  31. //开启新的activity
  32. startActivity(intent);
  33. }
  34. }

OtherActivity.java

  1. package com.example.day06_intent_data_type;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.widget.Toast;
  8.  
  9. public class OtherActivity extends Activity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. // TODO Auto-generated method stub
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.layout_other);
  15. //获取intent-filter的值
  16. //获取传过来的intent,并通过intent来获得uri
  17. Intent intent = getIntent();
  18. Uri data = intent.getData();
  19. String type = intent.getType();
  20. Toast.makeText(OtherActivity.this, "data:"+data.toString()+",type:"+type, 0).show();
  21.  
  22. }
  23. }

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. HttpListener supports SSL only for localhost? install certificate

    1.Start-All Programs - 2.execute below lines on that ‘Developer Command Prompt..’ tool makecert -n & ...

  2. POJ 3345-Bribing FIPA(树状背包)

    题意: 有n个国家投票,要得到一个国家的投票有一定的花费,如果给到一个国家的票同时也得到了它所有附属国的票,给出国家关系树,求至少得到m票的最小花费. 分析:基础树状背包,dp[i][j],以i为根的 ...

  3. 【暑假】[实用数据结构]UVAlive 3942 Remember the Word

    UVAlive 3942 Remember the Word 题目: Remember the Word   Time Limit: 3000MS   Memory Limit: Unknown   ...

  4. C 头文件阅读理解

    __BEGIN_DECLS ..... ..... __END_DECLS 很多时候,为了使 C 代码和 C++ 代码保持互相兼容的过程调用接口,需要在 C++ 代码里加上 extern " ...

  5. 【推荐】JavaScript的那些书

    又好久没写东西了,写上一篇的时候还以为接下来的工作会轻松一些,结果未从我所愿呐,又是一阵忙碌.而这段时间穿插着做了很多12年淘宝校园招聘的前端面试,很多同学都有问到,学校里没有前端的课程,那如何学习J ...

  6. SQL2008-显示表大小行数

    select object_name(id) tablename,8*reserved/1024 reserved,rtrim(8*dpages/1024)+'Mb' used,8*(reserved ...

  7. fscanf(格式化字符串输入)

    fscanf(格式化字符串输入) 相关函数 scanf,sscanf 表头文件 #include<stdio.h> 定义函数 int fscanf(FILE * stream ,const ...

  8. [Objective-c 基础 - 2.10] description方法

    A. 实例对象打印-description 1.当使用NSLog函数并且使用%@占位符的时候,会调用对象的-description方法 2.拿到-description的返回值,显示到console中 ...

  9. URL- 含义及组成

    URL (uniform resource locator) : 互联网的每个网页都有自己唯一的统一资源定位器,由3部分组成:通信协议,主机名,资源名. HTTP(hypertext transfer ...

  10. Oracle10g完全卸载正确步骤

    Oracle卸载要求比较严格,不能简单的卸载就完事了:当然Oracle卸载也没有那么难,只是步骤比较多.Oracle10g还是Oracle11g卸载步骤都是一样的.下边详细介绍一下. 找到Oracle ...