android添加删除桌面快捷方式
来源:互联网 发布:js数值比大小 编辑:程序博客网 时间:2024/04/30 14:13
转载自:http://orgcent.com/android-add-del-shortcut-desktop/
Android桌面程序提供了应用添加和删除桌面快捷方式的功能以及判断快捷方式是否存在,只要传入快捷方式标题、图标及点击快捷方式执行的应用Intent即可。代码如下:
1、Android添加桌面快捷方式
/** * 为当前应用添加桌面快捷方式 * * @param cx * @param appName * 快捷方式名称 */public static void addShortcut(Context cx) { Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); Intent shortcutIntent = cx.getPackageManager() .getLaunchIntentForPackage(cx.getPackageName()); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); // 获取当前应用名称 String title = null; try { final PackageManager pm = cx.getPackageManager(); title = pm.getApplicationLabel( pm.getApplicationInfo(cx.getPackageName(), PackageManager.GET_META_DATA)).toString(); } catch (Exception e) { } // 快捷方式名称 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); // 不允许重复创建(不一定有效) shortcut.putExtra("duplicate", false); // 快捷方式的图标 Parcelable iconResource = Intent.ShortcutIconResource.fromContext(cx, R.drawable.ic_launcher); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); cx.sendBroadcast(shortcut);}
2、Android删除桌面快捷方式
/** * 删除当前应用的桌面快捷方式 * * @param cx */public static void delShortcut(Context cx) { Intent shortcut = new Intent( "com.android.launcher.action.UNINSTALL_SHORTCUT"); // 获取当前应用名称 String title = null; try { final PackageManager pm = cx.getPackageManager(); title = pm.getApplicationLabel( pm.getApplicationInfo(cx.getPackageName(), PackageManager.GET_META_DATA)).toString(); } catch (Exception e) { } // 快捷方式名称 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); Intent shortcutIntent = cx.getPackageManager() .getLaunchIntentForPackage(cx.getPackageName()); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); cx.sendBroadcast(shortcut);}
3、Android判断应用是否已存在桌面快捷方式
/** * 判断桌面是否已添加快捷方式 * * @param cx * @param titleName * 快捷方式名称 * @return */public static boolean hasShortcut(Context cx) { boolean result = false; // 获取当前应用名称 String title = null; try { final PackageManager pm = cx.getPackageManager(); title = pm.getApplicationLabel( pm.getApplicationInfo(cx.getPackageName(), PackageManager.GET_META_DATA)).toString(); } catch (Exception e) { } final String uriStr; if (android.os.Build.VERSION.SDK_INT < 8) { uriStr = "content://com.android.launcher.settings/favorites?notify=true"; } else { uriStr = "content://com.android.launcher2.settings/favorites?notify=true"; } final Uri CONTENT_URI = Uri.parse(uriStr); final Cursor c = cx.getContentResolver().query(CONTENT_URI, null, "title=?", new String[] { title }, null); if (c != null && c.getCount() > 0) { result = true; } return result;}
4、相关权限配置
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /><uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /><uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
- android添加删除桌面快捷方式
- 【Android】添加删除桌面快捷方式
- android添加删除桌面快捷方式
- Android添加桌面快捷方式和删除桌面快捷方式
- Android 添加和删除桌面快捷方式
- android桌面添加快捷方式
- Android添加桌面快捷方式
- android 添加桌面快捷方式
- android桌面快捷方式添加 删除 更新 自动运行
- Android桌面 快捷方式的删除
- 【ANDROID】创建/删除桌面快捷方式
- Android 向桌面添加快捷方式
- Android 向桌面添加快捷方式
- Android 向桌面添加快捷方式
- Android 添加桌面快捷方式操作
- Android程序添加桌面快捷方式
- android应用添加桌面快捷方式
- android 添加快捷方式到桌面
- 风的世界里有海的七滴泪水
- 修改linux的时间可以使用date指令
- 家中长辈对晚辈的教育的6个新误区
- C的memcpy和strcpy的区别是什么?
- Oracle数据库字符集问题解析
- android添加删除桌面快捷方式
- (转)国企,私企与外企利弊通观--关键时刻给应届毕业生及时点拨5
- c语言版数据结构(奇迹冬瓜)-栈实战(3)括号匹配的检测
- hdu4288线段树
- NYOJ 364 田忌赛马 (贪心)
- 黑马程序员——day12多线程(二)
- MysqlMaster/Slave备份
- Linux下,多线程程序死循环问题调试
- android 手势操作GestureDetector