首先在创建快捷方式在在oncreat方法中执行
installShortCut();
以及加入权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
private SharedPreferences sp;//以防创立多个
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");
//快捷方式 要包含3个重要的信息 1,名称 2.图标 3.干什么事情
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "手机小卫士");
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(), "com.itheima.mobilesafe.SplashActivity");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
sendBroadcast(intent);
editor.putBoolean("shortcut", true);
editor.commit();
}