Android 操作系统对于<intent-filter>含有下列属性的Activity会在应用程序管理器(Launcher)显示一项,一般这个Activity对应于某个应用的主Activity。

<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />

此外,如果用户想在设备的Home Screen上添加应用的快捷方式,可以在Launcher中长按这个应用的图标,Android系统会自动为该应用在Home Screen上添加一个快捷方式,名称和图标和在Launcher中的一样。

除了支持指向应用(主Activity)的快捷方式外,Android可以在Home Screen上定义指向Application中任意Activity的快捷方式。

比如说你的应用中有个功能用户可能会经常使用,比如说地图中查询地址,正常情况下用户需要先启动主Activity,可能需要经过几次菜单选择或是 其它方式才能进到这个功能,用户可能感觉到不方便,这是可以为这个功能在Home Screen建立一个快捷方式,用户按这个快捷方式后会直接进入这个功能界面,即使这个Activity不是主Activity。

Launcher Shortcuts就是介绍了如何为某个非主Activity在Home Screen上建立一个快捷方式。

实现这个快捷方式,可以分下面几步来完成:

1.为需要创建快捷方式的Activity的<intent-filter>添加

<action android:name=”android.intent.action.CREATE_SHORTCUT” /> 

,标识这个Activity可以支持在Home Screen上添加快捷方式。Launcher Shortcuts 是采用的是activity-alias,activity-alias为Target的别名,类似于Activity.

2.添加相应用户添加快捷方式的代码,一般在Activity的onCreate方法中为Activity安装快捷方式:

private static final String EXTRA_KEY = "com.example.android.apis.app.LauncherShortcuts";
        // If the intent is a request to create a shortcut, we'll do that and exit

        if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
setupShortcut();
finish();
return;
}
    /**
* This function creates a shortcut and returns it to the caller. There are actually two
* intents that you will send back.
*
* The first intent serves as a container for the shortcut and is returned to the launcher by
* setResult(). This intent must contain three fields:
*
* <ul>
* <li>{@link android.content.Intent#EXTRA_SHORTCUT_INTENT} The shortcut intent.</li>
* <li>{@link android.content.Intent#EXTRA_SHORTCUT_NAME} The text that will be displayed with
* the shortcut.</li>
* <li>{@link android.content.Intent#EXTRA_SHORTCUT_ICON} The shortcut's icon, if provided as a
* bitmap, <i>or</i> {@link android.content.Intent#EXTRA_SHORTCUT_ICON_RESOURCE} if provided as
* a drawable resource.</li>
* </ul>
*
* If you use a simple drawable resource, note that you must wrapper it using
* {@link android.content.Intent.ShortcutIconResource}, as shown below. This is required so
* that the launcher can access resources that are stored in your application's .apk file. If
* you return a bitmap, such as a thumbnail, you can simply put the bitmap into the extras
* bundle using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}.
*
* The shortcut intent can be any intent that you wish the launcher to send, when the user
* clicks on the shortcut. Typically this will be {@link android.content.Intent#ACTION_VIEW}
* with an appropriate Uri for your content, but any Intent will work here as long as it
* triggers the desired action within your Activity.
*/
private void setupShortcut() {
// First, set up the shortcut intent. For this example, we simply create an intent that
// will bring us directly back to this activity. A more typical implementation would use a
// data Uri in order to display a more specific result, or a custom action in order to
// launch a specific operation. Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(this, this.getClass().getName());
shortcutIntent.putExtra(EXTRA_KEY, "ApiDemos Provided This Shortcut"); // Then, set up the container intent (the response to the caller) Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_name));
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
this, R.drawable.app_sample_code);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); // Now, return the result to the launcher setResult(RESULT_OK, intent);
}

如果用户想为某个Activity创建快捷方式,方法是在Home Screen的空白处长按,这时Android会显示用户可以选择添加的桌面种类列表,选择Shortcut(快捷方式)后,Android会列出所有定 义了android.intent.action.CREATE_SHORTCUT的所有应用:

此时如果选择ApiDemos,那么Add to Home Screen会启动LauncherShortcuts Activity,发出请求的Intent的action 会设置为Intent.ACTION_CREATE_SHORTCUT,因此可以在onCreate中使用 Intent.ACTION_CREATE_SHORTCUT.equals(action)来判断请求是来自Add to Home Screen还是用户选择App->Launcher Shorts。如果是来自Add to Home Screen,Launcher Shortcuts则为本Activity创建一个快捷方式setupShortcut,然后退出。

Add to Home Screen 发出Intent请其后(运行startActivityForResult),预期从LauncherShortcuts返回一个结果,这也是为什么 setupShortcut需要返回一个结果。对应创建的快捷方式的Intent至少需要定义三个参数:SHORTCUT_INTENT (value: Intent), SHORTCUT_NAME (value: String),  SHORTCUT_ICON (value: Bitmap)或SHORTCUT_ICON_RESOURCE (value: ShortcutIconResource).

