图标长按弹出快捷菜单及跳转

xzbxzb 安卓 2026-01-26 49 0
安卓实现长按App图标弹出快捷菜单并跳转指定页面,核心用App Shortcuts(应用快捷方式),分静态快捷方式(清单配置,无需代码)和动态快捷方式(代码动态添加),以下是可直接使用的完整实现方案:

一、静态快捷方式(推荐,简单高效,适用于固定跳转)

通过AndroidManifest.xml配置,应用安装后即可生效,支持API 25+。

步骤1:配置清单文件(AndroidManifest.xml)

在启动页标签内添加,关联快捷方式资源文件:
<!-- 启动页Activity(通常是MainActivity) -->
<activity
    android:name=".ui.MainActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <!-- 关联App Shortcuts资源 -->
    <meta-data
        android:name="android.app.shortcuts"
        android:resource="@xml/shortcuts" />
</activity>

步骤2:创建快捷方式资源文件

在res/xml/目录下新建shortcuts.xml(无xml目录则手动创建),配置跳转的页面和菜单信息:
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 快捷方式1:跳转“我的行程”页面 -->
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_shortcut_travel" <!-- 菜单图标(建议24x24dp) -->
        android:shortcutId="travel" <!-- 唯一ID,不可重复 -->
        android:shortcutLongLabel="@string/shortcut_long_travel" <!-- 长按显示的长名称 -->
        android:shortcutShortLabel="@string/shortcut_short_travel"> <!-- 短名称(优先显示) -->
        <!-- 跳转的意图:指定目标Activity -->
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.xzb.bolvtravel2.ui.travel.TravelActivity" <!-- 目标页面全类名 -->
            android:targetPackage="com.xzb.bolvtravel2" /> <!-- App包名 -->
        <!-- 若需多个intent,可添加,按顺序执行 -->
        <categories android:name="android.shortcut.conversation" />
    </shortcut>

    <!-- 快捷方式2:跳转“个人中心”页面(可添加多个) -->
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_shortcut_mine"
        android:shortcutId="mine"
        android:shortcutLongLabel="@string/shortcut_long_mine"
        android:shortcutShortLabel="@string/shortcut_short_mine">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.xzb.bolvtravel2.ui.mine.MineActivity"
            android:targetPackage="com.xzb.bolvtravel2" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
</shortcuts>

步骤3:配置字符串和图标(res/values/strings.xml)

<!-- 快捷方式文字 -->
<string name="shortcut_short_travel">我的行程</string>
<string name="shortcut_long_travel">查看我的出行行程</string>
<string name="shortcut_short_mine">个人中心</string>
<string name="shortcut_long_mine">进入我的个人中心</string>
图标要求:使用mipmap目录的矢量图/位图,建议24x24dp,避免过大导致变形。

二、动态快捷方式(适用于动态更新的菜单,如“最近订单”)

通过ShortcutManager代码实现,可在应用运行时添加/删除/更新快捷方式,支持API 25+,核心代码在Application或启动页MainActivity中。

核心代码(Kotlin版,适配你的开发语言)

import android.content.Context
import android.content.Intent
import android.os.Build
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.drawable.Icon
import androidx.annotation.RequiresApi
import com.xzb.bolvtravel2.ui.order.OrderActivity // 目标页面

class MyApp : android.app.Application() {
    override fun onCreate() {
        super.onCreate()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            initDynamicShortcuts() // 初始化动态快捷方式
        }
    }

    @RequiresApi(Build.VERSION_CODES.N_MR1)
    private fun initDynamicShortcuts() {
        val shortcutManager = getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
        // 检查是否支持快捷方式
        if (!shortcutManager.isRequestPinShortcutSupported) return

        // 构建跳转意图
        val orderIntent = Intent(this, OrderActivity::class.java).apply {
            action = Intent.ACTION_VIEW
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }

        // 构建动态快捷方式
        val dynamicShortcut = ShortcutInfo.Builder(this, "dynamic_order")
            .setShortLabel("最近订单") // 短名称
            .setLongLabel("查看我的最近出行订单") // 长名称
            .setIcon(Icon.createWithResource(this, R.mipmap.ic_shortcut_order)) // 图标
            .setIntent(orderIntent) // 跳转意图
            .build()

        // 添加到快捷方式列表(最多5个,静态+动态总数不超过5)
        shortcutManager.dynamicShortcuts = listOf(dynamicShortcut)
    }
}
注意:需在AndroidManifest.xml中注册MyApp:
<application
    android:name=".MyApp"
    ...>
    ...
</application>

三、关键注意事项(必看,避免踩坑)

  1. 数量限制:静态+动态快捷方式总数不超过5个,多余的不会显示。

  2. 页面权限:目标Activity需在AndroidManifest.xml中注册,无需添加,若设置exported,建议为true(API 30+要求)。

  3. API兼容:App Shortcuts最低支持API 25(Android 7.1),低于此版本无效果,无需做兼容(系统会自动忽略)。

  4. 图标和文字:短名称建议不超过10个字,长名称不超过25个字,图标使用纯色简约样式,适配不同桌面主题。

  5. 跳转逻辑:若目标页面需要传参,可在Intent中通过putExtra传递,和普通页面跳转一致。

四、效果测试

  1. 编译运行App,在手机桌面找到App图标;

  2. 长按图标,即可弹出配置的快捷菜单;

  3. 点击菜单选项,直接跳转到指定的Activity页面。

五、进阶:固定快捷方式到桌面(可选)

可通过代码让用户将快捷方式直接固定到桌面(需用户授权),核心代码:
@RequiresApi(Build.VERSION_CODES.O)
fun pinShortcut(context: Context) {
    val shortcutManager = context.getSystemService(ShortcutManager::class.java)
    val intent = Intent(context, TravelActivity::class.java).setAction(Intent.ACTION_VIEW)
    val shortcutInfo = ShortcutInfo.Builder(context, "pin_travel")
        .setShortLabel("我的行程")
        .setIcon(Icon.createWithResource(context, R.mipmap.ic_shortcut_travel))
        .setIntent(intent)
        .build()
    // 发起固定请求,用户确认后会显示在桌面
    shortcutManager.requestPinShortcut(shortcutInfo, null)
}

一为静态,二为动态,按需添加

 您阅读本篇文章共花了: 

版权声明

本文章如果涉及侵权,请联系我。
部分文章系本人原创未经许可,不得转载。

喜欢0发布评论

评论列表

发表评论

  • 昵称(必填)
  • 邮箱
  • 网址