Android应用程序图标快捷方式无法启动活动。

3

我正在尝试在安卓上实现应用程序图标快捷方式。我按照文档进行了操作,但是我在从快捷方式启动应用程序方面遇到了问题。每次单击应用程序图标快捷方式时,都没有任何反应。

以下是我在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>

嗨,我很好奇你是如何在应用程序标记中使用android:name属性启动应用的?当我创建简单项目时,没有这样的属性,当我尝试添加它时,Android Studio会抛出错误:“未找到类...”。 - Николай Гольцев
你应该添加一个继承自Application类的类。然后将该类设置为应用程序的 android:name。请查看文档:https://developer.android.com/guide/topics/manifest/application-element#nm - mgcaguioa
1个回答

1
所以问题出在buildType上。当在非发布版本的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"

感谢这个Stack Overflow问题,它几乎与我的问题相似。还有Rakesh的答案

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接