Otherwise, Android is especially open. In Android development, this requirement can be achieved by sending a broadcast.
Here is a piece of encapsulated code.
Copy codeThe Code is as follows: public class ShortcutUtil {
Public static void createShortCut (Activity act, int iconResId,
Int appnameResId ){
// Com. android. launcher. permission. INSTALL_SHORTCUT
Intent shortcutintent = new Intent (
"Com. android. launcher. action. INSTALL_SHORTCUT ");
// Repeated creation is not allowed
Shortcutintent. putExtra ("duplicate", false );
// The actual name is required
Shortcutintent. putExtra (Intent. EXTRA_SHORTCUT_NAME,
Act. getString (appnameResId ));
// Quick image
Parcelable icon = Intent. Reset cuticonresource. fromContext (
Act. getApplicationContext (), iconResId );
Shortcutintent. putExtra (Intent. EXTRA_SHORTCUT_ICON_RESOURCE, icon );
// Click the shortcut image to run the main program entry
Shortcutintent. putExtra (Intent. EXTRA_SHORTCUT_INTENT,
New Intent (act. getApplicationContext (), act. getClass ()));
// Send Broadcast
Act. sendBroadcast (shortcutintent );
}
}
The code is relatively simple and will not be explained in detail.
Do not forget to add the following permissions; otherwise, no effect will be displayed.
<Uses-permission android: name = "com. android. launcher. permission. INSTALL_SHORTCUT"/>
In addition, this may be unfriendly. A better way is to prompt the user whether to create a desktop shortcut when running the program for the first time, so that the user can choose. The prompt is no longer displayed after the task is run again.