Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create shortcuts on Android O, when targetting less than it?

Background

Android O has various changes of how shortcuts work:

https://developer.android.com/preview/behavior-changes.html#as

The problem

According to recent changes on Android O, the broadcast intent to create shortcuts is completely ignored :

https://developer.android.com/reference/android/content/Intent.html#ACTION_CREATE_SHORTCUT https://developer.android.com/preview/behavior-changes.html#as

The com.android.launcher.action.INSTALL_SHORTCUT broadcast no longer has any effect on your app, because it is now a private, implicit broadcast. Instead, you should create an app shortcut by using the requestPinShortcut() method from the ShortcutManager class.

This means this code, for example, won't work anymore, no matter which app you've made or which launcher the user has:

private void addShortcut(@NonNull final Context context) {
    Intent intent = new Intent().putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(context, MainActivity.class).setAction(Intent.ACTION_MAIN))
            .putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut")
            .putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, ShortcutIconResource.fromContext(context, R.mipmap.ic_launcher))
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    context.sendBroadcast(intent);
}

Currently, even the Play Store itself fails to create shortcuts to apps (at least in the current version: 80795200 ) . It just doesn't do anything, even to Google's launcher.

The question

While I'm very against this change of API (and wrote about it here, here) and here, I'd like to know what would it take to still make it work.

I know there is an API for this, using requestPinShortcut, but this requires the app to target Android O, which means a lot more changes have to be made to make sure the app works there.

My question is: Suppose your app targets Android API 25, how do you create shortcuts on Android O ? Is it possible by using reflection of the newer API ? If so, how?

like image 759
android developer Avatar asked Jun 25 '17 07:06

android developer


People also ask

How do I add text shortcuts to my keyboard on Android?

Using Samsung Galaxy Open the Settings app. Scroll down and tap General management. Tap Language and input. Tap On-screen keyboard. Tap Samsung Keyboard. Tap Smart typing. Tap Text Shortcuts. Tap Add. Type a shortcut. Type a phrase. Tap Add.

How many shortcuts are there in Android Studio?

Typically, Android System accepts multiple shortcuts (programmed inside the app), but for the UX, it shows only 4 of them. To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

How do I create a two- or three-letter typed shortcut on Android?

This wikiHow teaches you how to create a two- or three-letter typed shortcut for a full phrase or sentence on an Android. Open the Settings app. It's the blue-grey cog icon in your App Drawer. Swipe up from the bottom of the screen to access the App Drawer. You can also swipe down from the top of the screen and tap the cog icon.

Is it possible to create shortcuts in Android 11?

The option to create shortcuts in Android is still present in Android 11 and although it is not well known by many, it sure saves you time and effort in an option that you use very often.


2 Answers

It seems like I beat on this forever, but...

if(Build.VERSION.SDK_INT < 26) {
    ...
}
else {
    ShortcutManager shortcutManager
        = c.getSystemService(ShortcutManager.class);
    if (shortcutManager.isRequestPinShortcutSupported()) {
        Intent intent = new Intent(
            c.getApplicationContext(), c.getClass());
        intent.setAction(Intent.ACTION_MAIN);
        ShortcutInfo pinShortcutInfo = new ShortcutInfo
            .Builder(c,"pinned-shortcut")
            .setIcon(
                Icon.createWithResource(c, R.drawable.qmark)
            )
            .setIntent(intent)
            .setShortLabel(c.getString(R.string.app_label))
            .build();
        Intent pinnedShortcutCallbackIntent = shortcutManager
            .createShortcutResultIntent(pinShortcutInfo);
        //Get notified when a shortcut is pinned successfully//
        PendingIntent successCallback
            = PendingIntent.getBroadcast(
                c, 0
                , pinnedShortcutCallbackIntent, 0
            );
        shortcutManager.requestPinShortcut(
            pinShortcutInfo, successCallback.getIntentSender()
        );
    }
}

Is working for me. I know there were changes in 7.1 and don't know if this works for them and I don't know about the launcher issues mentioned above.
This was tested on a Samsung Galaxy Tab S3 running Android 8.0.0.

I put a simple app that does nothing but install a shortcut for itself on your homepage on github. It works for versions before and after Android 8. Pre Android 8 uses the sendBroadcast method and after creats a pinned shortcut.

like image 73
steven smith Avatar answered Sep 25 '22 18:09

steven smith


The right way is to call requestPinShortcut method. You don't need to target android O but you need to have at least compile SDK to 26. Compile SDK and target SDK are two different things.

like image 27
greywolf82 Avatar answered Sep 25 '22 18:09

greywolf82