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 );