Implementation principle: The use of intent, the code is as follows:
1 //Create a shortcut2 Private voidCreateShortcut () {3Intent shortcut =NewIntent ("Com.android.launcher.action.INSTALL_SHORTCUT"); 4 Shortcut.putextra (Intent.extra_shortcut_name, getString (r.string.app_name)); 5Intent Intent =NewIntent (Intent.action_main); 6 intent.addcategory (Intent.category_launcher); 7Intent.setclass ( This, Mainactivity.class);//set the first page8 Shortcut.putextra (intent.extra_shortcut_intent, Intent); 9Shortcuticonresource iconres = Intent.ShortcutIconResource.fromContext ( This, R.drawable.ic_launcher); Ten Shortcut.putextra (Intent.extra_shortcut_icon_resource, iconres); One sendbroadcast (shortcut); AToast.maketext (Getapplicationcontext (), "created successfully", Toast.length_short). Show (); - } - //To interpret if a shortcut already exists the Public Booleanisexistshortcut () { - BooleanIsinstallshortcut =false; - FinalContentresolver cr = Mainactivity. This. Getcontentresolver (); - FinalString authority = "Com.android.launcher2.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)},NULL); + if(c! =NULL&& c.getcount () > 0) { AIsinstallshortcut =true; at } - returnIsinstallshortcut; -}
And then use the time:
1 if (Isexistshortcut ()) {2 Toast.maketext (Getapplicationcontext (), "Shortcut already exists", Toast.length_short). Show (); 3 }else{4 createshortcut (); 5 }
Remember to add permissions in the manifest file:
1 <uses-permission android:name= "Com.android.launcher.permission.INSTALL_SHORTCUT"/>2 < Uses-permission android:name= "Com.android.launcher.permission.READ_SETTINGS"/>3 < Uses-permission android:name= "Com.android.launcher.permission.WRITE_SETTINGS"/>
Android app Add shortcut