Permissions:
<uses-permission android:name= "Com.android.launcher.permission.INSTALL_SHORTCUT"/>
Packagecom.example.t02;Importandroid.app.Activity;Importandroid.app.Fragment;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.ViewGroup;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); if(Savedinstancestate = =NULL) {Getfragmentmanager (). BeginTransaction (). Add (R.id.container,Newplaceholderfragment ()). commit (); } } /*** A placeholder fragment containing a simple view. */ Public Static classPlaceholderfragmentextendsFragment {Button btnchange; Public Static FinalString action_add_shortcut = "Com.android.launcher.action.INSTALL_SHORTCUT"; Publicplaceholderfragment () {} @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { View Rootview=inflater.inflate (R.layout.fragment_main, container,false); Btnchange=(Button) Rootview.findviewbyid (R.id.btnchange); Btnchange.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {addshortcut ("Tttt"); } }); returnRootview; } Private voidaddshortcut (String name) {Intent addshortcutintent=NewIntent (action_add_shortcut); //do not allow duplicate creationAddshortcutintent.putextra ("Duplicate",false);// //nameAddshortcutintent.putextra (Intent.extra_shortcut_name, NAME); //iconsAddshortcutintent.putextra (Intent.extra_shortcut_icon_resource, Intent.shortcuticonreso Urce.fromcontext (Getactivity (), r.drawable.ic_launcher2)); //setting up associated activityIntent launcherintent =NewIntent (Intent.action_main); Launcherintent.setclass (Getactivity (), mainactivity.class); Launcherintent.addcategory (Intent.category_launcher); Addshortcutintent. PutExtra (Intent.extra_shortcut_intent, launcherintent); //Send broadcastgetactivity (). Sendbroadcast (addshortcutintent); } }}
Create a shortcut icon for your app