Android and android Official Website
Add
The add shortcut is to send a broadcast of related actions to the desktop application (launcher:
public static final String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
Code:
Private void addShortcut (String name) {Intent addShortcutIntent = new Intent (ACTION_ADD_SHORTCUT); // duplicate addShortcutIntent creation is not allowed. putExtra ("duplicate", false); // The results are not repeated based on the shortcut name.
// Name addShortcutIntent. putExtra (Intent. EXTRA_SHORTCUT_NAME, name); // icon addShortcutIntent. putExtra (Intent. EXTRA_SHORTCUT_ICON_RESOURCE, Intent. using cuticonresource. fromContext (MainActivity. this, R. drawable. ic_launcher); // sets the associated program Intent launcherIntent = new Intent (Intent. ACTION_MAIN); launcherIntent. setClass (MainActivity. this, MainActivity. class); launcherIntent. addCategory (Intent. CATEGORY_LAUNCHER); addShortcutIntent. putExtra (Intent. EXTRA_SHORTCUT_INTENT, launcherIntent); // send broadcast sendBroadcast (addShortcutIntent );}
Remove
Action:
public static final String ACTION_REMOVE_SHORTCUT = "com.android.launcher.action.UNINSTALL_SHORTCUT";
Code:
Private void removeShortcut (String name) {Intent intent = new Intent (ACTION_REMOVE_SHORTCUT); // name intent. putExtra (Intent. EXTRA_SHORTCUT_NAME, name); // sets the associated program Intent launcherIntent = new Intent (MainActivity. this, MainActivity. class ). setAction (Intent. ACTION_MAIN); intent. putExtra (Intent. EXTRA_SHORTCUT_INTENT, launcherIntent); // send broadcast sendBroadcast (intent );}
Permission
<! -- Add shortcuts --> <uses-permission android: name = "com. android. launcher. permission. INSTALL_SHORTCUT"/> <! -- Remove shortcuts --> <uses-permission android: name = "com. android. launcher. permission. UNINSTALL_SHORTCUT"/>
I am the dividing line of tiantiao