1: Create a shortcut
Permissions Required: <uses-permission android:name= "com.android.launcher.permission.INSTALL_SHORTCUT"/>
Private Static voidCreateShortcut (Context cxt, String shortcutname,intshortcuticonres, String className,BooleanDuplicateBooleanlauchercategory) {Intent Intent=getshortcutintent (CXT, Cxt.getpackagename (), ClassName, Shortcutname, lauchercategory); intIconsize =cxt.getresources (). Getdimensionpixelsize (Res.dimen.app_icon_size); bitmapdrawable icon=(bitmapdrawable) cxt.getresources (). getdrawable (Shortcuticonres); Bitmap BMP= Imageutils.scaleto (Icon.getbitmap (), Iconsize, Iconsize,false); Intent.putextra (Intent.extra_shortcut_icon, BMP); Intent.putextra (Extra_shortcut_duplicate, DUPLICATE); //Now , notify the Launcher to create the shortcutCxt.sendbroadcast (Intent); }
Private StaticIntent getshortcutintent (Context cxt, String pkgname, String className, String shortcutname,Booleanlauchercategory) { //Prepare The intents for shortcutIntent shortcutintent =NewIntent (Intent.action_view); Shortcutintent.setclassname (Pkgname, className); Shortcutintent.setflags (Intent.flag_activity_new_task); Shortcutintent.putextra (Constants.extra_from_key, constants.extra_from_value_shortcut); if(lauchercategory) {shortcutintent.addcategory (intent.category_launcher); Shortcutintent.setaction (Intent.action_main); } Intent Intent=NewIntent (action_install_shortcut); Intent.putextra (Intent.extra_shortcut_intent, shortcutintent); Intent.putextra (Intent.extra_shortcut_name, shortcutname); returnIntent; }
2: Delete shortcut (MIUI system not supported):
Permissions Required: <uses-permission android:name= "com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
Public Static voidremoveshortcut (Context cxt, String shortcutname, String className,BooleanRemoveAll) { //Prepare The intents for shortcutIntent shortcutintent =NewIntent (Intent.action_view); Shortcutintent.setclassname (CXT, className); Intent Intent=NewIntent (action_uninstall_shortcut); Intent.putextra (Intent.extra_shortcut_intent, shortcutintent); Intent.putextra (Intent.extra_shortcut_name, shortcutname); Intent.putextra (Extra_shortcut_duplicate, RemoveAll); //Now , notify the Launcher to remove the shortcutCxt.sendbroadcast (Intent); }
3: Query shortcut exists (three-party ROM most queries fail with cursor NULL)
Permissions Required: <uses-permission android:name= " Com.android.launcher.permission.READ_SETTINGS "/>
or <uses-permission android:name= " Com.android.launcher.permission.WRITE_SETTINGS " ./>
Private Booleanhasshortcut () {BooleanIsinstallshortcut =false; FinalContentresolver CR =Activity.getcontentresolver (); FinalString authority = "Com.android.launcher.settings"; FinalUri Content_uri = Uri.parse ("content://" +authority + "/favorites?notify=true"); Cursor C= Cr.query (Content_uri,NewString[] {"title", "Iconresource"}, "Title=?", NewString[] {getString (r.string.app_name). Trim ()},NULL); if(c!=NULL&& C.getcount () >0){
String title = c.getString(c.getColumnIndexOrThrow( "title" )); isinstallshortcut=true ; } returnIsinstallshortcut; }
[Android Pro] Create a shortcut, delete a shortcut, query whether a shortcut exists