What's new in Android 7.0 - long press for shortcuts

A new feature of Android 7.0 is that if the app supports it, you can press and hold the app icon for a shortcut. For example, some popular apps include:

This is similar to the previous desktop Widget, but it is more convenient to use. The implementation is also relatively simple. There are two implementation methods, static configuration and dynamic configuration

Static configuration

There are only two steps:
1. Create the shortcut.xml configuration resource file;
2. Add the meta data configuration to the Manifest.

Create the shortcut.xml configuration resource file

Add the xml directory in the project res directory, and create the configuration file in the xml Directory:

Shortcut.xml configuration content:

<?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>

Add configuration in Manifest

Note: it is only valid in activities with action being android.intent.action.MAIN and category being android.intent.category.LAUNCHER!

<application
        <!-- Other configuration items... -->
        <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>
        <!-- Other configuration items... -->
    </application>

Dynamic configuration

The configuration code is as follows:

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("Notification channel")
                .setLongLabel("Notification channel presentation")
                .setIntent(intent)
                .build()
        shortcutManager.addDynamicShortcuts(listOf(shortcut))
    }

Shortcuts of dynamic configuration can also be deleted, and only those of dynamic configuration can be deleted. Those of static configuration cannot be deleted!
Delete by:

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

Tags: Android xml Java encoding

Posted on Tue, 31 Mar 2020 14:53:31 -0700 by logicsound