栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

android 为应用程序创建桌面快捷方式技巧分享

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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);
// 将第一次启动的标识设置为false
Editor editor = setting.edit();
editor.putBoolean("FIRST_START", false);
// 提交设置
editor.commit();
}
}

然后在onCreate()方法里加上上面方法名称就行了:
复制代码 代码如下:
// 安装后第一次启动时创建桌面快捷方式
createShortcut();

最后在AndroidManifest.xml里加上创建快捷方式的权限就行了:
复制代码 代码如下:


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/165720.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号