Android desktop shortcut icon generation and deletion, use intent to interact with Launcher

Source: Internet
Author: User

Original Author: dingran

By analyzing the process of generating shortcuts for launcher, you can find out the process of sending requests using intent. launcher uses the self-registered installshortcutreceiver and uninstallshortcutreceiver to generate and remove shortcuts. This article mainly analyzes how external APK uses intent requests to generate shortcuts and remove shortcuts.

 

Generate the shortcut code:

Private Static final string action_install_shortcut =
"Com. Android. launcher. Action. install_shortcut ";
 
/**
* Can there be multiple shortcut copies?
*/
Static final string extra_shortcut_duplicate = "DUPLICATE ";

Intent shortcutintent = new intent (action_install_shortcut );
Shortcutintent. putextra (intent. extra_shortcut_name,
Getstring (R. String. app_name ));
Shortcutintent. putextra (extra_shortcut_duplicate, false );
Intent intent2 = new intent (intent. action_main );
Intent2.addcategory (intent. category_launcher );

Intent2.setcomponent (New componentname (this. getpackagename (),
". Main "));

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

 

Note: intent intent2 = new intent (intent. action_main); this can also be replaced by the construction parameter or intent. action_create_shortcut cut can also generate a shortcut icon, but this is not standard. If it is not the same when it is deleted, it cannot be deleted. Therefore, intent. action_main is used.

 

The code for deleting shortcuts is:

Private Static final string action_uninstall_shortcut =
"Com. Android. launcher. Action. uninstall_shortcut ";

Intent intent = new intent (action_uninstall_shortcut );
Intent. putextra (intent. extra_shortcut_name, appname );
Componentname comp = new componentname (info. activityinfo. packagename,
Info. activityinfo. Name );
Intent. putextra (intent. extra_shortcut_intent, new intent ()
. Setcomponent (COMP). setaction ("android. Intent. Action. Main "));
Sendbroadcast (intent );

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.