长按应用图标弹出快捷方式,这个效果是Android 7.1之后新增的一个功能:应用快捷方式ShortcutManager,
官方API地址: https://developer.android.google.cn/reference/android/content/pm/ShortcutManager.html

桌面上长按应用图标弹出应用快捷列表,用户可以通过这些快捷键,直接访问应用具体的界面,并且长按这些快捷键可以在桌面上创建一个该快捷键的应用图标。

创建方式分为静态和动态的两种方式:
静态的: 在xml中定义, 适用于一些通用的动作.
动态的: 由ShortcutManager发布, 可以根据用户的行为或者偏好添加, 可以动态更新.

注:该快捷方式Android 7.1,且每一个应用目前最多可以有5个shortcut。

一、静态创建:在资源文件下创建xml包,再生成一个xml文件

<?xml version="1.0" encoding="utf-8"?><shortcuts xmlns:tools="http://schemas.android.com/tools"    xmlns:android="http://schemas.android.com/apk/res/android">                <shortcut        android:enabled="false"        android:icon="@drawable/ic_baby_head"        android:shortcutId="set"        android:shortcutLongLabel="@string/shortcut_long_set"        android:shortcutShortLabel="@string/shortcut_short_set"        android:shortcutDisabledMessage="@string/shortcut_long_set"        tools:targetApi="n_mr1">        <intent            android:action="android.intent.action.VIEW"            android:targetClass="com.shuniuyun.aiyoumi.ui.mine.SettingActivity"            android:targetPackage="com.shuniuyun.aiyoumi" />    shortcut>    <shortcut        android:enabled="true"        android:icon="@drawable/ic_baby_head"        android:shortcutId="info"        android:shortcutLongLabel="@string/shortcut_long_info"        android:shortcutShortLabel="@string/shortcut_short_info"        android:shortcutDisabledMessage="@string/shortcut_msg_info"        tools:targetApi="n_mr1">        <intent            android:action="android.intent.action.VIEW"            android:targetClass="com.shuniuyun.aiyoumi.ui.mine.PersonalInfoActivity"            android:targetPackage="com.shuniuyun.aiyoumi" />    shortcut>    <shortcut        android:enabled="true"        android:icon="@drawable/ic_baby_head"        android:shortcutId="username"        android:shortcutLongLabel="@string/shortcut_long_name"        android:shortcutShortLabel="@string/shortcut_short_name"        android:shortcutDisabledMessage="@string/shortcut_msg_name"        tools:targetApi="n_mr1">        <intent            android:action="android.intent.action.VIEW"            android:targetClass="com.shuniuyun.aiyoumi.ui.mine.ChangeUsernameActivity"            android:targetPackage="com.shuniuyun.aiyoumi" />    shortcut>shortcuts>

然后在清单文件中进行配置(注意在启动页面下进行配置)

        <activity android:name=".ui.main.StartActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            intent-filter>            <meta-data                android:name="android.app.shortcuts"                android:resource="@xml/shortcuts" />        activity>

二、动态创建:动态是通过 ShortcutManager API 进行操作,可以动态添加、修改、删除

  if (Build.VERSION.SDK_INT >= 25) {            ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, "shortcut_id_search")                    .setShortLabelResId(R.string.lable_shortcut_static_search_disable)                    .setLongLabelResId(R.string.lable_shortcut_static_search_disable)                    .setIcon(Icon.createWithResource(this, R.drawable.ic_search))                    .setIntent(new Intent(this, MainActivity.class))                    .build();            ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);            //这样就可以通过长按图标显示出快捷方式了            shortcutManager.setDynamicShortcuts(Arrays.asList(shortcutInfo));        }

通过ShortcutInfo.Builder新建 ShortcutInfo,再通过shortcutManager添加即可。其中:

  • setDynamicShortcuts(List)可以替换并添加所有 shortcut 列表;
  • addDynamicShortcuts(List)可以添加新的 shortcut 到列表,超过最大个数会报异常;
  • updateShortcuts(List)可以更新一组 shortcuts;
  • removeDynamicShortcuts(List)和removeAllDynamicShortcuts() 可以删除部分或所有
    shortcuts。

更多相关文章

  1. Android动态部署三:如何从插件apk中启动Activity(-)
  2. android String.xlm中使用emoji表情的方法
  3. Android(安卓)Action Bar学习(一)--基本介绍及使用
  4. 软件开发文档(以太网)
  5. android Fragment开发文档翻译 - 1
  6. Android快速开发框架dyh详解(四)---数据层的使用
  7. android listview嵌套gridview动态增加gridview的item项
  8. android studio Activity标题栏研究
  9. 第一章:初入Android大门(不同的Activity之间的数据传递与回调--s

随机推荐

  1. Android(安卓)Studio项目目录结构
  2. setImageResource(),setImageBitmap()和se
  3. android ffmpeg compile error fix notes
  4. android ——设置圆形图片
  5. Android(安卓)Color Palette
  6. android wifi测试小程序
  7. THE 10-STEP GUIDE TO ANNOTATION PROCES
  8. WebView与Android之间的交互(js有效,传值)
  9. RingTest
  10. Android(安卓)之 ListView使用BaseAdapte