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

Google Keep 图标快捷方式

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

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

分类

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

  • Static shortcuts(静态快捷方式)
  • Dynamic shortcuts(动态快捷方式)

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

实现

环境要求

  1. 只有Android7.1(25)及以上手机才能使用该功能
  2. 要求SDK版本为25及以上
  3. 最低兼容版本为23(因为动态快捷方式使用Icon类)

module 的build.gradle配置如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "net.devwiki.shortcuts"
        minSdkVersion 23
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles 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">
    <shortcut
        android: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">
        <intent
            android: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 them
             here. The last intent in the list is what the user sees when they
             launch 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">
    <application
        android: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