I want to make it easy to add website shortcut to home screen by pressing a button. So What I am Thinking is a button at the bottom of my hybrid app that says "Add to home screen ? yes or no" and when "yes" pressed, it adds the shortcut to the home screen without closing the application. what code should I add To do that?
here is the code that creates a web browser shortcut:
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Goto http://www.example.com/link");
Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.shortcut_icon);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new
Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com/link")));
sendBroadcast(shortcutIntent);
here is the permission you need to add:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
Related
I am trying to create an app which will create multiple shortcut of the same app with user input as the shortcut name. but every shortcut will do different specified things.For this I need to know the shortcut name.any possible way to fo that?
First you need the INSTALL_SHORTCUT permission
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
You can use the code below and make changes
EditText UserInput = (EditText)findViewById(R.id.userinput_text);
final String UserString = UserInput.getText().toString();
//Gets user input^
public void createIcon(){
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//So the shortcut doesn't get created multiple times
shortcutintent.putExtra("duplicate", false);
//UserString is the text from the user input to set the shortcut name
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(UserString));
//set the icon picture
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new
//Set the Activity that will be opened
Intent(getApplicationContext(), EnterActivity.class));
sendBroadcast(shortcutintent);
}
That will add a shortcut to the launcher and open a custom activity from your app. I'm not sure what you're really trying to do as it's unclear of what you're asking for.
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.
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));
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.
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.
I use the following codes to create a shortcut when installing an app:
in AndroidManifest.xml:
<!-- for creating a shortcut in the home screen -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
in onCreate() of the main activity:
// an Intent to create a shortCut
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//repeat to create is forbidden
shortcutIntent.putExtra("duplicate", false);
//set the name of shortCut
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, this.getString(R.string.app_name));
//set icon
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//set the application to lunch when you click the icon
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT
, new Intent(getApplicationContext() , MainActivity.class));
//sendBroadcast,done
sendBroadcast(shortcutIntent);
These codes work fine in Android 4.0.4, which creates an shortcut at the first time and send a toast saying the shortcut already exists after the 1st time installation. But in Android 4.2.2, I can create many duplicated shortcuts by clicking the back key and enter the app again.
Is there any way to work on both version of Android ?
Thanks in advance :)
shortcutIntent.putExtra("duplicate", false) is not working in 4.2 version. i think you have to use some other ways to create unique shortcut.
If failour then just uninstall the shortcut and install again.something like :
addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
and in manifest use both UNINSTALL_SHORTCUT and INSTALL_SHORTCUT permissions.
And this rough idea is nicely working on every versions.
I want to remove all the shortcuts on home screen of my app on a single event like button click.
Is there any way to do this ? or at least get the display-names of all the shortcuts placed on home screen from my app ?
( I cant have it in any shared-prefs / db while creation of shortcuts because user can even do a 'clear-data' )
Try this:
appShortcutIntent = new Intent();
appShortcutIntent.setClassName("com.pkg.pkgname", "MainActivity");
appShortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appIcon = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
Intent intentDeleteShortcut = new Intent();
intentDeleteShortcut.putExtra("android.intent.extra.shortcut.INTENT", appShortcutIntent);
intentDeleteShortcut.putExtra("android.intent.extra.shortcut.NAME", getResources().getString(R.string.app_name));
intentDeleteShortcut.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", appIcon);
intentDeleteShortcut.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
sendBroadcast(intentDeleteShortcut);
And add this permission in manifest file:
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
Hope this helps.
(P.S: This will require the device to be rooted as far as I know.)
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.
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));
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.
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.