New Shortcuts :App Shortcuts and Shortcuts

How do you implement this shortcut?

The official implementation steps: App Shortcuts | Android Developers

classification

App Shortcuts are divided into two categories:

  • Static Shortcuts
  • Dynamic Shortcuts

Static shortcuts are written in XML files, while dynamic shortcuts are written in Java code.

implementation

Environmental requirements

  1. Only phones running Android7.1(25) and above can use this feature
  2. The SDK version must be 25 or later
  3. Minimum compatible version 23(because dynamic shortcuts use the Icon class)

The build.gradle configuration for the Module is as follows:

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'
}
Copy the code

Static shortcut

1. Create the XML directory under the res/ folder of the project

2. Create the shortcuts. XML file in the XML directory with the following content

<?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>
Copy the code

Attribute meanings and picture descriptions are as follows:

ShortcutId: indicates the ID of the shortcut enabled: indicates whether the shortcut is available or unavailable. When clicked, the prompt shortcutDisabledMessage icon: indicates the shortcut icon shortcutShortLabel: ShortcutLongLabel: Specifies the title of the shortcut icon when you hold down the icon. ShortcutDisabledMessage: specifies the prompt when the shortcut icon is disabledCopy the code

3. Set a static shortcut in the androidmanifest.xml file

<?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>
Copy the code

Dynamic shortcut

Dynamic shortcuts are shortcuts added and updated in code.

ShortcutManager provides methods to add, remove, update, disable, start, obtain static shortcuts, obtain dynamic shortcuts, and obtain shortcuts fixed to the desktop. Icon shortcuts can be manipulated dynamically in Java code.

Add shortcuts

private void addShortcut(a) {
    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(); }}Copy the code

Remove shortcuts

private void removeShortcut(a) {
    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(); }}Copy the code

Project code

The project code is here :Shortcuts