一、 摘要

以Android O为分界,介绍两种动态创建快捷方式的途径:广播和ShortcutManager。

二、 Android O以前

在Android O(8.0)以前,动态创建快捷方式是通过发送广播实现的:

// 由该action可知,我们的创建快捷方式广播会由launcher,也就是系统桌面来接收

public static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";

public void createShortcutBelowO(Context ctx, String name, Bitmap icon) {

Intent shortcutIntent = new Intent(ACTION_INSTALL_SHORTCUT);

// 快捷方式的名字

shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);

// 快捷方式的bitmap尽可能小,因为广播内容超过2MB会抛出异常

shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);

// 设置是否允许重复创建快捷方式,该选项非必填,默认是允许

shortcutIntent.putExtra("duplicate", false);

// 快捷方式执行的intent,比如启动应用在AndroidManifest中配置的入口Activity

Intent launchIntent = new Intent();

launchIntent.setClass(ctx, "com.zengyu.shortcutdemo");

shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);

ctx.sendBroadcast(shortcutIntent);

}

三、 Android O以后

Android O(8.0)新增了一个叫ShortcutManager的类:

/**

* The ShortcutManager manages an app's shortcuts. Shortcuts provide users

* with quick access to activities other than an app's main activity in the currently-active

* launcher. For example,

* an email app may publish the "compose new email" action, which will directly open the

* compose activity. The {@link ShortcutInfo} class contains information about each of the

* shortcuts themselves.

*/

ShortcutManager管理应用的快捷方式。快捷方式为用户提供了一个访问应用的快速渠道。ShortcutInfo类包含了每个快捷方式的信息。

在ShortcutManager的API文档中,其实已经有详细的静态创建和动态创建的介绍,以及创建的具体步骤,但在此我还是以一个简单的例子,直观地展示使用ShortcutManager动态创建:

public void createShortcutAboveO(Context ctx, String name, Bitmap icon) {

ShortcutManager shortcutManager = (ShortcutManager) ctx.getSystemService(Context.SHORTCUT_SERVICE);

/**

* 判断是否支持该方式动态创建

*/

if (shortcutManager.isRequestPinShortcutSupported()) {

// 快捷方式执行的intent,比如启动应用在AndroidManifest中配置的入口Activity

Intent launchIntent = new Intent();

launchIntent.setClass(ctx, "com.zengyu.shortcutdemo");

ShortcutInfo.Builder builder = new ShortcutInfo.Builder(context, pkg)

.setShortLabel(name)

.setIcon(Icon.createWithBitmap(icon))

.setIntent(launchIntent);

/**

* 第二个参数为弹出创建快捷方式确认框时的回调PendingIntent,此例不关注该回调,因此为null,

* 如果需要监听该回调,需要自定义一个BroadcastReceiver,可参考参考文献中的例子

*/

shortcutManager.requestPinShortcut(builder.build(), null);

}

}

其中isRequestPinShortcutSupported方法:

/**

* Return {@code TRUE} if the app is running on a device whose default launcher supports

* {@link #requestPinShortcut(ShortcutInfo, IntentSender)}.

*

*

The return value may change in subsequent calls if the user changes the default launcher

* app.

*

*

Note: See also the support library counterpart

* {@link android.support.v4.content.pm.ShortcutManagerCompat#isRequestPinShortcutSupported(

* Context)}, which supports Android versions lower than {@link VERSION_CODES#O} using the

* legacy private intent {@code com.android.launcher.action.INSTALL_SHORTCUT}.

*

* @see #requestPinShortcut(ShortcutInfo, IntentSender)

*/

public boolean isRequestPinShortcutSupported() {

try {

return mService.isRequestPinItemSupported(injectMyUserId(),

LauncherApps.PinItemRequest.REQUEST_TYPE_SHORTCUT);

} catch (RemoteException e) {

throw e.rethrowFromSystemServer();

}

}

判断设备是否支持ShortcutManager的这种方式动态创建快捷方式,如果Android版本低于Android O,使用“com.android.launcher.action.INSTALL_SHORTCUT”这个intent,即我们在上一节中使用的action。因此,如果这个方法返回false,我们应该仍使用广播的方式来动态创建。

四、 参考文献

Android 8.0 快捷方式Shortcut

