现在的位置: 首页 > 综合 > 正文

intent-filter详解

2013年02月25日 ⁄ 综合 ⁄ 共 5760字 ⁄ 字号 评论关闭

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>

 

 

抱歉!评论已关闭.