9

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.

teleme.io
  • 815
  • 3
  • 10
  • 21

3 Answers3

31

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));
Gagan
  • 1,497
  • 1
  • 12
  • 27
Mike dg
  • 4,648
  • 2
  • 27
  • 26
  • 1
    hi Mike, your solution rocks! many thanks! One more question: is there anyway to check if any shortcut of target url has already been placed on the user's home screen. The situation is: we have a few apks. each of them will place a shortcut to our online apk store. so before placing a shortcut, I wish to check if one is already exists. Many thanks – teleme.io Apr 19 '11 at 03:19
  • 1
    ty, not that I am aware of. Sorry. – Mike dg Apr 19 '11 at 12:40
  • 4
    This technique is not recommended. This is an internal implementation, not part of the Android SDK. It will not work on all home screen implementations. It may not work on all past versions of Android. It may not work in future versions of Android, as Google is not obligated to maintain internal undocumented interfaces. Please do not use this. – CommonsWare Jun 13 '11 at 23:58
  • 2
    It's an intent, nothing happens if there isn't anything that can accept it, it's the same as using any third party app. Sometimes something can receive that broadcast, other times it can't. – Mike dg Jun 14 '11 at 13:10
  • on i.setData("http://www.blablaba.com"); it says The method setData(Uri) in the type Intent is not applicable for the arguments (String). any idea? – Guy Sep 03 '12 at 08:54
  • 1
    Answering my own question: Uri.parse(string); – Guy Sep 03 '12 at 09:01
  • instead of `fromContext(mContext` I've used `fromContext(getBaseContext()` – Pedro Lobito Jun 15 '13 at 17:56
1

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.

Soren Stoutner
  • 1,413
  • 15
  • 21
-1

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.

sddsadsf
  • 250
  • 1
  • 2