Compared with IOS development and Windows Phone development, Android is open, and Android development is more flexible and can do a lot of things. Some may find that after some Android applications are installed and run for the first time, a shortcut will be created on the desktop. How is this done?
Otherwise, Android is especially open. In Android development, this requirement can be achieved by sending a broadcast.
Here is a piece of encapsulated code.
1 public class ShortcutUtil {2 3 public static void createShortCut (Activity act, int iconResId, 4 int appnameResId) {5 6 // com. android. launcher. permission. INSTALL_SHORTCUT 7 8 Intent shortcutintent = new Intent (9 "com. android. launcher. action. INSTALL_SHORTCUT "); 10 // repeated creation of 11 shortcutintent is not allowed. putExtra ("duplicate", false); 12 // The actual name 13 shortcutintent is required. putExtra (Intent. EXTRA_SHORTCUT_NAME, 14 act. getString (appnameResId); 15 // shortcut image 16 Parcelable icon = Intent. using cuticonresource. fromContext (17 act. getApplicationContext (), iconResId); 18 shortcutintent. putExtra (Intent. EXTRA_SHORTCUT_ICON_RESOURCE, icon); 19 // click the shortcut image. the main entrance of the program to run is 20 shortcutintent. putExtra (Intent. EXTRA_SHORTCUT_INTENT, 21 new Intent (act. getApplicationContext (), act. getClass (); 22 // send broadcast 23 act. sendBroadcast (shortcutintent); 24} 25}
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.