define two functions first:
Public void addshortcut () {intent addintent = new intent ("com. android. launcher. action. install_shortcut "); string mytitle = getresources (). getstring (R. string. app_name); parcelable icon = intent. using cuticonresource. fromcontext (this, R. drawable. APP); // set intent after clicking the shortcut operation // intent myintent = new intent (this, start. class); // whether to allow repeated addintent Creation. putextra ("DUPLICATE", false); intent = new intent (intent. acti On_main); intent. setflags (intent. flag_activity_reset_task_if_needed); intent. addflags (intent. flag_activity_launched_from_history); intent. addcategory (intent. category_launcher); intent. setclass (this, start. class); addintent. putextra (intent. extra_shortcut_name, mytitle); addintent. putextra (intent. extra_shortcut_icon, icon); addintent. putextra (intent. extra_shortcut_intent, intent); sendbroadcast (addinte NT);} public Boolean ifaddshortcut () {Boolean isinstallshortcut = false; Final contentresolver Cr = appstoreactivity. this. getcontentresolver (); final string authority = "com. android. launcher2.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 [] {getstring (R. String. app_name)}, null); If (C! = NULL & C. getcount ()> 0) {isinstallshortcut = true;} return isinstallshortcut ;}
ThenProgramAt startup:
// Create a shortcut Boolean flag = ifaddshortcut (); // if you have already created a shortcut, you do not need to create if (flag = false) {addshortcut ();}