How to create shortcuts on Android O when they are less targeted?

Background

Android O has various shortcut changes:

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

Problem

According to recent changes on Android O, the intent of the broadcast 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

No longer broadcasting com.android.launcher.action.INSTALL_SHORTCUT has any effect on your application since it is now a private, implied broadcast. Instead, you should create an application shortcut using requestPinShortcut () from the ShortcutManager class.

This means that this code, for example, will no longer work, regardless of which application you have made or where the user is running:

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 cannot create shortcuts for applications (at least in the current version: 80795200). It just does nothing, even to launch Google.

Question

Although I am very opposed to this API change (and wrote about it here , here ) and here , I would like to know what it takes to still work.

I know there is an API for this using requestPinShortcut , but this requires the application to be designed for Android Output, which means more changes need to be made to make sure the application works there.

My question is: Suppose your application is for Android API 25, how do you create shortcuts on Android O? Is it possible to use reflection of the new API? If so, how?

+5
source share
1 answer

The correct way is to call the requestPinShortcut method. You do not need to configure Android-O, but you need to at least compile the SDK to 26. Compiling the SDK and the target SDK are two different things.

+4
source

Source: https://habr.com/ru/post/1269193/


All Articles