android直接在桌面生成快捷方式
? ??Android在桌面上生成快捷方式有两种情况,一种是直接在桌面直接生成;一种是长按桌面,在弹出的快捷菜单中生成。这是讨论第一种,直接在桌面生成。
? ??这种是通过广播(Broadcast)的形式向Luncher发送请求生成快捷方式的。
首先在activity中设置:
? ? 然后添加权限:
?下面就是代码层的实现:
在activity中创建一个创建快捷方式的方法:addShortCut();
public boolean addShortcut() {// 创建快捷方式的IntentIntent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");// 不允许重复创建shortcutintent.putExtra("duplicate", false);// 快捷方式名称shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.shortcutname));// 快捷方式图片Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.shortcut);shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 点击快捷图片,运行的程序主入口shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), Activity01.class));// 发送广播。OKsendBroadcast(shortcutintent);return true;}?