Android 7.0新特性——桌面长按图标出现快捷方式

简介

Android 7.0版本有一个新特性:如果app支持,可以通过长按app图标出现一些快捷操作。一些热门应用举例: 
  
      
实现也比较简单,有两种实现方式:静态配置和动态配置。

一、静态配置

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

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

在工程res目录下添加xml目录,并在xml目录下创建配置文件: 

 
shortcuts.xml配置内容:

<?xml version="1.0" encoding="utf-8"?>
<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中配置才有效!

<application
        <!-- 其他配置项... -->
        <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>
        <!-- 其他配置项... -->
    </application>

 


二、动态配置

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 动态添加和删除演示

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




 


转自:https://blog.csdn.net/china_style/article/details/79570400 

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值