牛骨文教育服务平台(让学习变的简单)

3,Action、Category属性与intent-filter配置

Intent的Action和Category属性都是一个普通的字符串,其中Action代表该Intent所要完成的一个抽象“动作”,Category则用于为Action增加额外的附加类别信息,通常,Action与Category结合使用。

元素里通常可包括如下子元素:
a、0~N个子元素
b、0~N个子元素
c、0~1个子元素

子元素的配置非常简单,它们都可指定android:name属性,该属性的值就是一个普通字符串。
当元素里的子元素里包含多个子元素(相当于指定了多个字符串)时,就表明该Activity能响应Action属性值为其中任意一个字符串的Intent。

一个Intent对象最多只能包括一个Action属性,程序调用Intent的setAction(String str)方法来设置Action属性值;但一个Intent对象可包含多个Category属性,调用Intent的addCategory(String str)方法添加。

当程序创建Intent时,该Intent默认启动Category属性值为Intent.CATEGORY_DEFAULT常量(常量值为android.intent.category.DEFAULT)的组件。


public class ActionCateAttr extends Activity
{
	// 定义一个Action常量
	final static String _ACTION = 
		"com.intent.action._ACTION";
	// 定义一个Category常量
	final static String _CATEGORY =
		"com.intent.category._CATEGORY";

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button bn = (Button) findViewById(R.id.bn);
		bn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				Intent intent = new Intent();
				// 设置Action属性
				intent.setAction(ActionCateAttr._ACTION);
				// 添加Category属性
				intent.addCategory(ActionCateAttr._CATEGORY);
				startActivity(intent);
			}
		});
	}
}

public class SecondActivity extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		EditText show = (EditText) findViewById(R.id.show);
		// 获取该Activity对应的Intent的Action属性
		String action = getIntent().getAction();
		// 显示Action属性
		show.setText("Action为:" + action);
		EditText cate = (EditText) findViewById(R.id.cate);
		// 获取该Activity对应的Intent的Category属性
		Set<String> cates = getIntent().getCategories();
		// 显示Action属性
		cate.setText("Category属性为:" + cates);
	}
}

Manifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="org.crazyit.intent"
	android:versionCode="1"
	android:versionName="1.0">
	<uses-sdk
		android:minSdkVersion="10"
		android:targetSdkVersion="17" />
	<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
		<activity android:name=".ActionCateAttr"
				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=".SecondActivity"
				android:label="@string/app_name">
			<intent-filter>
				<!-- 指定该Activity能响应action为指定字符串的Intent -->
				<action android:name="com.intent.action._ACTION" />
				<!-- 指定该Activity能响应category为指定字符串的Intent -->
				<category android:name="com.intent.category.CRAZYIT_CATEGORY" />
				<!-- 指定该Activity能响应category为android.intent.category.DEFAULT的Intent -->
				<category android:name="android.intent.category.DEFAULT" />   
			</intent-filter>
		</activity>
	</application>
	<uses-permission android:name="android.permission.READ_CONTACTS" />
	<uses-permission android:name="android.permission.WRITE_CONTACTS" />

</manifest> 

3、Data、Type属性与intent-filter配置

Data属性通常用于向Action属性提供操作的数据,Data属性接收一个Uri对象,一个Uri对象通常通过如下形式的字符串表示:
content://com.android.contracts/contacts/1
tel:123
上面两个字符串的冒号前面大致指定了数据的类型,冒号后面是数据部分。

Uri字符串总满足如下格式:

scheme://host.port/path

Type属性则用于明确指定Data属性所指定数据的类型或MIME类型,当Intent不指定Data属性时Type属性才会起作用,否则Android系统将会根据Data属性值来分析数据的类型,因此无需指定Type属性。

一旦为Intent同时指定Action、Data属性,那么Android将可根据指定的数据类型来启动特定的应用程序,并对指定数据执行相应的操作。下面介绍几个Action、Data属性的组合:

ACTION_VIEW content://com.android.contacts/contacts/1:显示标识为1的联系人的信息
ACTION_EDIT content://com.android.contacts/contacts/1:编辑标识为1的联系人的信息
ACTION_DIAL content://com.android.contacts/contacts/1:显示向标识为1的联系人拨号的界面
ACTION_VIEW tel:123:显示向指定号码123拨号的界面
ACTION_DIAL tel:123:显示向指定号码123拨号的界面
ACTION_VIEW content://contacts/people/:显示所有联系人列表的信息,通过这种组合可以方便地查看系统联系人