android 动态添加快捷方式,Android动态创建快捷方式相关推荐

  1. Qt封装TDMS文件实现动态添加组以及动态写入通道数据

    TDMS文件: TDMS文件是NI主推的一种二进制记录文件,TDMS文件由三个层次结构级别组成:文件.组.通道.在NI的LabVIEW软件中,可通过许多接口直接访问NI TDMS文件,但使用LABVI ...

  2. Vue中 动态添加class写法 动态静态clas混合

    Vue中 动态添加class写法 动态静态clas混合 示例 //fx,cont-block为静态class ob为动态class <div :class="[ 'fx','cont- ...

  3. android 动态添加颜色,Android绘制一个三角形并且可动态改变颜色

    方法一: 这种方法的三角形角度没法控制,因为其实是矩形旋转. android:fromDegrees="45" android:pivotX="135%" an ...

  4. android动态添加圆,Android开发中TextView 实现右上角跟随文本动态追加圆形红点

    在一个比较坑的需求里,一段文字右上角需要追加一个圆形红点.最右侧有个金额,红点动态随着文字移动,然后各种摆布局,一下午坑死我了.后来果断放弃.然后就想试试直接自定义view来实现这个需求. 最坑的就是 ...

  5. android动态添加标签,android – 动态添加Textview

    在布局文件中,我有以下内容: android:layout_width="100dp" android:layout_height="wrap_content" ...

  6. 动态添加综合布局---动态添加控件及将某XML动态加入到Activity显示(续)

    前言:以前曾写过一篇关于动态生成控件的文章<动态添加控件及将某XML动态加入到Activity显示>,比较浅显,对于RelativeLayout的相关布局设置方法及相对布局与线性布局的混合 ...

  7. android如何添加gif,Android加载Gif和ImageView的通用解决方案:android-gif-drawable(1)...

     Android加载Gif和ImageView的通用解决方案:android-gif-drawable(1) Android自己的ImageView或者View不能直接加载运行Gif图片,如果要在 ...

  8. C#操作快捷方式(获取快捷方式属性、创建快捷方式)

    第一步  创建一个项目 无需废话,跳过. 第二步  引用COM组件 右键"引用","添加引用",选择"COM组件",找到"Wind ...

  9. android view添加背景,android – 如何将视图作为背景添加到surfaceView?

    嗨我目前正在制作游戏,其中包含SurfaceView背景中音频效果可视化的视图. surfaceView包含实际的游戏. 我发布了一些代码片段: – main.xml中 android:layout_ ...

  10. linux脚本创建快捷方式,批处理BAT创建快捷方式

    发个有用的东西--批处理创建快捷方式.非我原创,这里整理一下,希望对大家有用. 一.批处理生成快捷方式: 1.可以直接生成: echo [InternetShortcut]>>" ...

最新文章

  1. TCP超时与重传机制与拥塞避免
  2. 妇女在IT安全工作人员当中的比例只有10%
  3. loader.asm 注释
  4. JZOJ__Day 2:【NOIP普及模拟】分数
  5. VTK:网格之SplitPolyData
  6. 音视频技术开发周刊 | 240
  7. ambari 2.5.0源码编译安装
  8. C++ 复制字符串/字符数组
  9. 大数据认知计算在内容安全管控中的应用
  10. Mac上关于shell使用Python3和C++11声明
  11. Thinkpad T61/R61/X61安装XP驱动流程
  12. 获取指定进程所对应的可执行(EXE)文件全路径(代码)
  13. msxml6 C++
  14. Oracle 大数据量查询优化
  15. FFmpeg - Windows下使用MSYS2和VS编译FFmpeg
  16. WESAD:情绪分类多模态传感器数据集
  17. 《数值分析(原书第2版)》—— 导读
  18. EEA为以太坊以隐私为主的Web应用发布标准化架构栈
  19. width,height为多少px时,A4纸打印时刚好一页?(转载)
  20. TDDI/ITD原理总结(touch 自容原理)-----/*自己总结*/

热门文章

  1. luogu P2600 [ZJOI2008]瞭望塔
  2. 世事一场大梦,人生几度秋凉?
  3. mysql怎么创建信息表_怎么在MySQL创建表
  4. 优雅的绕过校园网认证实现免费上网
  5. 侵入式及非侵入式概念
  6. 华为云早报 印度政府拟要求 Google、Facebook 等在本地存储数据
  7. docker: Error response from daemon: driver failed programming external connectivity on endpoint
  8. 【软件简史】怎样理解 Alan Kay 曾在1984 年写道:“我们希望像以前编辑文档一样编辑我们的工具” 这句话 —— LLM 将如何影响软件的创建?
  9. 动量和马科维茨Markowitz投资组合(Portfolio)模型实现
  10. ZenCart 使用Facebook账号登陆