Android 7.0新特性——长按出现快捷方式

    • 简介
    • 一、静态配置
      • 1.1 创建shortcuts.xml配置资源文件
      • 1.2 在Manifest中添加配置
    • 二、动态配置
      • 1.1动态添加
      • 1.2动态删除
      • 1.3 动态添加和删除演示

简介

Android 7.0版本有一个新特性:如果app支持,可以通过长按app图标出现一些快捷操作。一些热门应用举例:
Android 7.0新特性——长按出现快捷方式_第1张图片 Android 7.0新特性——长按出现快捷方式_第2张图片
Android 7.0新特性——长按出现快捷方式_第3张图片 Android 7.0新特性——长按出现快捷方式_第4张图片
(除了Chrome,Google出品的App几乎都是支持这个功能的。)
这和之前的桌面小插件(Widget)功能类似,但是使用起来要更方便一些。实现也比较简单,有两种实现方式:静态配置和动态配置。

一、静态配置

只需要两步:
1. 创建shortcuts.xml配置资源文件;
2. 在Manifest中添加meta-data配置。

1.1 创建shortcuts.xml配置资源文件

在工程res目录下添加xml目录,并在xml目录下创建配置文件:
这里写图片描述
shortcuts.xml配置内容:


<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

    <shortcut
        android:shortcutId="background_settings"
        android:enabled="true"
        android:icon="@drawable/ic_launcher"
        android:shortcutShortLabel="@string/shortcuts_back_short_label"
        android:shortcutLongLabel="@string/shortcuts_back_long_label"
        android:shortcutDisabledMessage="@string/shortcuts_back_disable_msg">

        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.sy.androidofeatures"
            android:targetClass="com.sy.androidofeatures.background.BackgroundTestActivity" />
        <categories android:name="android.shortcut.conversation"/>
    shortcut>

    <shortcut
        android:shortcutId="pip_settings"
        android:enabled="true"
        android:icon="@drawable/ic_launcher"
        android:shortcutShortLabel="@string/shortcuts_pip_short_label"
        android:shortcutLongLabel="@string/shortcuts_pip_long_label"
        android:shortcutDisabledMessage="@string/shortcuts_pip_disable_msg">

        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.sy.androidofeatures"
            android:targetClass="com.sy.androidofeatures.pip.PictureInPictureActivity" />
        <categories android:name="android.shortcut.conversation"/>
    shortcut>

shortcuts>

1.2 在Manifest中添加配置

注意:只能在有action是android.intent.action.MAIN和category是android.intent.category.LAUNCHER的Activity中配置才有效!

... -->
        ".MainActivity">
            
                "android.intent.action.MAIN"/>
                "android.intent.category.LAUNCHER"/>
            

            "android.app.shortcuts"
                android:resource="@xml/shortcuts"/>
        
        
    

二、动态配置

1.1动态添加

fun onClickshortcutsAdd(v: View) {
        var shortcutManager = getSystemService(ShortcutManager::class.java) as ShortcutManager
        var intent = Intent(this, NotificationChannelActivity::class.java)
        intent.action = Intent.ACTION_VIEW
        var shortcut = ShortcutInfo.Builder(this, "noti_channel_demo")
                .setIcon(Icon.createWithResource(this, R.drawable.ic_launcher))
                .setShortLabel("通知渠道")
                .setLongLabel("通知渠道演示")
                .setIntent(intent)
                .build()
        shortcutManager.addDynamicShortcuts(listOf(shortcut))
    }

1.2动态删除

动态配置的快捷方式也可以删除,并且只能删除动态配置的,静态配置的是不能动态删除的!从sdk的api提示就可以知看出,并没有提供删除静态配置项的接口:
这里写图片描述
删除方式:

fun onClickshortcutsDel(v: View) {
        var shortcutManager = getSystemService(ShortcutManager::class.java) as ShortcutManager
        shortcutManager.removeDynamicShortcuts(listOf("noti_channel_demo"))
    }

1.3 动态添加和删除演示

长按出现的快捷方式条目,都还可以长按拖动到桌面,放置为一个单独固定的桌面快捷方式,静态和动态配置的都可以。

你可能感兴趣的:(Android开发)