How to add bookmarks(URL) on the home screen for android - android

I have tried adding a bookmark on the home screen manually. Need some help on doing it programatically. Thanks.

This works for me -
Add to permission to manifest
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Use the following code:
public void createGoogleSearchShortcut(Context context) {
String urlStr = String.format(context.getString(R.string.homescreen_shortcut_search_url), context.getString(R.string.app_id));
Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlStr));
// shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.search));
// Set the custom shortcut icon
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_action_search));
intent.putExtra("duplicate", false);
// add the shortcut
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(intent);
}
Please note: I strongly suggest asking the user before adding the bookmark. Adding bookmarks to his home screen without his permission is "not polite"...

I'm not sure what you mean by bookmark. Maybe this tutorial could help?
http://www.tutorialforandroid.com/2009/04/open-urlwebsite-from-android.html
This is an application that starts up the browser pointing to a web page. Is this what you had in mind?
Emmanuel

Check out the (poorly documented) action INSTALL_SHORTCUT. You'll need the respective permission, as explained in that link

Related

Android - adding shortcut to homescreen

When I use the firebox browser on my android device, I observe they have an option to bookmark a page such that it appears as shortcut (like an app) in the android launcher.
For example:
Here's before I open firefox:
In firefox I navigate to a page and then select the option at the bottom:
When I go back to the launcher, I can see a new icon. Clicking that icon allows me to go that page directly in firefox.
Does anyone know how this is achieved in the app ?
This can be done using the following:
Intent shortcutIntent = new Intent();
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutTitle);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, shortcutIcon);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);
shortcutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
MyActivity.this.sendBroadcast(shortcutIntent);
With the following permission set in the manifest:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
In your case of opening a specific URL, your target intent will contain the intent of the activity you wish to launch, and could also have some extras that would contain the desired URL.
try this,
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, R.string.app_name);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.app_icon));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate
getApplicationContext().sendBroadcast(addIntent);

How to avoid creation of home screen widget for your application?

I have an application on google play. Once user download and installs, google play use to create a home screen widgets automatically (google play->settings->Auto-add widgets is checked). Due to some technical reasons I want to avoid this creation.
Is it possible to avoid this auto creation of home screen widget programmatically(through manifest or with any other options)?
Any answers will be highly appreciated, thanks in advance.
Removing shortcut in Android uses an Intent (UNINSTALL_SHORTCUT) to
perform the task.
So first add this permission to your manifest:
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
and in java code
private void removeShortcut() {
//Deleting shortcut for MainActivity
//on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
So now you can call it on ur first launch (may be when ur splash screen is loading, if you have any). Now its upto you how you utilize this :)
For more info.. http://viralpatel.net/blogs/android-install-uninstall-shortcut-example/
https://gist.github.com/zeuxisoo/1008973

How can I add my application's shortcut to the homescreen upon app installation? [duplicate]

This question already has answers here:
How can I place app icon on launcher home screen?
(6 answers)
Closed 5 years ago.
I want to create my app's shortcut/launcher icon on the homescreen as soon as I install my app (even before I start it). Is that possible? How might I do that?
Since ICS, you can do like this:
public void createShortCut(){
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), EnterActivity.class));
sendBroadcast(shortcutintent);
}
Please also refer to the source code of launcher at: this link
Edit : If somebody would miss reading comment so adding following line.
This requires "com.android.launcher.permission.INSTALL_SHORTCUT" permission
You forgot to add permissions:
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission
android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
Great tutorial here:
http://androidforbegineers.blogspot.in/2014/01/android-home-screen-shortcut.html#!
NOTE: This answer is now wrong. See, for instance, Robin's answer for a way to do this.
As far as I know, an app cannot force itself onto the home screen. It gets added to the app list that the launcher app maintains, but the home screen is generally under user control. Giving apps the ability to clutter up the home screen would be an open invitation for abuse.
If you install your application, neither your application, nor any services or other processes are active! Android want's the user to make the first step in "opening" the application. You are not able to install a shortcut directly! Thats the answer. Notice: On most devices the launcher will create a shortcut for your application itself.
Can you install shortcuts in runtime after the user has somehow started your app at least once? : yes (see Robins answer).
Is there a workaround?
Maybe, but no good one.
Update, Another hint: If you have already an application on the device of the user. (For example if your second app that gets installed is the key for "going pro" or something), then you actually can start the second application from the first one, without the user has started the second app ever (and add a shortcut).
Create function to call intent of shortcut:
private void criarAtalho() {
Intent shortcutIntent = new Intent(getApplicationContext(), SplashScreen.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "nomeDaApp");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate
getApplicationContext().sendBroadcast(addIntent);
}
write on OnCreat the call of function:
if(!getSharedPreferences("APP_PREFERENCE", Activity.MODE_PRIVATE).getBoolean("IS_ICON_CREATED", false)){
criarAtalho();
getSharedPreferences("APP_PREFERENCE", Activity.MODE_PRIVATE).edit().putBoolean("IS_ICON_CREATED", true).commit();
}
set the permissions on manifest:
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
First, add permission for adding shortcut to manifest:
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Then, call the function below.
public void createShortcut(){
Intent intentShortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
Parcelable appicon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher);
intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, appicon);
intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), MainActivity.class));
intentShortcut.putExtra("duplicate", false);
sendBroadcast(intentShortcut);
}

How to close application shortcut in android?

Hi i am creating application shortcut to my application. My application shortcut is in the front of home screen. When i close the application at that time how to close/delete application shortcut. I need source code. Please any one help me. Thanks.
Code is here. Help me.
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
Parcelable icon = Intent.ShortcutIconResource.fromContext(
getApplicationContext(), R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
new Intent(getApplicationContext() , FirstScreenActivity.class));
sendBroadcast(shortcutintent);
To uninstall shortcut you can use Intent with "com.android.launcher.action.UNINSTALL_SHORTCUT" action. You should have "com.android.launcher.permission.UNINSTALL_SHORTCUT" permission in the manifest also.
Intent uninstallShortcutIntent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
uninstallShortcutIntent.putExtra("duplicate", false);
uninstallShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
uninstallShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
new Intent(getApplicationContext() , FirstScreenActivity.class));
sendBroadcast(uninstallShortcutIntent);
I haven't test it. Get it from the launcher sources: AndroidManifest.xml and UninstallShortcutReceiver
Note: AFAIK, it may not work in some third-party launchers, it is not part of Android SDK.

Desktop Icon link

I would like to know if there is an option of setting an auto icon link of my application in the user's desktop, after installing it?
The only way I know to do it, is that the user could drag it manually to his desktop from the applications list. Is there any way of doing it automaticlly for the user(withouth his touch) ?
Thanks,
Moshic.
Please don't do that automatically!!!
Let the user choose wether or not he want your shortcut!
Here is the code you need:
//Create shortcutIntent here with the intent that will launch you app.
Intent shortcutIntent = (...)
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,sName);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
// add the shortcut
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
Don't forget an extra permission in the Manifest!
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

Categories

Resources