The Android app automatically creates a desktop shortcut after it is started.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.