10

I´m having some issues when adding an static app shortcut to an existing app. I followed the steps from https://developer.android.com/guide/topics/ui/shortcuts.html and the shortcut shows up, but when I tap it it doesn't launches the activity, instead it shows a toast message saying: "App isn´t installed".

Here is the relevant section of the manifest:

<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>

Here is the shortcuts.xml file:

<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>

I´ve already double checked and the target activity full qualified name com.mypackage.activities.NewActivity is ok.

Thanks in advance!

Alejandro Casanova
  • 3,633
  • 4
  • 31
  • 46
  • 1
    NewActivity is not your launcher activity that is why you are getting this message. – Maveňツ Dec 14 '16 at 04:56
  • What´s the point in creating a shortcut for SplashActivity? What I need is the shortcut to take me to NewActivity instead of SplashActivity. If you check the link I provided with the official documentation they use as targetActivity com.example.myapplication.ComposeActivity while the launcher activity is Main. That´s the behaviour I need. – Alejandro Casanova Dec 14 '16 at 16:32
  • Please update if you got the answer done – Akash kumar Dec 04 '17 at 17:03
  • [Proper answer given in following link, resolved my query](https://stackoverflow.com/a/48695894/11228388) – Smita Sonavane Mar 22 '19 at 10:45

4 Answers4

24

If you set the different productFlavors in build.gradle, you should make sure android:targetPackage is application id, android:targetClass should include package name.

eg:

<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'
        ...
    }
}

in here for your internal version, targetPackage should be com.example.app.internal, targetClass should be com.example.app.MainActivity

weei.zh
  • 271
  • 2
  • 8
4

Change your android:targetPackage="com.mypackage" to your application id present in your app/gradle. Hope this will help you.

Nicks
  • 3,188
  • 3
  • 25
  • 29
  • 3
    If you use a different buildTypes, e.g. `debug` and you added `applicationIdSuffix` then the targetPackage to use is `applicationId`+`applicationIdSuffix`. Example: `applicationId "com.mypackage"` `applicationIdSuffix ".debug"` `android:targetPackage="com.mypackage.debug` – Roberto Feb 07 '19 at 15:04
3

Just add android:exported="true" to your target activity tag under AndroidManifest.xml and also include INSTALL_SHORTCUT permission

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
...

<activity android:name=".activity.MainActivity"
        android:exported="true" />
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Giddy Naya
  • 4,237
  • 2
  • 17
  • 30
0

You'r registering your NewActivity while Splash is the Main Launcher Activity.

 <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>

So just need to remove noHistoty and add android:exported="true"

Here is the shortcuts.xml file:

<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>
Maveňツ
  • 1
  • 12
  • 50
  • 89
  • I understand your point, but why should I need to create a shortcut for the SplashActivity? What I need is that the shortcut to take me to NewActivity instead of SplashActivity.If you check the link of the official documentation they use as targetActivity com.example.myapplication.ComposeActivity while the launcher activity is Main. That´s the behaviour I need. – Alejandro Casanova Dec 14 '16 at 16:30
  • 1
    After the changes you are porposing (removing noHistoty and adding android:exported="true") to the manifest the issue remains. If I move the meta-data with the shortcuts info to NewActivity the shortcut doesn´t shows at all. This solution doesn´t fixes the issue. – Alejandro Casanova Dec 16 '16 at 15:28