Permissions Required:
<android:name= "Com.android.launcher.permission.READ_SETTINGS"/> < android:name= "Com.android.launcher.permission.INSTALL_SHORTCUT"/>
Call Method:
Shortcut.create (this);
The shortcut code is as follows:
Public classShortCut {/*** If there is a shortcut *@paramContext *@return */ Private Static BooleanIsaddshortcut (Context context) {BooleanIsinstallshortcut =false; FinalContentresolver CR =Context.getcontentresolver (); intVersionlevel =Android.os.Build.VERSION.SDK_INT; String Authority= "Com.android.launcher2.settings"; //more than 2.2 of the system's file names are not the same if(Versionlevel >= 8) {Authority= "Com.android.launcher2.settings"; } Else{Authority= "Com.android.launcher.settings"; } FinalUri Content_uri = Uri.parse ("content://" +Authority+ "/favorites?notify=true"); Cursor C=cr.query (Content_uri,NewString[] {"title", "Iconresource"}, "Title=?", NewString[] {context.getstring (r.string.app_name)},NULL); if(c! =NULL&& c.getcount () > 0) {Isinstallshortcut=true; } returnIsinstallshortcut; } /*** Add Shortcut *@paramContext*/ Private Static voidAddshortcut (Context context) {Intent shortcut=NewIntent ("Com.android.launcher.action.INSTALL_SHORTCUT"); //Setting PropertiesShortcut.putextra (Intent.extra_shortcut_name, Context.getresources (). getString (R.string.app_na Me)); Shortcuticonresource Iconres=Intent.ShortcutIconResource.fromContext (Context.getapplicationcontext (), r.drawable.ic_launcher); Shortcut.putextra (Intent.extra_shortcut_icon, iconres); //whether to allow duplicate creationShortcut.putextra ("Duplicate",false); //set icons for desktop Shortcutsparcelable icon =Intent.ShortcutIconResource.fromContext (context, r.drawable.ic_launcher); Shortcut.putextra (Intent.extra_shortcut_icon_resource, ICON); //Click the action of the shortcutIntent Intent =NewIntent (Intent.action_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 (context, applistactivity.class); //Setting up the startup programShortcut.putextra (intent.extra_shortcut_intent, Intent); //broadcast Notification desktop to createcontext.sendbroadcast (shortcut); } /*** External Call to create Shortcut *@paramContext*/ Public Static voidCreate (Context context) {if(Context! =NULL) { if(!isaddshortcut (context)) {addshortcut (context); } } }
android--Creating a shortcut