How to close application shortcut in android? - 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.

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);

Creating a unique shortcut when installing an app behaves differently on different Android version

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.

How to add bookmarks(URL) on the home screen for 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

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" />

Application launcher icon is not deleted from Home screen when uninstalling android app

I'm using a similar codesnippet as shown below to add an application shortcut on the homescreen:
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(this, this.getClass().getName());
shortcutIntent.putExtra(EXTRA_KEY, "ApiDemos Provided This Shortcut");
// Then, set up the container intent (the response to the caller)
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_name));
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
this, R.drawable.app_sample_code);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
// Now, return the result to the launcher
setResult(RESULT_OK, intent);
There is no problem with creating the shortcut, but when uninstalling the app, the shortcut remains on the homescreen. When uninstalling other apps they all seem to also remove their corresponding homescreen shortcuts. This is what i try to achieve with my "created-by-code-shortcut-icon"
Does any of you Android experts here on Stackoverflow know whats needed to remove the app shortcut from the homescreen when the app is uninstalled ?
I found some related threads, but they do not provide me the solution for my problem, but please feel free to catch up:
[0] https://developer.android.com/intl/de/resources/samples/ApiDemos/src/com/example/android/apis/app/LauncherShortcuts.html
[1] Remove application from launcher programatically in Android
[2] How to remove application shortcut from home screen on uninstall automatically?
I think you can try putting this action in the second Intent: "com.android.launcher.action.INSTALL_SHORTCUT"
This works for me, the launcher icon gets installed on the home screen, and when I uninstall the application, the icon is removed. Have been struggling some time with this.
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(this, this.getClass().getName());
Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
this, R.drawable.launcher_icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
sendBroadcast(intent);
Hope this helps.
I had the same problem as well.
Finally, i've figured out that when creating application's shortcut, application's intent must contain the Intent.ACTION_MAIN action, otherwise the shortcut won't be removed from the home screen upon uninstalling the application (not the intent being used for installing the shortcut, which has the com.android.launcher.action.INSTALL_SHORTCUT action) .
Hope it helps.

Categories

Resources