[Android] a desktop shortcut is automatically created after the Android app 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.

The following is a encapsulated section.Code.

Public class shortcututil {public static void createshortcut (Activity Act, int iconresid, int appnameresid) {// COM. android. launcher. permission. install_shortcutintent shortcutintent = new intent ("com. android. launcher. action. install_shortcut "); // duplicate shortcutintent cannot be created. putextra ("DUPLICATE", false); // the actual name shortcutintent is required. putextra (intent. extra_shortcut_name, act. getstring (appnameresid); // saved image parcelable icon = intent. using cuticonresource. fromcontext (Act. getapplicationcontext (), iconresid); shortcutintent. putextra (intent. extra_shortcut_icon_resource, icon); // click the shortcut image to runProgramMain Entry shortcutintent. putextra (intent. 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"/>

<Uses-Permission   Android: Name = "Com. Android. launcher. Permission. read_settings"   />

 

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.

 

---------------------------------------------------------------------------

GL (arui319)

Http://blog.csdn.net/arui319

<This article can be reproduced, but please keep the above author information. Thank you.>

---------------------------------------------------------------------------

 

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.