欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > Android >内容正文

Android

Android7.1图标快捷方式(AppShortcuts)实现Demo

发布时间:2025/5/22 Android 136 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Android7.1图标快捷方式(AppShortcuts)实现Demo 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

今天给手上的Nexus6P手机升级系统至Android7.1,体验了一下新功能:App Shortcuts(图标快捷方式),如下图所示:

如何实现这样的快捷方式呢?

官方给出的实现步骤:App Shortcuts | Android Developers

分类

图标快捷方式(App Shortcuts)分为两种:

  • Static shortcuts(静态快捷方式)

  • Dynamic shortcuts(动态快捷方式)

静态快捷方式是写在xml文件中,而动态快捷方式是在Java代码中编写.

实现

环境要求

  • 只有Android7.1(25)及以上手机才能使用该功能

  • 要求SDK版本为25及以上

  • 最低兼容版本为23(因为动态快捷方式使用Icon类)

  • module 的build.gradle配置如下:

    apply plugin: 'com.android.application'android {compileSdkVersion 25buildToolsVersion "25.0.0"defaultConfig {applicationId "net.devwiki.shortcuts"minSdkVersion 23targetSdkVersion 25versionCode 1versionName "1.0"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}} }dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])compile 'com.android.support:appcompat-v7:25.0.0'testCompile 'junit:junit:4.12' }

    静态快捷方式

    1.在项目的res/文件夹下建立 xml目录

    2.在xml目录创建shortcuts.xml文件,内容如下

    <?xml version="1.0" encoding="utf-8"?> <shortcuts xmlns:android="http://schemas.android.com/apk/res/android"><shortcutandroid:shortcutId="compose"android:enabled="true"android:icon="@drawable/ic_share_red_700_48dp"android:shortcutShortLabel="@string/activity_short_label"android:shortcutLongLabel="@string/activity_long_label"android:shortcutDisabledMessage="@string/activity_disable_message"><intentandroid:action="android.intent.action.VIEW"android:targetPackage="net.devwiki.shortcuts"android:targetClass="net.devwiki.shortcuts.StaticActivity" /><!-- If your shortcut is associated with multiple intents, include themhere. The last intent in the list is what the user sees when theylaunch this shortcut. --><categories android:name="android.shortcut.conversation" /></shortcut><!-- Specify more shortcuts here. --> </shortcuts>

    属性含义和图片说明如下:

    shortcutId : 快捷方式的ID enabled : 是否可用,不可用时点击Toast提示shortcutDisabledMessage icon : 快捷方式图标 shortcutShortLabel : 快捷图标放置到桌面时的标题 shortcutLongLabel : 长按图标出现快捷方式时的标题 shortcutDisabledMessage : 快捷图标禁用时的提示语

    3.在AndroidManifest.xml文件中设置静态快捷方式

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="net.devwiki.shortcuts"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><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><activity android:name=".StaticActivity"></activity></application> </manifest>

    动态快捷方式

    动态快捷方式即在代码中添加,更新快捷图标.

    ShortcutManager提供了添加,移除,更新,禁用,启动,获取静态快捷方式,获取动态快捷方式,获取固定在桌面的快捷方式等方法.
    可在Java代码中动态对图标快捷方式进行操作.

    添加快捷方式

    private void addShortcut() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {List<ShortcutInfo> infoList = shortcutManager.getDynamicShortcuts();String shortcutId = null;for (ShortcutInfo shortcutInfo : infoList) {if (ShortcutsId.WEB_DEVWIKI.equals(shortcutInfo.getId())) {shortcutId = shortcutInfo.getId();}}if (shortcutId == null) {ShortcutInfo shortcut = new ShortcutInfo.Builder(this, ShortcutsId.WEB_DEVWIKI).setShortLabel(getString(R.string.blog_short_label)).setLongLabel(getString(R.string.blog_long_label)).setIcon(Icon.createWithResource(this, R.drawable.ic_blog_logo)).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.devwiki.net"))).build();shortcutManager.addDynamicShortcuts(Arrays.asList(shortcut));Toast.makeText(this, R.string.add_shortcut_success, Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, R.string.shortcut_already_exist, Toast.LENGTH_SHORT).show();}} else {Toast.makeText(this, R.string.not_support_function, Toast.LENGTH_SHORT).show();} }

    移除快捷方式

    private void removeShortcut() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {List<ShortcutInfo> infoList = shortcutManager.getDynamicShortcuts();String shortcutId = null;for (ShortcutInfo shortcutInfo : infoList) {if (ShortcutsId.WEB_DEVWIKI.equals(shortcutInfo.getId())) {shortcutId = shortcutInfo.getId();}}if (shortcutId == null) {Toast.makeText(this, R.string.shortcut_not_exist, Toast.LENGTH_SHORT).show();} else {shortcutManager.removeDynamicShortcuts(Arrays.asList(shortcutId));Toast.makeText(this, R.string.remove_shortcut_success, Toast.LENGTH_SHORT).show();}} else {Toast.makeText(this, R.string.not_support_function, Toast.LENGTH_SHORT).show();} }

    项目代码

    项目代码在此处:Shortcuts

    总结

    以上是生活随笔为你收集整理的Android7.1图标快捷方式(AppShortcuts)实现Demo的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。