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

来源:爱站网时间:2019-01-11编辑:网友分享
Android快捷方式是桌面最基本的组件,它可以用于直接启动应用程序的组件,下面是爱站技术频道和大家分享android应用程序创建桌面快捷方式技巧,一起进入下文了解一下吧!

Android快捷方式是桌面最基本的组件,它可以用于直接启动应用程序的组件,下面是爱站技术频道和大家分享android应用程序创建桌面快捷方式技巧,一起进入下文了解一下吧!

分享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();

 

上文是分享android应用程序创建桌面快捷方式技巧,相信大家都有了一定的了解,想要了解更多的技术信息,请继续关注爱站技术频道吧!

上一篇:android 分辨率适配的方法

下一篇:详解android dialog自定义实例

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载