Android adds methods for of to create delete and determine if a desktop shortcut exists

  • 2020-06-19 11:44:06
  • OfStack

This article illustrated how Android can add (create), delete, and determine if a desktop shortcut exists. Share to everybody for everybody reference. Specific implementation methods are as follows:


/**
*  Determine if a shortcut has been added to the desktop 
* 
* @param cx
* @param titleName
*  Shortcut name 
* @return
*/
public static boolean hasShortcut(Context cx) {
boolean result = false;
//  Gets the current application name 
String title = null;
try {
final PackageManager pm = cx.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(cx.getPackageName(),
PackageManager.GET_META_DATA)).toString();
} catch (Exception e) {
}
final String uriStr;
if (android.os.Build.VERSION.SDK_INT < 8) {
uriStr = "content://com.android.launcher.settings/favorites?notify=true";
} else {
uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
}
final Uri CONTENT_URI = Uri.parse(uriStr);
final Cursor c = cx.getContentResolver().query(CONTENT_URI, null,
"title=?", new String[] { title }, null);
if (c != null && c.getCount() > 0) {
result = true;
}
return result;
}
/**
*  Delete the desktop shortcut for the current application 
* 
* @param cx
*/
public static void delShortcut(Context cx) {
Intent shortcut = new Intent(
"com.android.launcher.action.UNINSTALL_SHORTCUT");
//  Gets the current application name 
String title = null;
try {
final PackageManager pm = cx.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(cx.getPackageName(),
PackageManager.GET_META_DATA)).toString();
Log.v("test", "title:" + title);
} catch (Exception e) {
}
//  Shortcut name 
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
Intent shortcutIntent = cx.getPackageManager()
.getLaunchIntentForPackage(cx.getPackageName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
cx.sendBroadcast(shortcut);
}
/**
*  Adds a desktop shortcut to the current application 
* 
* @param cx
* @param appName
*  Shortcut name 
*/
public static void addShortcut(Context cx) {
Intent shortcut = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
Intent shortcutIntent = cx.getPackageManager()
.getLaunchIntentForPackage(cx.getPackageName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
//  Gets the current application name 
String title = null;
try {
final PackageManager pm = cx.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(cx.getPackageName(),
PackageManager.GET_META_DATA)).toString();
Log.v("test", "title:" + title);
} catch (Exception e) {
}
//  Shortcut name 
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
//  Duplicate creation is not allowed (no 1 Effective) 
shortcut.putExtra("duplicate", false);
//  Shortcut icon 
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(cx, R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
cx.sendBroadcast(shortcut);
}

Hopefully, this article has been helpful in your Android programming.


Related articles: