android Add shortcut (short) to mobile desktop
Permissions
To add a shortcut to your phone's desktop, you first need to add permissions to the manifest.
<!--Add Shortcut - <uses-permissionAndroid:name= "Com.android.launcher.permission.INSTALL_SHORTCUT" /> <!--Remove Shortcut - <uses-permissionAndroid:name= "Com.android.launcher.permission.UNINSTALL_SHORTCUT" /> <!--Query Shortcut - <uses-permissionAndroid:name= "Com.android.launcher.permission.READ_SETTINGS" />
Add Shortcut
Adding a shortcut is a broadcast to the desktop app (launcher) about the action, with the following action:
Public Static Final String action_add_shortcut = "Com.android.launcher.action.INSTALL_SHORTCUT";
To add a shortcut:
Private voidaddshortcut (String name) {Intent addshortcutintent=NewIntent (action_add_shortcut); //do not allow duplicate creationAddshortcutintent.putextra ("Duplicate",false);//The test is not repeated according to the name of the shortcut.//should be based on the intent of the fast chain to determine whether duplicate, that is, the value of the Intent.extra_shortcut_intent field//but the name is not the same, although some phone system will show a toast prompt repeat, will still establish a fast chain//prompt when there is no space on the screen//Note: Duplicate creation behavior MIUI and Samsung phones are not the same, millet on the seemingly can't repeat create shortcut//nameAddshortcutintent.putextra (Intent.extra_shortcut_name, NAME); //iconsAddshortcutintent.putextra (Intent.extra_shortcut_icon_resource, Intent.ShortcutIconResource.fro Mcontext (mainactivity. This, R.drawable.ic_launcher)); //set up affiliate programsIntent launcherintent =NewIntent (Intent.action_main); Launcherintent.setclass (mainactivity. This, Mainactivity.class); Launcherintent.addcategory (Intent.category_launcher); Addshortcutintent. PutExtra (Intent.extra_shortcut_intent, launcherintent); //Send broadcastSendbroadcast (addshortcutintent); }
Remove Shortcut
Action to remove shortcut:
Public Static Final String action_remove_shortcut = "Com.android.launcher.action.UNINSTALL_SHORTCUT";
Ways to remove Shortcuts:
Private voidremoveshortcut (String name) {//The Remove shortcut method does not work on the Xiaomi system and can be removed from SamsungIntent Intent =NewIntent (action_remove_shortcut); //nameIntent.putextra (Intent.extra_shortcut_name, NAME); //set up affiliate programsIntent launcherintent =NewIntent (mainactivity. This, mainactivity.class). Setaction (Intent.action_main); Intent.putextra (Intent.extra_shortcut_intent, launcherintent); //Send broadcastSendbroadcast (Intent); }
Test on two phones, found that the Xiaomi phone added a shortcut can not be removed, Samsung mobile phone.
Query Shortcut
The way to find out if a shortcut exists is from other sources on the Internet, but the test query failed, two phones (Xiaomi, Samsung) can not be found.
Keep the code to see what the reason is:
Private Booleanhasinstallshortcut (String name) {BooleanHasinstall =false; FinalString authority = "Com.android.launcher2.settings"; Uri Content_uri= Uri.parse ("content://" +Authority+ "/favorites?notify=true"); //here is always failed to find provider info//com.android.launcher2.settings and com.android.launcher.settings are not .cursor cursor = This. Getcontentresolver (). Query (Content_uri,NewString[] {"title", "Iconresource"}, "Title=?", NewString[] {name},NULL); if(Cursor! =NULL&& cursor.getcount () > 0) {Hasinstall=true; } returnHasinstall; }
References
Android Build Desktop shortcut (i)
Android Build desktop Shortcut (ii)
http://blog.csdn.net/ldj299/article/details/6298452
http://www.xmumu.com/post/2012-04-01/17357119
Http://www.cnblogs.com/CoolPigs/p/3317234.html