Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add HomeScreen WebApps from another application? [duplicate]

Tags:

android

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.

like image 334
teleme.io Avatar asked Apr 15 '11 11:04

teleme.io


2 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));
like image 171
Mike dg Avatar answered Oct 23 '22 05:10

Mike dg


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.

like image 27
Soren Stoutner Avatar answered Oct 23 '22 04:10

Soren Stoutner