Android Add, remove, and judge desktop shortcut icons

Source: Internet
Author: User

Ideas:

Launcher in order for the application to customize its own shortcut icons, a broadcastreceiver is registered to receive customized information for the shortcut icons sent by other applications. So we just need to construct the corresponding intent based on the broadcastreceiver and load our custom information, and finally call the Sendbroadcast method to create a shortcut icon.

Steps:

    1. You must have permission to create a shortcut;
    2. Create a shortcut for the broadcast of the Intent action setting Com.android.launcher.action.INSTALL_SHORTCUT
    3. Remove the Intent action setting for the broadcast of the shortcut Com.android.launcher.action.UNINSTALL_SHORTCUT
    4. Set the picture and name of the shortcut in the Intent;

The permissions you need to add are as follows:

<uses-permissionAndroid:name= "Com.android.launcher.permission.INSTALL_SHORTCUT"/>    <uses-permissionAndroid:name= "Com.android.launcher.permission.UNINSTALL_SHORTCUT"/>    <uses-permissionAndroid:name= "Com.android.launcher.permission.READ_SETTINGS"/>    <uses-permissionAndroid:name= "Com.android.launcher2.permission.READ_SETTINGS"/>    <uses-permissionAndroid:name= "Com.android.launcher3.permission.READ_SETTINGS"/>

The core code is:

1     /**2 * Add desktop shortcuts for the current app3      *4      * @paramContext5      */6      Public Static voidAddshortcut (Context context,intAppIcon) {7Intent shortcut =NewIntent (8"Com.android.launcher.action.INSTALL_SHORTCUT");9 TenIntent shortcutintent =Context.getpackagemanager () One . Getlaunchintentforpackage (Context.getpackagename ()); A Shortcut.putextra (intent.extra_shortcut_intent, shortcutintent); -         //get the current app name -String title =NULL; the         Try { -             FinalPackagemanager pm =Context.getpackagemanager (); -title =Pm.getapplicationlabel ( - Pm.getapplicationinfo (Context.getpackagename (), + packagemanager.get_meta_data)). ToString (); -}Catch(Exception e) { +         } A         //Shortcut Name at Shortcut.putextra (Intent.extra_shortcut_name, title); -         //duplicate creation is not allowed (not necessarily valid) -Shortcut.putextra ("Duplicate",false); -         //icon for shortcut -Parcelable Iconresource =Intent.ShortcutIconResource.fromContext (Context, - AppIcon); in Shortcut.putextra (Intent.extra_shortcut_icon_resource, iconresource); -  to context.sendbroadcast (shortcut); +     } -  the     /** * * Remove desktop shortcuts for the current app $      *Panax Notoginseng      * @paramContext -      */ the      Public Static voidDelshortcut (Context context) { +Intent shortcut =NewIntent ( A"Com.android.launcher.action.UNINSTALL_SHORTCUT"); the  +         //get the current app name -String title =NULL; $         Try { $             FinalPackagemanager pm =Context.getpackagemanager (); -title =Pm.getapplicationlabel ( - Pm.getapplicationinfo (Context.getpackagename (), the packagemanager.get_meta_data)). ToString (); -}Catch(Exception e) {Wuyi         } the         //Shortcut Name - Shortcut.putextra (Intent.extra_shortcut_name, title); WuIntent shortcutintent =Context.getpackagemanager () - . Getlaunchintentforpackage (Context.getpackagename ()); About Shortcut.putextra (intent.extra_shortcut_intent, shortcutintent); $ context.sendbroadcast (shortcut); -     } -  -     /** A * Determine if there is a desktop shortcut for the current application on the desktop +      * the      * @paramContext -      */ $      Public Static BooleanHasshortcut (Context context) { the         Booleanresult =false; theString title =NULL; the         Try { the             FinalPackagemanager pm =Context.getpackagemanager (); -title =Pm.getapplicationlabel ( in Pm.getapplicationinfo (Context.getpackagename (), the packagemanager.get_meta_data)). ToString (); the}Catch(Exception e) { About  the         } the  the         FinalString uristr; +         if(Android.os.Build.VERSION.SDK_INT < 8) { -Uristr = "Content://com.android.launcher.settings/favorites?notify=true"; the}Else if(Android.os.Build.VERSION.SDK_INT < 19) {BayiUristr = "Content://com.android.launcher2.settings/favorites?notify=true"; the}Else { theUristr = "Content://com.android.launcher3.settings/favorites?notify=true"; -         } -         FinalUri Content_uri =Uri.parse (URISTR); the         FinalCursor C = context.getcontentresolver (). Query (Content_uri,NULL, the"Title=?",NewString[]{title},NULL); the         if(c! =NULL&& c.getcount () > 0) { theresult =true; -         } the         returnresult; the}

Android Add, remove, and judge desktop shortcut icons

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.