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

1.9k views Asked by At

Background

Android 7.1 now has a new feature called "AppShortcut" . On the docs, they explain how to create static ones and dynamic ones, and they even have a sample app.

The problem

I tried out the sample, yet I've noticed that when I click on the static app-shortcut, it shows me a toast "app isn't installed".

enter image description here

Looking at the code, I've found a suspicious configuration (in "shortcuts.xml" file) :

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

It doesn't look good, because nothing in the app has this intent action of "ADD_WEBSITE" .

The dynamic shortcuts work fine btw (can be added via the normal launch of MainActivity).

What I tried

So I thought this should be changed. I tried to create a new activity and change this configuration to match the activity (action and targetCalss), but for some reason I still got the same toast .

The question

What could be wrong in the code? What should be changed to fix it?

2

There are 2 answers

1
quanlt On BEST ANSWER

It's strange, but you can fix this by changing
android:targetPackage = "com.example.android.shortcutsample" (same as applicationid) or
applicationId "com.example.android.appshortcuts" (same as package name).

0
Trinea On

Because your "com.example.android.appshortcuts.Main" don't has this action

com.example.android.appshortcuts.ADD_WEBSITE

You can change this action to

android.intent.action.VIEW