我们安装一个应用程序后,应用程序会在手机的主页面上生成应用图标,这样用户如果要使用的话需要到主页面去找这个应用程序的图标,这样操作起来不太方便。所以个人感觉最好是在手机的桌面上创建一个应用程序的快捷方式,就像PC上的windows桌面快捷方式一样。这个在网上已经有实现的比较好的代码了,本文参考转载自:http://www.jb51.net/article/32457.htm
具体实现如下:
/**
* 创建桌面快捷方式
*/
static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
private String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
public void createShortCut(){
SharedPreferences setting = getSharedPreferences("chentequan.preferences", 0);
// 判断是否第一次启动应用程序(默认为true)
boolean firstStart = setting.getBoolean("FIRST_START", true);
if (!firstStart) {
return;
}
Intent shortcutIntent = new Intent(ACTION_INSTALL_SHORTCUT);
/* 设置快捷方式名称 */
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
/* 设置不允许重复创建 */
shortcutIntent.putExtra(EXTRA_SHORTCUT_DUPLICATE, false);
/* 需要使用ACTION_MAIN这个在卸载应用程序时,才会同步连桌面快捷方式也一起删除 */
Intent intent2 = new Intent(Intent.ACTION_MAIN);
intent2.addCategory(Intent.CATEGORY_LAUNCHER);
/* 指定应用程序的首个启动页面*/
intent2.setComponent(new ComponentName(this.getPackageName(), ".loginactivity"));
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this,
R.drawable.sslvpnicon12_l));
sendBroadcast(shortcutIntent);
Editor editor = setting.edit();
editor.putBoolean("FIRST_START", false);
// 提交设置
editor.commit();
}
注意:这边要创建桌面快捷方式,需要申请用户权限,在androidMainfest.xml上添加如下代码即可:
<!-- 创建桌面快捷方式的权限 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />