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



    
        
        
        
    
    

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



    
        
            
                

                
            
            
        
        
        
    

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() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { List 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() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { List 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