创建桌面快捷图标
/**
* 创建桌面快捷图标
* 桌面上有一个广播接受者可以接受创建快捷图标的广播和卸载快捷图标的广播
*/
*private void installShortCut() {*
//一般开发都需要将是否创建过快捷图标的标志存到SP中,以防止在桌面上创建很多图标
SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);;
boolean shortcut = sp.getBoolean("shortcut", false);
if(shortcut){//如果为true 代表已经安装了该应用,不需要在此安装快捷方式
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, "何伟手机卫士");// 指定桌面快捷图标的名称
// 指定桌面快捷图标,图标必须是Bitmap对象,因为id只是在本程序的范围内可用,
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);
//如果已经安装过,则将shortcut置为true,下次就不要再安装快捷方式
editor.putBoolean("shortcut", true);
editor.commit();
}
注意:在清单文件中需要加入权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
卸载快捷方式的权限
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
卸载快捷方式的action
"com.android.launcher.action.UNINSTALL_SHORTCUT"