Home » Android » Android: Is there a programming way to create a web shortcut on home screen

Android: Is there a programming way to create a web shortcut on home screen

Posted by: admin June 15, 2020 Leave a comment

Questions:

Do you know is there a programmatic way to create a web shortcut on the phone user’s home screen?

What I want to do is:

When the phone user clicks a button in our Android application, the application will then place a website shortcut onto the phone user’s home screen.

How to&Answers:

First you’ll need to add a permission to your manifest.xml

<uses-permission
            android:name="com.android.launcher.permission.INSTALL_SHORTCUT">
</uses-permission>

You’ll need to build an intent to view the webpage. Something like…

Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.blablaba.com"));

You can test this by creating a little test app and doing startActivity(i); This should open the browser. Once you verified that the above intent is correct you should move on to the next step.

Now you’ll need to actually install the shortcut.

    Intent installer = new Intent();
    installer.putExtra("android.intent.extra.shortcut.INTENT", i);
    installer.putExtra("android.intent.extra.shortcut.NAME", "THE NAME OF SHORTCUT TO BE SHOWN");
    installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", I THINK this is a bitmap); //can also be ignored too
    installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(installer)

;

It’s also possible some home screens don’t accept this, but most do. So enjoy.

EDIT:
Icon can be set to the shortcut using:

installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(mContext, R.drawable.icon));

Answer:

As an addition to the correct answer by @Mike-dg and @Gagan, instead of using

putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(mContext, R.drawable.icon))

which requires a ShortcutIconResource, you can use

putExtra("android.intent.extra.shortcut.ICON", Bitmap))

which lets you use any bitmap as the icon. This makes it easy to use a the favicon of the shortcut’s website as the icon.

Answer:

You can’t place UI elements on the phone’s home screen through an .apk. For the web shortcut – you can create a widget that opens the browser (with the specified URL) upon click.