android应用-创建快捷方式
我们开发一款软件后,如果手机装的软件过多,去翻的话会很难翻的,所以,在主页面有一个快捷方式的话会很不错的,下面是详细代码:
/** * 创建桌面快捷方式 */private void createShortcut() {SharedPreferences setting = getSharedPreferences("silent.preferences", 0);// 判断是否第一次启动应用程序(默认为true)boolean firstStart = setting.getBoolean("FIRST_START", true);// 第一次启动时创建桌面快捷方式if (firstStart) {Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");// 快捷方式的名称shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name2));// 不允许重复创建shortcut.putExtra("duplicate", false);// 指定快捷方式的启动对象ComponentName comp = new ComponentName(this.getPackageName(), "." + this.getLocalClassName());shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));// 快捷方式的图标ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.zhangxy);shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);// 发出广播sendBroadcast(shortcut);// 将第一次启动的标识设置为falseEditor editor = setting.edit();editor.putBoolean("FIRST_START", false);// 提交设置editor.commit();}}// 安装后第一次启动时创建桌面快捷方式createShortcut();
<!-- 创建桌面快捷方式的权限 --> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />