Required Permissions:
<android:name= "Com.android.launcher.permission.INSTALL_SHORTCUT"/> <android:name= "Com.android.launcher.permission.UNINSTALL_SHORTCUT" /> < android:name= "Com.android.launcher.permission.READ_SETTINGS"/>
/*** Shortcut Tool class*/ Public classShortcututils {/*** Add desktop shortcuts for current app *@paramCX*/ Public Static voidaddshortcut (Context cx) {Intent shortcut=NewIntent ("Com.android.launcher.action.INSTALL_SHORTCUT"); Intent shortcutintent=Cx.getpackagemanager (). Getlaunchintentforpackage (Cx.getpackagename ()); Shortcut.putextra (Intent.extra_shortcut_intent, shortcutintent); //get the current app nameString title =NULL; Try { FinalPackagemanager pm =Cx.getpackagemanager (); Title=Pm.getapplicationlabel (Pm.getapplicationinfo (Cx.getpackagename (), Pack Agemanager.get_meta_data). toString (); } Catch(Exception e) {}//Shortcut NameShortcut.putextra (Intent.extra_shortcut_name, title); //duplicate creation is not allowed (not necessarily valid)Shortcut.putextra ("Duplicate",false); //icon for shortcutParcelable Iconresource =Intent.ShortcutIconResource.fromContext (CX, R.drawable.ic_launcher); Shortcut.putextra (Intent.extra_shortcut_icon_resource, Iconresource); Cx.sendbroadcast (shortcut); } /*** Delete the desktop shortcut for the current app *@paramCX*/ Public Static voiddelshortcut (Context cx) {Intent shortcut=NewIntent ("Com.android.launcher.action.UNINSTALL_SHORTCUT"); //get the current app nameString title =NULL; Try { FinalPackagemanager pm =Cx.getpackagemanager (); Title=Pm.getapplicationlabel (Pm.getapplicationinfo (Cx.getpackagename (), Pack Agemanager.get_meta_data). toString (); } Catch(Exception e) {}//Shortcut NameShortcut.putextra (Intent.extra_shortcut_name, title); Intent shortcutintent=Cx.getpackagemanager (). Getlaunchintentforpackage (Cx.getpackagename ()); Shortcut.putextra (Intent.extra_shortcut_intent, shortcutintent); Cx.sendbroadcast (shortcut); } /*** Determine if the current app has desktop shortcuts on the desktop *@paramCX*/ Public Static Booleanhasshortcut (Context cx) {Booleanresult =false; String title=NULL; Try { FinalPackagemanager pm =Cx.getpackagemanager (); Title=Pm.getapplicationlabel (Pm.getapplicationinfo (Cx.getpackagename (), Pack Agemanager.get_meta_data). toString (); } Catch(Exception e) {}FinalString 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"; } FinalUri Content_uri =Uri.parse (URISTR); FinalCursor C = cx.getcontentresolver (). Query (Content_uri,NULL, "Title=?",NewString[] {title},NULL); if(c! =NULL&& c.getcount () > 0) {result=true; } returnresult; }}
Shortcut tool Classes