How to automatically create a desktop shortcut after the Android app is started

Source: Internet
Author: User

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.

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.