Some users reported that after creating a shortcut, they cannot start it after clicking the shortcut.ProgramOr prompt "not installed", it seems that the New rom has been modified in the shortcut section (because this article was issued in May, it is estimated that it should be 2.0 or 2.1 ROM ), corrected. HTC G11 2.3.5rom passed the test.
1. Determine whether shortcuts have been created (in some models, you need to determine)
Private Boolean hasshortcut () {Boolean isinstallshortcut = false; Final contentresolver Cr = activity. getcontentresolver (); final string authority = "com. android. launcher. settings "; Final URI content_uri = Uri. parse ("content: //" + authority + "/favorites? Required y = true "); cursor c = Cr. Query (content_uri, new string [] {" title "," iconresource "}," Title =? ", New string [] {mapviewactivity. getstring (R. String. app_name). Trim ()}, null); If (C! = NULL & C. getcount ()> 0) {isinstallshortcut = true;} return isinstallshortcut ;}
2. Create
/*** Create a desktop shortcut for the Program */private void addshortcut () {intent shortcut = new intent ("com. android. launcher. action. install_shortcut cut "); // shortcut name: shortcut cut. putextra (intent. extra_shortcut_name, getstring (R. string. app_name); shortcut cut. putextra ("DUPLICATE", false ); // duplicate creation is not allowed * * ********************* // componentname comp = new componentname (this. getpackagename (),". "+ this. getlocalclassname (); // shortcut cut. putextra (intent. extra_shortcut_intent, new intent (intent. action_main ). setcomponent (COMP )); /**************************** end ******** * *********************/intent shortcutintent = new intent (intent. action_main); shortcutintent. setclassname (this, this. getclass (). getname (); shortcut cut. putextra (intent. extra_shortcut_intent, shortcutintent); // shortcut icon export cuticonresource iconres = intent. using cuticonresource. fromcontext (this, R. drawable. icon); shortcut cut. putextra (intent. extra_shortcut_icon_resource, iconres); sendbroadcast (shortcut );}
3. Delete
/*** Shortcut for deleting a program */private void delshortcut () {intent shortcut = new intent ("com. android. launcher. action. uninstall_shortcut cut "); // shortcut name: shortcut cut. putextra (intent. extra_shortcut_name, getstring (R. string. app_name); string appclass = This. getpackagename () + ". "+ this. getlocalclassname (); componentname comp = new componentname (this. getpackagename (), appclass); shortcut. putextra (intent. extra_shortcut_intent, new intent (intent. action_main ). setcomponent (COMP); sendbroadcast (shortcut );}
3. Declare Permissions
Declare permissions when creating and deleting shortcuts in the androidmanifest. xml file