Create shortcut on home screen in Android O

Starting with Android O, use is com.android.launcher.action.INSTALL_SHORTCUTnot recommended. In previous versions, I used this and it worked.

Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), MainActivity.class));
sendBroadcast(shortcutintent);

but now it no longer works. A home screen shortcut is not created. How to create a home screen shortcut in Android O? The source code says that @deprecated Replaced with {@link android.content.pm.ShortcutManager#createShortcutResultIntent}. So, I tried this:

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo.Builder mShortcutInfo = new ShortcutInfo.Builder(MainActivity.this, getString(R.string.app_name));
mShortcutInfo.setShortLabel(getString(R.string.app_name));
mShortcutInfo.setLongLabel(getString(R.string.app_name));
mShortcutInfo.setIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher));
shortcutManager.createShortcutResultIntent(mShortcutInfo.build());

I get an error saying the shortcut must be specified:

10-17 23:08:00.305 13256-13256/com.audiorecorder.wel.voicerecorder E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.wel.shortcut, PID: 13256
                                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wel.shortcut/com.wel.shortcut.MainActivity}: java.lang.NullPointerException: Shortcut Intent must be provided
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                     at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                     at android.os.Looper.loop(Looper.java:164)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                                  Caused by: java.lang.NullPointerException: Shortcut Intent must be provided
                                                                                     at android.os.Parcel.readException(Parcel.java:1948)
                                                                                     at android.os.Parcel.readException(Parcel.java:1888)
                                                                                     at android.content.pm.IShortcutService$Stub$Proxy.createShortcutResultIntent(IShortcutService.java:635)
                                                                                     at android.content.pm.ShortcutManager.createShortcutResultIntent(ShortcutManager.java:1043)
                                                                                     at voicerecorder.wel.audiorecorder.com.voicerecorder.MainActivity.onCreate(MainActivity.java:80)
                                                                                     at android.app.Activity.performCreate(Activity.java:6975)
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892at android.app.ActivityThread.-wrap11(Unknown Source:0at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593at android.os.Handler.dispatchMessage(Handler.java:105at android.os.Looper.loop(Looper.java:164at android.app.ActivityThread.main(ActivityThread.java:6541at java.lang.reflect.Method.invoke(Native Method) 
                                                                                     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767

Edit: as suggested in ianhanniballake's answer, I set the intent and received java.lang.NullPointerException: intent action must be setso I tried new Intent("com.android.launcher.action.INSTALL_SHORTCUT"). Code executes, but no shortcut is created.

Edit 2: this is the code that I am running now, but I do not see the shortcut on the desktop.

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo.Builder mShortcutInfo = new ShortcutInfo.Builder(MainActivity.this, getString(R.string.app_name));
mShortcutInfo.setShortLabel(getString(R.string.app_name));
mShortcutInfo.setLongLabel(getString(R.string.app_name));
mShortcutInfo.setIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher));
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_CREATE_SHORTCUT);
shortcutIntent.putExtra("duplicate", false);
mShortcutInfo.setIntent(shortcutIntent);
sendBroadcast(shortcutManager.createShortcutResultIntent(mShortcutInfo.build()));

Edit 3:

ShortcutInfo.Builder mShortcutInfoBuilder = new ShortcutInfo.Builder(MainActivity.this, getString(R.string.app_name));
mShortcutInfoBuilder.setShortLabel(getString(R.string.app_name));
mShortcutInfoBuilder.setLongLabel(getString(R.string.app_name));
mShortcutInfoBuilder.setIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher));
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_CREATE_SHORTCUT);
mShortcutInfoBuilder.setIntent(shortcutIntent);
ShortcutInfo mShortcutInfo = mShortcutInfoBuilder.build();
ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);
mShortcutManager.requestPinShortcut(mShortcutInfo, null);

This brings up a permission dialog, as shown below:

Permisison dialog

, . . home.

+7
4

shortcutManager.requestPinShortcut ( shortcutInfo ,  null )

shortcutInfo

ShortcutInfo.Builder mShortcutInfo = new ShortcutInfo.Builder(MainActivity.this, **getString(R.string.Different_String)**);

πŸ‘

+2

private void createShortcut() {
ShortcutManager shortcutManager = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
    shortcutManager = mContext.getSystemService(ShortcutManager.class);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    if (shortcutManager != null) {
        if (shortcutManager.isRequestPinShortcutSupported()) {
            ShortcutInfo shortcut = new ShortcutInfo.Builder(mContext, uniqueid)    
                    .setShortLabel("Demo")
                    .setLongLabel("Open the Android Document")
                    .setIcon(Icon.createWithResource(mContext, R.drawable.andi))
                    .setIntent(new Intent(Intent.ACTION_VIEW,
                            Uri.parse("https://stackoverflow.com")))
                    .build();

            shortcutManager.requestPinShortcut(shortcut, null);
        } else
            Toast.makeText(mContext, "Pinned shortcuts are not supported!", Toast.LENGTH_SHORT).show();
    }
}}

, MAX, , , - , , ..

+1

, , , , , :

, , setIntent Edit 3. , , Intent.ACTION_CREATE_SHORTCUT. .

The fix for editing 3 will look like this:

ShortcutInfo.Builder mShortcutInfoBuilder = new ShortcutInfo.Builder(MainActivity.this, getString(R.string.app_name));
mShortcutInfoBuilder.setShortLabel(getString(R.string.app_name));
mShortcutInfoBuilder.setLongLabel(getString(R.string.app_name));
mShortcutInfoBuilder.setIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher));
mShortcutInfoBuilder.setIntent(new Intent(getApplicationContext(), MainActivity.class));
ShortcutInfo mShortcutInfo = mShortcutInfoBuilder.build();
ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);
mShortcutManager.requestPinShortcut(mShortcutInfo, null);

createShortcutResultIntent actually for something completely different when you want to call a callback function when the creation of a pinned shortcut was successful.

+1
source

You need to call setIntent () :

mShortcutInfo.setIntent(new Intent(getApplicationContext(), MainActivity.class));
0
source

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


All Articles