intent-filter详解

简介: main.xml如下: another.xml如下:   MainActivity如下: package cn.

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/mainActivity_tip" 
        android:textSize="25sp"
        />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/button_tip" 
        android:textSize="25sp"/>

</RelativeLayout>


another.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
     
   <TextView 
       android:layout_width="200dip"
       android:layout_height="50dip"
       android:layout_centerInParent="true"
       android:gravity="center"
       android:textSize="20sp"
       android:text="@string/anotherActivity_tip"
       />
   
</RelativeLayout>


 

MainActivity如下:

package cn.com.bravesoft.testactivity4;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 利用隐式意图激活组件的测试笔记
* 在利用隐式意图激活组件时常用action category data进行匹配
* 测试1:
* 代码中设置为:
* Intent intent = new Intent();
* intent.setAction("cn.com.bravesoft.testAction");
* startActivity(intent);
* Manifest文件中设置:
*  <intent-filter >
*     <action android:name="cn.com.bravesoft.testAction"/>
*  </intent-filter>
* 结果:
* 报错,无法激活组件
* 原因:
* 在intent设置action的时候会默认给改intent设置一个默认的category即
* 相当于会自动执行:intent.addCategory("android.intent.category.DEFAULT");
* 解决:
* 在<intent-filter >中设置category即
*  <category android:name="android.intent.category.DEFAULT" />
* 这样就可以匹配到
* 总结:
* 在代码中的intent和manifest中的</intent-filter>均为设置data的情况下
* 只要代码中intent的action和category均出现在了</intent-filter>那么就
* 可以激活组件
* 
* 测试2:
* 关于data的匹配测试
* 在测试1中可以看到如果我们在代码中没有设置category那么系统会自动添加一个
* 默认category即android.intent.category.DEFAULT.
* 但是如果在意图对象或manifest中设置了data而另一方没有设置data,那么无论如何是不能匹配的
* 关于该点不再单独测试
* 关于数据的匹配常用到scheme host path
* 比如在http://www.bravesoft.com.cn/android中
* scheme是http
* host是www.bravesoft.com.cn
* path是/android
* 此时代码如下:
* Intent intent = new Intent();
* intent.setAction("cn.com.bravesoft.testAction");
* intent.setData(Uri.parse("http://www.bravesoft.com.cn/android"));
* startActivity(intent);
* manifest的<intent-filter >如下:
* <intent-filter >
*    <action android:name="cn.com.bravesoft.testAction"/>
*    <category android:name="android.intent.category.DEFAULT" />
*    <data android:scheme="http" android:host="www.bravesoft.com.cn" android:path="/android"/>
* </intent-filter>
* 此时可以匹配的
* 
* 测试3:
* 在测试2的基础上继续测试
* 关于数据的匹配除了常用到scheme host path,有时还会采用mimeType
* 为 </intent-filter>添加了mimeType即<data android:mimeType="image/gif"/>
* 为了匹配该意图所以在代码中设置了intent.setType("image/gif");
* 此时代码如下:
* Intent intent = new Intent();
* intent.setAction("cn.com.bravesoft.testAction");
* intent.setData(Uri.parse("http://www.bravesoft.com.cn/android"));
* intent.setType("image/gif");
* 
* manifest的<intent-filter >如下:
*  <intent-filter >
*     <action android:name="cn.com.bravesoft.testAction"/>
*     <category android:name="android.intent.category.DEFAULT" />
*     <data android:scheme="http" android:host="www.bravesoft.com.cn" android:path="/android"/>
*     <data android:mimeType="image/gif"/>
*   </intent-filter>
* 结果:
* 无法匹配
* 原因:
* 在调用setType的时候会自动清除setData所设置的内容,即setData失效
* 解决:
* 当manifest中的 </intent-filter>除了设置scheme host path外还
* 设置了mimeType时在代码中需要intent.setDataAndType将这些必须的
* 东西同时设置.
* 此时代码如下:
* Intent intent = new Intent();
* intent.setAction("cn.com.bravesoft.testAction");
* intent.setDataAndType(Uri.parse("http://www.bravesoft.com.cn/android"), "image/gif");
* startActivity(intent);

* manifest的<intent-filter >如下:
*  <intent-filter >
*     <action android:name="cn.com.bravesoft.testAction"/>
*     <category android:name="android.intent.category.DEFAULT" />
*     <data android:scheme="http" android:host="www.bravesoft.com.cn" android:path="/android"/>
*     <data android:mimeType="image/gif"/>
*   </intent-filter>
*/
public class MainActivity extends Activity {
	private Button mButton;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mButton = (Button) findViewById(R.id.button);
		mButton.setOnClickListener(new ButtonOnClickListener());
	}

	private class ButtonOnClickListener implements OnClickListener {
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setAction("cn.com.bravesoft.testAction");
			intent.setDataAndType(Uri.parse("http://www.bravesoft.com.cn/android"), "image/gif");
			startActivity(intent);
		}
	}
	
}

AnotherActivity如下:

package cn.com.bravesoft.testactivity4;
import android.app.Activity;
import android.os.Bundle;

public class AnotherActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.another);
      
	}
	
}

AndroidManifest如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.com.bravesoft.testactivity4"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="7" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="cn.com.bravesoft.testactivity4.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 -->
        <activity android:name="cn.com.bravesoft.testactivity4.AnotherActivity">
            <intent-filter >
                <action android:name="cn.com.bravesoft.testAction"/>
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="http" android:host="www.bravesoft.com.cn" android:path="/android"/>
                <data android:mimeType="image/gif"/>
            </intent-filter>
        </activity>
    </application>

</manifest>


 


 

相关文章
|
10月前
|
XML 存储 前端开发
Android:Intent 和 Intent 过滤器
在前 4 篇文章中,我们介绍了 Android 四大组件的基础知识,四大组件是构成我们 App 的基础,也是 Android 系统设计的最佳体现。各个组件之间完全是解耦的,如果想访问其他组件或者启动其他组件可以使用 Intent 来操作。在四种组件类型中,有三种(Activity、Service 和 Broadcast)均可以通过异步消息 Intent 进行启动。Intent 会在运行时对各个组件进行互相绑定。所以我们可以把 Intent 当作是各个组件之间的信使(无论该组件是自己 App 的还是其他 App)。
51 0
Android:Intent 和 Intent 过滤器
|
安全 Android开发 开发者
Android 13 针对 Intent filters 安全的再加强
Android 13 针对 Intent filters 安全的再加强
|
XML Java Android开发
Intent的用法
Intent的用法
Intent的用法
|
Android开发 数据格式 XML
|
Android开发 数据格式 Java
intent
intent intent 是一种构造,应用程序可以通过它发出请求,这就像是发出求助信号。intent 可能像下面这样: "Wanted: An application to help me look up a contact" 或 "Wanted: An application to help me display this image" 或 "Wanted: An application to perform this geographic-based search" 应用程序可以按照相似或互补的方式进行注册,表明它们有能力或有兴趣执行各种请求或 intent。