public void createDeskShortCut(){
Intent shortcutIntent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
shortcutIntent.putExtra("duplicate", false);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
Parcelable icon = Intent.ShortcutIconResource.fromContext(
getApplicationContext(), R.drawable.icon);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
sendBroadcast(shortcutIntent);
getApplicationContext().getSharedPreferences("shortcut", Context.MODE_PRIVATE)
.edit().putBoolean("iscreated", true).commit();
}
然后在oncreate中调用:
Context appContext = getApplicationContext();
SharedPreferences shortcutpref = appContext.getSharedPreferences(
"shortcut", Context.MODE_PRIVATE);
boolean iscreated = shortcutpref.getBoolean("iscreated", false);
if (!iscreated) createDeskShortCut();
<uses-permissionandroid:name="com.android.launcher.permission.INSTALL_SHORTCUT">