Android Intent.ACTION_CHOOSER 与 Intent.ACTION_PICK_ACTIVITY

1.Intent.ACTION_CHOOSER = “android.intent.action.CHOOSER”其作用是显示一个Activity选择器。

Intent提供了一个静态的createChooser方法,让我们能够更方便的创建这样一个Intent。具体的用法示例可以参考Launcher应用里Launcher.java的startWallpaper函数:

privatevoidstartWallpaper() {

closeAllApps(true);

finalIntent pickWallpaper =newIntent(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所示。

<activityandroid:name="ActivityPicker"

android:label="@string/activity_picker_label"

android:theme="@*android:style/Theme.Dialog.Alert"

android:finishOnCloseSystemDialogs="true">

<intent-filter>

<actionandroid:name="android.intent.action.PICK_ACTIVITY"/>

<categoryandroid: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)}.

*/

publicvoidonClick(DialogInterface dialog,intwhich) {

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方法,贴出重要代码如下。

publicvoidonClick(DialogInterface dialog,intwhich) {

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_INTENTfield containing the Intent being executed, and can optionally have a EXTRA_TITLEfield 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_INTENTis an Intent used with PackageManager.queryIntentActivitiesto 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。在此不详述。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值