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:
- You must have permission to create a shortcut;
- Create a shortcut for the broadcast of the Intent action setting Com.android.launcher.action.INSTALL_SHORTCUT
- Remove the Intent action setting for the broadcast of the shortcut Com.android.launcher.action.UNINSTALL_SHORTCUT
- 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