Android Add (Create), delete, and determine the existence of desktop shortcuts _android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.