在Nougat 7.1.1中,当点击应用程序快捷方式时出现未安装应用程序错误。

10
我在为现有应用添加静态应用程序快捷方式时遇到了一些问题。我遵循了来自https://developer.android.com/guide/topics/ui/shortcuts.html的步骤,虽然快捷方式显示出来了,但是当我点击它时,它并没有启动活动,而是显示一个弹出消息,说:“App未安装”。

这是清单文件中相关的部分:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".activities.SplashActivity"
            android:noHistory="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>

        <activity
            android:name=".activities.MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBar" />

        <activity
            android:name=".activities.ListActivity"
            android:label="@string/title_activity_list"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
            android:value="com.mypackage.activities.MainActivity" />
        </activity>

        <activity
            android:name=".activities.NewActivity"
            android:label="@string/title_activity_new"
            android:parentActivityName=".activities.ListActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.mypackage.activities.ListActivity" />
        </activity>
    <application/>
</manifest>

这是shortcuts.xml文件:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="shortcut_new_alarm"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="short label"
        android:shortcutLongLabel="long label"
        android:shortcutDisabledMessage="message">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.mypackage"
        android:targetClass="com.mypackage.activities.NewActivity" />
        <!-- If your shortcut is associated with multiple intents, include them
         here. The last intent in the list determines what the user sees when
         they launch this shortcut. -->
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <!-- Specify more shortcuts here. -->
</shortcuts>

我已经仔细检查过目标活动的完全限定名称com.mypackage.activities.NewActivity,一切正常。提前致谢!

1
NewActivity不是您的启动器活动,这就是为什么您会收到此消息的原因。 - Maveňツ
创建SplashActivity的快捷方式有什么意义呢?我需要的是快捷方式将我带到NewActivity而不是SplashActivity。如果您查看我提供的官方文档链接,它们使用com.example.myapplication.ComposeActivity作为targetActivity,而启动器活动是Main。那就是我需要的行为。 - Alejandro Casanova
请在您完成答案后更新。 - Akash kumar
4个回答

24

如果在build.gradle文件中设置了不同的productFlavors,则应确保android:targetPackage的值为应用程序IDandroid:targetClass应包含包名。

例如:

<shortcut
    android:shortcutId="shortcut_id_xxx"
    android:enabled="true"
    android:icon="@drawable/shortcut_xxx"
    android:shortcutShortLabel="@string/shortcut_xxx">
    <intent
        android:action="android.intent.action.VIEW"
        android:targetPackage="com.example.app.internal"
        android:targetClass="com.example.app.MainActivity" />
    <categories android:name="android.shortcut.conversation" />
</shortcut>

productFlavors {
    internal {
        applicationId 'com.example.app.internal'
        ...
    }
}

对于您的内部版本,targetPackage 应为 com.example.app.internal,targetClass 应为 com.example.app.MainActivity


@Pierre,你应该为不同的构建类型添加不同的shortcuts.xml文件。 - weei.zh
2
如何使它动态? 在xml中吗?我已经尝试过使用${applicationId},但它显然使用了完整的包名称。 - Aditi Parikh

4

android:targetPackage="com.mypackage"改为您在应用程序/gradle中所使用的应用程序ID。希望这可以帮助您。


3
如果您使用不同的构建类型,例如 debug,并添加了 applicationIdSuffix,则要使用的targetPackageapplicationId+applicationIdSuffix。 例如:
applicationId“com.mypackage”
applicationIdSuffix“.debug” android:targetPackage="com.mypackage.debug"
- Roberto

3
只需在AndroidManifest.xml下的目标活动标签中添加android:exported="true",并包含INSTALL_SHORTCUT权限。
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
...

<activity android:name=".activity.MainActivity"
        android:exported="true" />

0

当 Splash 是主要启动 Activity 时,您正在注册您的 NewActivity

 <activity
        android:name="com.mypackage.SplashActivity"
        android:label="@string/app_name"
          android:exported="true"
        android:screenOrientation="portrait" >
        <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>

所以只需要删除noHistoty并添加android:exported="true"

这是shortcuts.xml文件:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
    android:shortcutId="shortcut_new_alarm"
    android:enabled="true"
    android:icon="@mipmap/ic_launcher"
    android:shortcutShortLabel="short label"
    android:shortcutLongLabel="long label"
    android:shortcutDisabledMessage="message">
    <intent
        android:action="android.intent.action.VIEW"
        android:targetPackage="com.mypackage"
    android:targetClass="com.mypackage.activities.NewActivity"/>
    <!-- If your shortcut is associated with multiple intents, include them
     here. The last intent in the list determines what the user sees when
     they launch this shortcut. -->
    <categories android:name="android.shortcut.conversation" />
</shortcut>
<!-- Specify more shortcuts here. -->
</shortcuts>

我理解你的观点,但是为什么我需要为SplashActivity创建一个快捷方式呢?我需要的是快捷方式将我带到NewActivity而不是SplashActivity。如果您查看官方文档的链接,它们使用com.example.myapplication.ComposeActivity作为targetActivity,而启动器活动是Main。这就是我需要的行为。 - Alejandro Casanova
1
在你提出的更改(删除noHistoty并添加android:exported="true")应用于清单文件后,问题仍然存在。如果我将包含快捷方式信息的元数据移动到NewActivity中,则根本不会显示快捷方式。这种解决方案无法修复该问题。 - Alejandro Casanova

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