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

更多相关文章

  1. Android入门笔记 - 界面开发 - Notification, NotificationManag
  2. 在Android使用新浪微博的开发平台API
  3. Android(安卓)多次点击的另一种思路
  4. Android(安卓)应用软件开发(四)菜单控件
  5. Android(安卓)代码审计工具和常见问题
  6. View点击涟漪效果
  7. ListView的item点击失效解决
  8. Android(安卓)监听返回按钮事件
  9. Android设置中清除应用数据源码分析

随机推荐

  1. Android(安卓)自定义ViewGroup
  2. Android(安卓)Fragment---与Activity生命
  3. Android中Spinner下拉列表(使用ArrayAdapt
  4. 2015最火十大Android开源项目,是个程序员
  5. Android(Java):在textview中显示富文本
  6. Android(安卓)编译时出现r cannot be res
  7. 使用drawable资源
  8. Android(安卓)dumpsys命令学习小记
  9. android聊天,存储聊天记录sqlite
  10. Android(安卓)文件存放路径