Android: is there a way to program the web shortcut on the home screen?

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

What I want to do:

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

+8
android
Apr 15 2018-11-11T00:
source share
3 answers

First you will need to add permission to your manifest.xml

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

You will need to create an intention to view the web page. Something like...

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

You can verify this by creating a small test application and running startActivity (i); This should open the browser. After you confirm the correctness of the above intention, proceed to the next step.

Now you will need to set a 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 is also possible that some home screens do not accept this, but most of them. So enjoy it.

EDIT: The icon can be set to a shortcut using:

 installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(mContext, R.drawable.icon)); 
+28
Apr 15 2018-11-11T00:
source share

As a complement to the correct answer @ Mike-dg and @Gagan instead of using

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

which requires ShortcutIconResource, you can use

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

which allows you to use any bitmap as an icon. This makes it easy to use the website shortcut icon as an icon.

0
Dec 16 '15 at 16:49
source share

You cannot place user interface elements on the phone’s main screen via .apk. For a web shortcut - you can create a widget that opens a browser (with a specified URL) after a click.

-one
Apr 15 2018-11-11T00:
source share



All Articles