在一个demo中看到以下代码,觉得以后可能有用,先收藏着。
下面是在桌面添加快捷方式的代码:
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "我的快捷方式");
shortcut.putExtra("duplicate", false); // 不允许重复创建
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.background);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); // 快捷方式资源图标
String action = "com.android.action.test";
Intent respondIntent = new Intent(this, this.getClass());
respondIntent.setAction(action);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent);
sendBroadcast(shortcut);
下面是删除快捷方式的代码:
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "我的快捷方式"); // 必须和创建时名字一致
String action = "com.android.action.test"; // 必须和创建时名字一致
String appClass = this.getPackageName() + "." + this.getLocalClassName();
ComponentName comp = new ComponentName(this.getPackageName(), appClass);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(action).setComponent(comp));
sendBroadcast(shortcut);
最后别忘了还要声明权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />