2

I'm trying to implement App Shortcuts to my app, but i can't get them work. My 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>

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>

So, i do shortcut same as in google example. I can see and click on shortcut, but nothing happens. I've tried different activities, changing activity location, activity action, params, like "exported=true", but nothing changes - in logs i can see only

ActivityNotFoundException: Shortcut could not be started

The only difference between my shortcut log, and google camera shortcut log is path to activity:

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

vs

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 has full path to activity, but package path differs.

PS: tried today google sample, static app shortcuts doesn't work in sample too. Dynamic app shortcuts work well.

Anton
  • 251
  • 3
  • 13

4 Answers4

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

Gokhan Arik
  • 2,626
  • 2
  • 24
  • 50
5

android:targetPackage have to contain your application ID. So if you have defined your app id in build.gradle as applicationId "com.my.app.id" you have to use android:targetPackage="com.my.app.id"

Matin Petrulak
  • 1,087
  • 15
  • 23
0

Change the package name on your Activity to:

android:name=".Activity"

Change your shortcut target class to:

android:targetClass="com.example.Activity"
tonyo.dev
  • 692
  • 1
  • 5
  • 12
  • The reason why you would get the ActivityNotFoundException is if the Activity is not located in the package that it is pointing to in the manifest. What is the package name you see at the top of the Activity java file? – tonyo.dev Feb 17 '17 at 15:50
  • com.example.package, just like in path. I have tried to write full activity path, nothing changes. – Anton Feb 17 '17 at 15:51
0

I also met this problem. Then i found that android:targetClass is not right path. android:targetClass="com.example.camille.testappshortcuts.Main" Main is app's access. Maybe you should change Activity to another name, then add it.

Camille
  • 26
  • 4