Add such a method to the activity:
/**
* Create Shortcut
*/
public void Createdeskshortcut () {
LOG.I ("coder", "------createshortcut--------");
Intent to create a shortcut
Intent shortcutintent = new Intent (
"Com.android.launcher.action.INSTALL_SHORTCUT");
Do not allow duplicate creation
Shortcutintent.putextra ("duplicate", false);
Need a real name
Shortcutintent.putextra (Intent.extra_shortcut_name,
GetString (R.string.app_name));
Quick picture
parcelable icon = Intent.ShortcutIconResource.fromContext (
Getapplicationcontext (), r.drawable.ic_launcher);
Shortcutintent.putextra (Intent.extra_shortcut_icon_resource, ICON);
Intent Intent = new Intent (Getapplicationcontext (),
Androidlayoutactivity.class);
Click on the shortcut picture, run the program main entrance
Shortcutintent.putextra (Intent.extra_shortcut_intent, Intent);
Send a broadcast. Ok
Sendbroadcast (shortcutintent);
}
Give the full code:
Package com.jiahui.layout;
Import android.app.Activity;
Import Android.content.Context;
Import android.content.Intent;
Import android.content.SharedPreferences;
Import Android.os.Bundle;
Import android.os.Parcelable;
Import Android.util.Log;
public class Androidlayoutactivity Extendsactivity {
/**called when the activity is first created. */
@Override
Publicvoid onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.view_personal_info);
Sharedpreferencespreferences = Getsharedpreferences ("First",
Context.mode_private);
Booleanisfirst = Preferences.getboolean ("Isfrist", true);
if (IsFirst) {
Createdeskshortcut ();
}
Sharedpreferences.editoreditor = Preferences.edit ();
Editor.putboolean ("Isfrist", false);
Editor.commit ();
}
/**
* Create Shortcut
*/
Publicvoid Createdeskshortcut () {
LOG.I ("coder", "------createshortcut--------");
Intent to create a shortcut
Intentshortcutintent = new Intent (
"Com.android.launcher.action.INSTALL_SHORTCUT");
Do not allow duplicate creation
Shortcutintent.putextra ("duplicate", false);
Need a real name
Shortcutintent.putextra (Intent.extra_shortcut_name,
GetString (R.string.app_name));
Quick picture
Parcelableicon = Intent.ShortcutIconResource.fromContext (
Getapplicationcontext (), r.drawable.ic_launcher);
Shortcutintent.putextra (Intent.extra_shortcut_icon_resource,icon);
Intentintent = new Intent (Getapplicationcontext (),
Androidlayoutactivity.class);
The following two properties are intended to remove a shortcut on the desktop when the application is uninstalled
Intent.setaction ("Android.intent.action.MAIN");
Intent.addcategory ("Android.intent.category.LAUNCHER");
Click on the shortcut picture, run the program main entrance
Shortcutintent.putextra (intent.extra_shortcut_intent,intent);
Send a broadcast. Ok
Sendbroadcast (shortcutintent);
}
}