如何在 Android 7.1 上正确创建静态应用程序快捷方式?

How to correctly create static App-Shortcut on Android 7.1 ?

背景

Android 7.1 现在有一个名为“AppShortcut" . On the docs, they explain how to create static ones and dynamic ones, and they even have a sample app.

的新功能

问题

我试用了该示例,但我注意到当我单击静态应用程序快捷方式时,它显示了祝酒词 "app isn't installed"。

查看代码,我发现了一个可疑的配置(在 "shortcuts.xml" 文件中):

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
    <shortcut
        android:shortcutId="add_website"
        android:icon="@drawable/add"
        android:shortcutShortLabel="@string/add_new_website_short"
        android:shortcutLongLabel="@string/add_new_website"
        >
        <intent
            android:action="com.example.android.appshortcuts.ADD_WEBSITE"
            android:targetPackage="com.example.android.appshortcuts"
            android:targetClass="com.example.android.appshortcuts.Main"
            />
    </shortcut>
</shortcuts>

看起来不太好,因为应用程序中没有任何内容具有 "ADD_WEBSITE" 的意图操作。

顺便说一句,动态快捷方式工作正常(可以通过 MainActivity 的正常启动添加)。

我试过的

所以我认为这应该改变。我尝试创建一个新的 activity 并更改此配置以匹配 activity(action 和 targetCalss),但出于某种原因,我仍然得到相同的 toast。

问题

代码中可能有什么问题?应该改变什么来修复它?

这很奇怪,但您可以通过更改
来解决这个问题 android:targetPackage = "com.example.android.shortcutsample" (与 applicationid 相同)或
applicationId "com.example.android.appshortcuts"(与包名相同)。

因为你的"com.example.android.appshortcuts.Main"没有这个动作

com.example.android.appshortcuts.ADD_WEBSITE

您可以将此操作更改为

android.intent.action.VIEW