本例是为当前Activity(LauncherShortcuts)创建快捷方式,实际应用中可以根据需要为别的Activity或是提供一个列表供用户来选择需创建的快捷方式。

本例在Home Screen创建一个Sample 快捷方式,选择该快捷方式后,直接进入Launcher Shortcuts,而无需从App再进入Launcher Shortcuts。

【起航计划 022】2015 起航计划 Android APIDemo的魔鬼步伐 21 App->Launcher Shortcuts 为某个非主Activity在Home Screen上建立一个快捷方式的更多相关文章

  1. 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01

    本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitap ...

  2. 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程

    本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...

  3. 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener

    前篇文章Android ApiDemo示例解析(31):App->Preferences->Launching preferences 中用到了Advanced preferences 中 ...

  4. 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面

    我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状 ...

  5. 【起航计划 020】2015 起航计划 Android APIDemo的魔鬼步伐 19 App->Dialog Dialog样式

    这个例子的主Activity定义在AlertDialogSamples.java 主要用来介绍类AlertDialog的用法,AlertDialog提供的功能是多样的: 显示消息给用户,并可提供一到三 ...

  6. 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState

    Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的UI类似,但 ...

  7. 【起航计划 023】2015 起航计划 Android APIDemo的魔鬼步伐 22 App->Menu->Inflate from XML 使用xml资源展示菜单

    本例MenuInflateFromXml.java演示了如何从Menu 资源(XML 定义)展开菜单项.这个例子的onCreate 采用了使用代码来创建Activity 界面的方法 而通常的方法是采用 ...

  8. 【起航计划 017】2015 起航计划 Android APIDemo的魔鬼步伐 16 App->Alarm->Alarm Controller Alarm事件 PendingIntent Schedule AlarmManager

    Alarm Controller演示如何在Android应用中使用Alarm事件,其功能和java.util.Timer ,TimerTask类似.但Alarm可以即使当前应用退出后也可以做到Sche ...

  9. 【起航计划 004】2015 起航计划 Android APIDemo的魔鬼步伐 03 App->Activity->Animation Activity跳转动画 R.anim.×× overridePendingTransition ActivityOptions类

    App->Activity->Animation示例用于演示不同Activity切换时动态效果. android 5.0例子中定义了6种动画效果: 渐变Fade In 缩放Zoom In ...

随机推荐

  1. kuangbin专题十六 KMP&&扩展KMP POJ2752 Seek the Name, Seek the Fame

    The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the l ...

  2. 黑马MySQL数据库学习day04 MySQL变量 存储过程 用户和授权管理

    /* MySQL中的变量局部变量,用户变量,会话变量和全局变量. 用户变量不用定义,直接使用. 1.用户变量赋值 set @xxx = 值; 2.查询 select @xxx; 语法帮助: 过程保存在 ...

  3. 安装maven时遇到的问题

    配置好环境变量和本地仓库后,在命令行里输入mvn compile,可以显示成功,但是mvn install后就一直失败.显示如下错误: 在idea中显示如下错误: 其实即使显示编译成功,但并没有真正的 ...

  4. Java实现微信小程序支付(支付,提现,退款)

    1.添加WXpayCommon类用以具体实现功能,代码如下: package com.karat.cn.wxCommon; import java.io.IOException; import jav ...

  5. 树莓派安装开源智能家居系统 Domoticz

    前言 最近闲来无事开始折腾自己的智能家居系统,对比了几种比较流行的开源智能家居系统,觉得 Domoticz 更适合,Domoticz的官方中文文档,虽然不是很完善但还是可以参考一下.需要注意的是下文用 ...

  6. linux 数据库管理

    1.安装数据库: yum install mariadb.serversystemctl staus mariadbsystemctl start mariadbsystemctl enable ma ...

  7. 如何使用java代码进行视频格式的转换(FLV)

    如何使用java代码进行视频格式的转换(FLV) 一,前言 在给网页添加视频播放功能后,发现上传的视频有各种格式,那么就需要将他么转换成FLV,以很好的支持在线视频播放. 公司一直在使用中,配合使用, ...

  8. 转 windows下安装pycharm并连接Linux的python环境 以及 windows 下notepad ++编辑 linux 的文件

    ######sample 1:windows下安装pycharm并连接Linux的python环境 https://www.cnblogs.com/junxun/p/8287998.html wind ...

  9. CSS 样式分类

    CSS样式可以分为三大类:内联样式.内部样式表和外部样式表 1.内联样式(样式写在html标签里,只对该标签的内容起作用) <span style="color:red;font-si ...

  10. PlayMaker 状态机FSM重用

    注意:playmaker做模板的时候不应该有拖入的东西,这样保存模版后会报错,提示容易丢失东西.