Android apps automatically add desktop icons

Source: Internet
Author: User

Recently, I have seen that other apps have the function of automatically adding desktop icons, and I want my apps to have such a function. below is the method I used during my exercises.CodeFor your reference.

Every interactive application has a launcher class in the project list file. In addition to prompting that the system activity is an entry function, a shortcut icon of the application is added to the Application List. This article describes how launcher generates and removes shortcuts through the self-registered installshortcutreceiver and uninstallshortcutreceiver, and analyzes the problem of generating shortcuts and removing shortcuts for external APK intent requests.

 

Add icon:

Intent intent = new intent ("com. Android. launcher. Action. install_shortcut ");
Intent. putextra (intent. extra_shortcut_name, getstring (R. String. app_name ));
// Whether there can be copies of multiple shortcuts. If the parameter is true, multiple shortcuts can be generated. If the parameter is false, duplicate shortcuts will not be added.
Intent. putextra ("DUPLICATE", false );

Intent intent2 = new intent (intent. action_main );
Intent2.addcategory (intent. category_launcher );
// Delete the applicationProgramIs the application package name + activity name.
Intent2.setcomponent (New componentname (this. getpackagename (), this. getpackagename () + ". Main "));

Intent. putextra (intent. extra_shortcut_intent, intent2 );
Intent. putextra (intent. extra_shortcut_icon_resource, intent. Reset cuticonresource. fromcontext (this,
R. drawable. Icon ));
Sendbroadcast (intent );

Permissions to be added:

<Uses-Permission Android: Name = "com. Android. launcher. Permission. install_shortcut"/>

 

Delete icon:

Intent intent = new intent ("com. Android. launcher. Action. uninstall_shortcut ");
Intent. putextra (intent. extra_shortcut_name, appname );
// Componentname of the application to be deleted, that is, application package name + activity name
Intent. putextra (intent. extra_shortcut_intent, new intent ()
. Setcomponent (New componentname (info. activityinfo. packagename,
Info. activityinfo. Name). setaction ("android. Intent. Action. Main "));
Sendbroadcast (intent );

Add the delete permission: <uses-Permission Android: Name = "com. Android. launcher. Permission. uninstall_shortcut"/>

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.