首页 > 解决方案 > Android应用程序图标快捷方式未启动活动

问题描述

我正在尝试在 Android 上实现应用程序图标快捷方式。我遵循了文档,但从快捷方式启动应用程序时遇到问题。每次我单击应用程序图标快捷方式时,都没有任何反应。这是我AndroidManifest.xml文件中的代码:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.package.myapp">

<application
    android:name=".MyApp"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme.NoActionBar"
    android:usesCleartextTraffic="${usesCleartextTraffic}"
    tools:ignore="ExportedService,GoogleAppIndexingWarning,UnusedAttribute">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.app.shortcuts"
            android:resource="@xml/shortcuts" />
    </activity>

    ...

</application>
</manifest>

这是shortcuts.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="UnusedAttribute">

    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_send"
        android:shortcutId="send_funds"
        android:shortcutLongLabel="@string/LBL_SEND_FUNDS"
        android:shortcutShortLabel="@string/BTN_SEND">

        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.package.myapp.MainActivity"
            android:targetPackage="com.package.myapp" />
    </shortcut>

</shortcuts>

标签: androidandroid-appshortcut

解决方案


所以问题就在buildType. 在发布版本以外的 s 上运行时buildType,我遇到了问题,因为包名称与快android:targetPackage​​捷方式的意图上指示的名称不同。所以我所做的是,我在各自的构建文件夹中创建了多个shortcuts.xml不同的:targetPackage

app/src/debug/res/xml/shortcuts.xml
app/src/dev/res/xml/shortcuts.xml
app/src/staging/res/xml/shortcuts.xml

android:targetPackage分别设置:

android:targetPackage="com.package.myapp.debug"
android:targetPackage="com.package.myapp.dev"
android:targetPackage="com.package.myapp.staging"

感谢这个与我的问题几乎相似的SO question 。而对于Rakesh 的回答


推荐阅读