You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Activity类创建多图标应用:仅主入口显示在应用列表

实现单应用多主屏幕图标且仅主入口显示在应用列表

可以实现你要的效果,但不能仅通过AndroidManifest.xml的intent-filter配置达成——应用列表的图标由带有android.intent.category.LAUNCHER的组件决定,而主屏幕快捷方式无法自动关联非LAUNCHER组件。正确的实现思路是:

  1. 仅让MainActivity保留LAUNCHER类别,确保它作为唯一入口出现在应用列表。
  2. 通过代码手动为SecondActivity、ThirdActivity、FourthActivity创建主屏幕快捷方式,这些快捷方式只会显示在主屏幕,不会出现在应用列表。

步骤1:修正AndroidManifest.xml配置

移除三个子Activity的intent-filter(因为它们不需要作为启动入口,仅需被快捷方式调用),只保留exported="true"以允许外部启动:

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.MultipleApps"
    tools:targetApi="31">
    <!-- 主入口:仅这个Activity出现在应用列表 -->
    <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>
    </activity>
    <!-- 子Activity:仅被快捷方式调用,无应用列表图标 -->
    <activity
        android:name=".SecondActivity"
        android:label="@string/source_01"
        android:icon="@android:drawable/ic_delete"
        android:exported="true" />

    <activity
        android:name=".ThirdActivity"
        android:label="@string/source_02"
        android:icon="@android:drawable/ic_btn_speak_now"
        android:exported="true" />

    <activity
        android:name=".FourthActivity"
        android:label="@string/source_03"
        android:icon="@android:drawable/ic_lock_lock"
        android:exported="true" />
</application>

步骤2:添加快捷方式创建代码

在MainActivity中添加创建快捷方式的逻辑,首次启动应用时自动创建三个子Activity的主屏幕快捷方式(用SharedPreferences避免重复创建)。需要兼容Android 8.0前后的不同API:

1. 添加权限(仅针对Android 7.1及以下)

在AndroidManifest.xml的根节点下添加:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

2. 编写创建快捷方式的方法

import android.content.Intent
import android.content.SharedPreferences
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat

class MainActivity : AppCompatActivity() {
    private lateinit var prefs: SharedPreferences

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        prefs = getSharedPreferences("app_prefs", MODE_PRIVATE)
        val shortcutsCreated = prefs.getBoolean("shortcuts_created", false)
        if (!shortcutsCreated) {
            createActivityShortcuts()
            prefs.edit().putBoolean("shortcuts_created", true).apply()
        }
    }

    private fun createActivityShortcuts() {
        // 创建SecondActivity快捷方式
        createShortcut(
            id = "second_shortcut",
            label = getString(R.string.source_01),
            iconRes = android.R.drawable.ic_delete,
            targetClass = SecondActivity::class.java
        )

        // 创建ThirdActivity快捷方式
        createShortcut(
            id = "third_shortcut",
            label = getString(R.string.source_02),
            iconRes = android.R.drawable.ic_btn_speak_now,
            targetClass = ThirdActivity::class.java
        )

        // 创建FourthActivity快捷方式
        createShortcut(
            id = "fourth_shortcut",
            label = getString(R.string.source_03),
            iconRes = android.R.drawable.ic_lock_lock,
            targetClass = FourthActivity::class.java
        )
    }

    private fun createShortcut(id: String, label: String, iconRes: Int, targetClass: Class<*>) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Android 8.0+ 使用ShortcutManagerCompat
            val shortcut = ShortcutInfoCompat.Builder(this, id)
                .setShortLabel(label)
                .setLongLabel(label)
                .setIcon(IconCompat.createWithResource(this, iconRes))
                .setIntent(Intent(this, targetClass).apply {
                    action = Intent.ACTION_MAIN
                    flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
                })
                .build()

            if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
                ShortcutManagerCompat.requestPinShortcut(this, shortcut, null)
            } else {
                Toast.makeText(this, "当前 launcher 不支持创建快捷方式", Toast.LENGTH_SHORT).show()
            }
        } else {
            // Android 7.1及以下 使用广播创建
            val shortcutIntent = Intent(this, targetClass).apply {
                action = Intent.ACTION_MAIN
                flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            }

            val installIntent = Intent("com.android.launcher.action.INSTALL_SHORTCUT").apply {
                putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent)
                putExtra(Intent.EXTRA_SHORTCUT_NAME, label)
                putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this@MainActivity, iconRes))
                putExtra("duplicate", false) // 避免重复创建
            }

            sendBroadcast(installIntent)
        }
    }
}

说明

  • 这种方式创建的快捷方式是用户可手动删除的,卸载重装应用后需要重新创建。
  • 部分定制化launcher可能对快捷方式创建有限制,需测试兼容性。
  • Android 8.0+创建快捷方式时系统会强制弹出确认对话框,无法静默创建;Android 7.1及以下可通过广播静默创建(需launcher支持)。

内容的提问来源于stack exchange,提问作者thinkoutofidea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 12:44:59