2

我正在尝试为我的应用程序实现应用程序快捷方式,但我无法让它们工作。我的快捷方式.xml:

<Shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
    android:shortcutId="shortcut"
    android:enabled="true"
    android:icon="@drawable/ic_shortcut"
    android:shortcutShortLabel="@string/shortcut_label">
    <intent
        android:action="android.intent.action.VIEW"
        android:targetPackage="com.example"
        android:targetClass="com.example.package.Activity"/>
</shortcut>

清单.xml:

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

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

    <activity
        android:name=".package.Activity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

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

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

所以,我做了与谷歌示例相同的快捷方式。我可以看到并单击快捷方式,但没有任何反应。我尝试了不同的活动,更改了活动位置、活动操作、参数,例如“exported=true”,但没有任何变化 - 在日志中我只能看到

ActivityNotFoundException:快捷方式无法启动

我的快捷方式日志和谷歌相机快捷方式日志之间的唯一区别是活动路径:

I/ActivityManager: 启动 u0 {act=android.intent.action.VIEW flg=0x1000c000 cmp=com.example/.package.Activity}

对比

I/ActivityManager:启动 u0 {act=android.media.action.STILL_IMAGE_CAMERA flg=0x1000c000 cmp=com.google.android.GoogleCamera/com.android.camera.activity.CameraImageActivity}

Google cam 有完整的活动路径,但包路径不同。

PS:今天尝试了谷歌示例,静态应用快捷方式在示例中也不起作用。动态应用快捷方式运行良好。

4

4 回答 4

8

In addition to Matin's answer, if you have multiple build types that change your application id, it won't work.

In my case, I had .debug and .qa suffix for application id. You can't reference to string reference or use variables inside intent element.

I found two solutions:

1) You will create different shortcuts.xml file for different build types and update your targetPackage.

For example,

<intent
        android:action="com.example.ACTION"
        android:targetClass="com.example.MainActivity"
        android:targetPackage="com.example.debug"/>

2) There is a plugin that allows you to use applicationId variable in your XML file.

https://github.com/timfreiheit/ResourcePlaceholdersPlugin

于 2018-01-03T19:49:31.017 回答
5

android:targetPackage必须包含您的应用程序 ID。因此,如果您已经定义了您必须使用的应用build.gradle程序applicationId "com.my.app.id"IDandroid:targetPackage="com.my.app.id"

于 2017-06-29T14:31:42.397 回答
0

我也遇到了这个问题。然后我发现 android:targetClass 不是正确的路径。android:targetClass="com.example.camille.testappshortcuts.Main" Main 是应用的访问权限。也许您应该将 Activity 更改为另一个名称,然后添加它。

于 2017-04-25T09:02:42.070 回答
0

将 Activity 上的包名称更改为:

android:name=".Activity"

将您的快捷方式目标类更改为:

android:targetClass="com.example.Activity"
于 2017-02-17T15:46:45.847 回答