您的位置:首页 > 其它

intent使用总结

2011-08-08 20:16 134 查看
1. 指定action 和type

// SIM import

Intent importIntent = new Intent(Intent.ACTION_VIEW);

importIntent.setType("vnd.android.cursor.item/sim-contact");

importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");

menu.add(0, 0, 0, R.string.importFromSim)

.setIcon(R.drawable.ic_menu_import_contact)

.setIntent(importIntent);

2. 指定action, data和type

(1)隐式查找type

示例代码:

uri: content://simcontacts/simPeople/(id)

intent = new Intent("android.intent.action.SIMEDIT",uri);

startActivity(intent);

程序会很据data中的uri去查找匹配的type(必须的)

provider中的getType()

case SIM_PEOPLE_ID:

return "vnd.android.cursor.item/sim-contact";

配置文件中的filter设定

AndroidManifest.xml

<intent-filter>

<action android:name="android.intent.action.SIMEDIT" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="vnd.android.cursor.item/sim-contact" />

</intent-filter>

也可以自己设定type,但只能使用 setDataAndType()

3. 其他设定intent的属性方式

Intent setComponent(ComponentName component)

Intent setClassName(Context packageContext, String className)

Intent setClassName(String packageName, String className)

Intent setClass(Context packageContext, Class<?> cls)

注:

1. intent的实现代码

public Intent setData(Uri data) {

mData = data;

mType = null;

return this;

}

public Intent setType(String type) {

mData = null;

mType = type;

return this;

}

public Intent setDataAndType(Uri data, String type) {

mData = data;

mType = type;

return this;

}

2. 如果指定data,而type为null,则会提示找不到activity

这时需要在putExtra()中指定uri
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: