Android编程之创设桌面快捷方式
Android编程之创建桌面快捷方式
为了保持一致,这里的图标和名字都采用在应用程序中建立的名字,当然,你也可以修改成其他名字和图标。(估计没人会这么干的。。。-_-|||)
最后,注意增加权限:<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
在我们安装应用程序的时候,喜欢把常用的软件在桌面上有个快捷方式,那么我们可以在自己的程序中帮用户实现这一点,在用户第一次运行的时候,创建一个快捷方式在桌面上。这里,你也可以给用户个选择,是否需要创建快捷方式。
以下是创建快捷方式的代码:
/** * 创建桌面快捷方式 */ private void addShortcut() { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); String cls = new StringBuilder(String.valueOf(getPackageName())) .append(".").append(getLocalClassName()).toString(); ComponentName componentName = new ComponentName(getPackageName(), cls); intent.setComponent(componentName); Intent shortCut = new Intent(); shortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); // 快捷方式的名称 shortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); // 快捷方式的ICON Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource .fromContext(this, R.drawable.icon); shortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); // 不允许重复创建 shortCut.putExtra("duplicate", false); shortCut.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); sendBroadcast(shortCut); }
为了保持一致,这里的图标和名字都采用在应用程序中建立的名字,当然,你也可以修改成其他名字和图标。(估计没人会这么干的。。。-_-|||)
最后,注意增加权限:<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />