Android desktop long press icon shortcut - Shortcuts

Introduction

When we press and hold the desktop icon of an Android application, a list usually pops up, which generally has functions such as application information and uninstalling applications, and some applications also add their own shortcuts here. Today we mainly introduce how to add custom shortcuts.

The shortcut displayed by long pressing the desktop is called Shortcut in Android . It has two creation methods, divided into static creation and dynamic creation, and like Widget, it also has a corresponding management class for management. Don't talk nonsense, let's start directly

static creation

first step

Static creation is divided into two steps. The first step is to specify the location of the shortcut configuration file in the Manifest file. First, find the activity tag of the Launcher and add the meta-data tag as follows:

<activity
   android:name=".MainActivity"
    android:exported="true">
    <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>

second step

Create an xml folder under the resources resource directory, and create an xml file named shortcuts in the folder, the content of which is as follows: We created two tags

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher_round"
        android:shortcutDisabledMessage="@string/disable_hint_message"
        android:shortcutId="shortcut1"
        android:shortcutLongLabel="@string/long_label_one"
        android:shortcutShortLabel="@string/short_label_one">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.helloworld.RecyclerViewTestActivity"
            android:targetPackage="com.example.helloworld">
            <extra
                android:name="key1"
                android:value="value1" />
        </intent>
    </shortcut>

    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher_round"
        android:shortcutDisabledMessage="@string/disable_hint_message"
        android:shortcutId="shortcut2"
        android:shortcutLongLabel="@string/long_label_two"
        android:shortcutShortLabel="@string/short_label_two">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.helloworld.TestViewPagerActivity"
            android:targetPackage="com.example.helloworld">
            <extra
                android:name="key2"
                android:value="value2" />
        </intent>
    </shortcut>
</shortcuts>

<string name="short_label_one">名称一</string>
<string name="short_label_two">名称二</string>
<string name="long_label_one">一个很长很长的长到你无法相信的名称一</string>
<string name="long_label_two">一个略长略长的长到你可以想象的名称二</string>
<string name="disable_hint_message">你确认要禁用该快捷方式吗?</string>

  • enabled Whether to open
  • icon icon
  • shortcutDisabledMessage Disable the shortcut message
  • shortcutId unique identifier
  • shortcutLongLabel Long label, display first
  • shortcutShortLabel Short label, if the long label cannot be displayed, the short label will be displayed
  • Intent Jump to Activity's Intent

Precautions

Static ShortCuts created through xml cannot be dynamically modified and deleted through code, but can only be modified through xml.

dynamically created

Use ShortCuts-related classes and methods to quickly implement operations such as adding, changing, and deleting dynamic shortcuts.

object ShortCutManager {

    fun createShortCuts(context: Context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            ShortcutManagerCompat.removeAllDynamicShortcuts(context)
            val shortCut = ShortcutInfoCompat.Builder(context, "shortCutId")
                .setShortLabel("搜索一下")
                .setLongLabel("搜索一下下")
                .setIcon(IconCompat.createWithResource(context, R.mipmap.ic_launcher))
                .setIntent(Intent(context,MainActivity::class.java).apply {
                    action = Intent.ACTION_VIEW
                })
                .build()
            ShortcutManagerCompat.pushDynamicShortcut(context, shortCut)
        }
    }
}

Call the above method at the appropriate place to add ShortCuts. Before adding, all dynamic shortcuts are deleted, but the static shortcuts configured through xml will not be deleted.
insert image description here


---------------------
Author: Sruur
Source: CSDN
Original text: https://blog.csdn.net/weixin_42643321/article/details/129854463
Copyright statement: This article is the original article of the author, please attach the blog post link for reprinting!
Content analysis By: CSDN, CNBLOG blog post one-click reprint plug-in

Guess you like

Origin blog.csdn.net/xiaowang_lj/article/details/131805483