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.javastartWallpaper函数:

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 。在此不详述。

更多相关文章

  1. Android(安卓)- GestureDetector.onGestureLister
  2. Android实战教程第七篇之如何在内存中存储用户名和密码
  3. Android本地代码直接访问远程数据库(Rxjava方式)
  4. Android——设计原则(Design Principles)
  5. android用户界面之SeekBar教程实例汇总
  6. Android(安卓)Permission denied 错误 ( 附Android权限大全 )
  7. Android(安卓)user defined service handling
  8. 安卓多用户学习笔记
  9. Android各个版本之间的区别(二)

随机推荐

  1. 配置ssh远程登录sshd服务器
  2. 固定定位的联系客服+简易的三行三列布局
  3. php学习第三天
  4. 字体图标、布局原理以及盒模型实例操作及
  5. 固定定位,三行三列定位布局
  6. 商品信息表设计
  7. 图标,布局,元素类型,盒子模型
  8. web前端作业
  9. CSS基本选择器与上下文选择器
  10. 表单的制作