Ali
Ali

Reputation: 859

Create shortcut on home screen in Android O

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:

Permisison dialog

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.

Upvotes: 8

Views: 6163

Answers (4)

evilscientress
evilscientress

Reputation: 181

I know this thread is ancient but it's among the first result when you google for the deprecation so I thought this might help others:

The person asking is setting a wrong intent in setIntent in Edit 3. You are supposed to specify an intent that is called when the short cut is pressed, not an Intent.ACTION_CREATE_SHORTCUT intend. ianhanniballake already pointed this out.

A fix for edit 3 would 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 is for something very different, it's for when you want to get an callback function called when the creation of the pinned shortcut was successful.

Upvotes: 2

Sunil Varma
Sunil Varma

Reputation: 39

You Can Use This Method

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();
    }
}}

I am using this in my app but only one restriction it only creates Two shortcut at MAX don't know why if any one found way to create more then please mention here..

Upvotes: 2

Elias Fazel
Elias Fazel

Reputation: 2113

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)**);

👍

Upvotes: 2

ianhanniballake
ianhanniballake

Reputation: 199825

You have to call setIntent():

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

Upvotes: 0

Related Questions