private void installShortCut() {
boolean shortcut = sp.getBoolean("shortcut", false);
if(shortcut)
return;
Editor editor = sp.edit();
Intent intent = new Intent();
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
//快捷方式 1.名称 2.图标 3.意图
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ApplicationName");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
Intent shortcutIntent = new Intent();
shortcutIntent.setAction("android.intent.action.MAIN");
shortcutIntent.addCategory("android.intent.category.LAUNCHER");
shortcutIntent.setClassName(getPackageName(), "package.MainActivity");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent);
sendBroadcast(intent);
editor.putBoolean("shortcut", true);
editor.commit();
}
记得加权限:
<uses-permission android:name="com.android.Launcher.permission.INSTALL_SHORTCUT"/>