您的位置:首页 > 移动开发 > Android开发

Android Intent.ACTION_CHOOSER 与 Intent.ACTION_PICK_ACTIVITY实现设置壁纸

2011-06-14 10:10 615 查看
Android Intent.ACTION_CHOOSER 与 Intent.ACTION_PICK_ACTIVITY
2011-04-13 23:02

1.Intent.ACTION_CHOOSER = “android.intent.action.CHOOSER” 其作用是显示一个Activity选择器。
Intent提供了一个静态的createChooser方法,让我们能够更方便的创建这样一个Intent。具体的用法示例可以参考Launcher应用里Launcher.java的startWallpaper函数:
private void startWallpaper() {
closeAllApps(true);
final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Intent chooser = Intent.createChooser(pickWallpaper,
getText(R.string.chooser_wallpaper));

startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
}
这里是要找到所有能处理Intent.ACTION_SET_WALLPAPER请求的activity,其字符串表示为android.intent.action.SET_WALLPAPER。使用Eclipse搜索之后,在以下应用的AndroidManifest.xml文件都找到了能处理这个请求的activity:
packages/apps/Gallery
packages/apps/Launcher2
packages/wallpapers/LivePicker
所以,在Home界面“按Menu键”--“点击壁纸”后自然就能在一个对话框里列出这些应用,让用户选择到哪里去设置壁纸了,如下图所示。



在上图中,用户点击任意一个列表项,都会激活其对应的Activity。用户设置壁纸的操作会在新启动的Activity里完成,例如用户点击上图中的“照片”项,则会进入到Gallery应用,在Gallery里完成壁纸设置,见下图。



2. Intent.ACTION_PICK_ACTIVITY介绍及两者区别
Intent.ACTION_PICK_ACTIVITY = “android.intent.action.PICK_ACTIVITY”。
乍一看去,真看不出它和 Intent.ACTION_CHOOSER有什么区别。为了弄清这一点,笔者特地做了个试验,把上面设置壁纸的intent action改为 Intent.ACTION_PICK_ACTIVITY,运行出来的界面如下图。



从界面上看去,和Intent.ACTION_CHOOSER的表现方式基本一致,但是点击以后却没有像Intent.ACTION_CHOOSER一样启动所选的Activity。笔者很快从Android源码里找到了原因。
对intent.ACTION_PICK_ACTIVITY action的处理位于Settings应用里,如下XML所示。
<activity android:name="ActivityPicker"
android:label="@string/activity_picker_label"
android:theme="@*android:style/Theme.Dialog.Alert"
android:finishOnCloseSystemDialogs="true">
<intent-filter>
<action android:name="android.intent.action.PICK_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
咱们再到com.android.settings.ActivityPicker类里去看个究竟。
/**
* Handle clicking of dialog item by passing back
* {@link #getIntentForPosition(int)} in {@link #setResult(int, Intent)}.
*/
public void onClick(DialogInterface dialog, int which) {
Intent intent = getIntentForPosition(which);
setResult(Activity.RESULT_OK, intent);
finish();
}
这下很明白了,咱们点击列表项的时候,它没有用intent启动相应的activity,而是将它返回了。这就是它与Intent.ACTION_CHOOSER action的区别所在。同样地,咱们也可以从源代码里找到Intent.ACTION_CHOOSER的真实处理流程。跟上面流程差不多,到源码里搜索“android.intent.action.CHOOSER”就能很快找到入口并追踪下去。对于Intent.ACTION_CHOOSER,笔者在ResolverActivity内也找到一个onClick方法,贴出重要代码如下。
public void onClick(DialogInterface dialog, int which) {
ResolveInfo ri = mAdapter.resolveInfoForPosition(which);
Intent intent = mAdapter.intentForPosition(which);

。。。。。。

if (intent != null) {
startActivity(intent);
}
finish();
}
跟前面推断的一样,它最后直接启动了用户选择的列表项对应的activity。

3.文档阐述
看看文档里对它们各自的阐述,本人英文水平不高,所以直接贴了。
ACTION_CHOOSER
Input: No data should be specified. get*Extra must have a EXTRA_INTENT field containing the Intent being executed, and can optionally have a EXTRA_TITLE field containing the title text to display in the chooser.
Output: Depends on the protocol of EXTRA_INTENT.

ACTION_PICK_ACTIVITY
Input: get*Extra field EXTRA_INTENT is an Intent used with PackageManager.queryIntentActivities to determine the set of activities from which to pick.
Output: Class name of the activity that was selected.

4.Intent.ACTION_PICK_ACTIVITY实例
不用到网上翻来翻去,Android源码是最好的学习资料。Launcher应用里的添加文件夹(userFolder)和LiverFolder时用的就是这个action。在此不详述。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