在欢迎界面延迟几秒后调用
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_name));
// 不允许重复创建
shortcut.putExtra("duplicate", false);
// 指定快捷方式的启动对象
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent(this, this.getClass()).setAction(Intent.ACTION_MAIN));
// 快捷方式的图标
Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
// 发出广播
sendBroadcast(shortcut);
// 将第一次启动的标识设置为false
SharedPreferences.Editor editor = setting.edit();
editor.putBoolean("FIRST_START", false);
// 提交设置
editor.commit();
}
本博客介绍如何在应用程序首次启动时自动创建桌面快捷方式,并通过SharedPreferences管理该过程,确保唯一性和易于操作。
1571

被折叠的 条评论
为什么被折叠?



