This article illustrates the way Android adds (creates), deletes, and determines whether a desktop shortcut exists. Share to everyone for your reference. The implementation method is as follows:
/** * To determine if the desktop has been added shortcuts * * @param cx * @param titlename * Shortcut name * @return/public static Boolean Hasshortcut (context CX) {Boolean result = false;//get 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 (u
RISTR);
Final Cursor C = cx.getcontentresolver (). query (Content_uri, NULL, "Title=?", new string[] {title}, NULL);
if (c!= null && c.getcount () > 0) {result = true; /** * Delete the currently applied desktop shortcuts * * @param cx/public static void Delshortcut (context cx) {Intent shortcut = new Intent ("com.an Droid.launcher.action.UNINSTALL_SHORTCut ");
Gets the currently applied 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); /** * Add desktop shortcuts to current application * * @param cx * @param appName * Shortcut name/public static void Addshortcut (context cx) {Intent Shor
Tcut = new Intent ("Com.android.launcher.action.INSTALL_SHORTCUT");
Intent shortcutintent = Cx.getpackagemanager (). Getlaunchintentforpackage (Cx.getpackagename ());
Shortcut.putextra (Intent.extra_shortcut_intent, shortcutintent);
Gets the currently applied 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);
Do not allow duplicate creation (not necessarily valid) Shortcut.putextra ("duplicate", false);
The icon for the shortcut parcelable Iconresource = Intent.ShortcutIconResource.fromContext (cx, R.drawable.icon);
Shortcut.putextra (Intent.extra_shortcut_icon_resource, Iconresource);
Cx.sendbroadcast (shortcut);
}
I hope this article will help you with your Android program.