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

781 阅读2分钟
原文链接: blog.devwiki.net

今天给手上的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文件,内容如下



    
        
        
        
    
    

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

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

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



    
        
            
                

                
            
            
        
        
        
    

动态快捷方式

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

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

添加快捷方式

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();
    }
}

移除快捷方式

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();
    }
}

项目代码

项目代码在此处:Shortcuts