Since Android O, using com.android.launcher.action.INSTALL_SHORTCUT
is deprecated. In the 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 this does not work any more. No home screen shortcut is created. How do I create a home screen shortcut in Android O? In source code it says @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 error that shortcut intent must be provided:
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: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)
Edit:
As suggested in ianhanniballake's answer I set intent and got java.lang.NullPointerException: intent's action must be set
So I tried new Intent("com.android.launcher.action.INSTALL_SHORTCUT")
. The code runs but no shortcut is created.
Edit 2: This is the code that I am running now but I do not see shortcut on homescreen.
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 the permission dialog as below:
but the problem is that it does not appear in the app foreground. It only appears after pressing the back key. It also does not appear on home key press.
Select Start , scroll to the app you want to pin, then press and hold (or right-click) the app. Select More > Pin to taskbar. If the app is already open on the desktop, press and hold (or right click) the app's taskbar icon, and then select Pin to taskbar.
To add shortcut to Home screen, tap and hold an app and select Add shortcut to Home option. Alternatively, drag the icon to the top to add shortcut on Home screen. To delete shortcut on Home screen, tap and hold an app on Home and select Remove shortcut option.
Find the file you want to pin, then tap and hold it to select it. Next, tap on More in the lower right corner of the screen. This opens a menu, where you should select Add to Home screen. In the next screen, tap on Add to Home screen or drag the icon around to place it manually.
add this at the end
shortcutManager.requestPinShortcut ( shortcutInfo , null )
and for shortcutInfo check to set unique Id
ShortcutInfo.Builder mShortcutInfo = new ShortcutInfo.Builder(MainActivity.this, **getString(R.string.Different_String)**);
👍
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With