ActivityNotFoundException:快捷方式无法启动

Ant*_*ton 2 android android-appshortcut

我正在尝试对我的应用程序实施“应用程序快捷方式”,但无法使它们正常工作。我的shortcut.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>
Run Code Online (Sandbox Code Playgroud)

manifest.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>
Run Code Online (Sandbox Code Playgroud)

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

ActivityNotFoundException:快捷方式无法启动

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

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

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

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

PS:今天尝试过Google示例,静态应用程序快捷方式也无法在示例中使用。动态应用程序快捷方式效果很好。

Gok*_*rik 6

除了Matin的答案之外,如果您有多种构建类型可以更改应用程序ID,那么它将无法正常工作。

就我而言,我不得不.debug.qa后缀为应用程序ID。您不能引用字符串引用或在intent元素内部使用变量。

我发现了两种解决方案:

1)您将为shortcuts.xml不同的构建类型创建不同的文件,然后更新targetPackage

例如,

<intent
        android:action="com.example.ACTION"
        android:targetClass="com.example.MainActivity"
        android:targetPackage="com.example.debug"/>
Run Code Online (Sandbox Code Playgroud)

2)有一个插件,可让您applicationId在XML文件中使用变量。

https://github.com/timfreiheit/ResourcePlaceholdersPlugin


Mat*_*lak 5

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