---
title: "Android 创建和删除快捷方式以及判断是否有快捷方式"
author: "Java-Object"
date: "2013-09-27T15:31:04Z"
tags: []
categories: ["编程语言"]
slug: "android"
draft: false
lastmod: "2013-09-27T15:31:04Z"
id: 1948246
url: "https://hezd.iteye.com/blog/1948246"
---

Launcher为了应用程序能够定制自己的快捷图标，就注册了一个BroadcastReceiver专门接收其他应用程序发来的快捷图标定制信息。所以只需要根据该BroadcastReceiver构造出相对应的Intent并装入我们的定制信息，最后调用sendBroadcast方法就可以创建一个快捷图标了。既然可以创建快捷方式也就可以删除快捷方式，以及判断快捷方式是否存在。

### 如何向这个 BroadcastReceiver 发送广播，设置如下：

1. 创建快捷方式必须要有权限；
2. 创建快捷方式的广播的Intent的action设置com.android.launcher.action.INSTALL\_SHORTCUT
3. 删除快捷方式的广播的Intent的action设置com.android.launcher.action.UNINSTALL\_SHORTCUT
4. 设置快捷方式的图片和名称等信息放在Intent中；

快捷图标的信息则是以附加信息的形式存储在广播出去的Intent对象中的，包括有图标、显示的名称以及用来启动目标组件的Intent这三种信息。我们可以通过putExtra的重载方法，通过指定相应的键值，将这些信息放到附加信息的Bundle对象中。

下面是快捷方式的工具类，可以创建快捷方式，删除快捷方式等方法。

 

**\[java]** [view plain](http://blog.csdn.net/lonely_fireworks/article/details/7964183 "view plain")[copy](http://blog.csdn.net/lonely_fireworks/article/details/7964183 "copy")

 

01. /\**
02. * @author wangli 快捷方式工具类
03. \*/
04. public class ShortCutUtils {
05. /\**
06. * 添加当前应用的桌面快捷方式
07. \*
08. * @param cx
09. \*/
10. public static void addShortcut(Context cx) {
11. Intent shortcut = new Intent(
12. "com.android.launcher.action.INSTALL\_SHORTCUT");
13. Intent shortcutIntent = cx.getPackageManager()
14. .getLaunchIntentForPackage(cx.getPackageName());
15. shortcut.putExtra(Intent.EXTRA\_SHORTCUT\_INTENT, shortcutIntent);
16. // 获取当前应用名称
17. String title = null;
18. try {
19. final PackageManager pm = cx.getPackageManager();
20. title = pm.getApplicationLabel(
21. pm.getApplicationInfo(cx.getPackageName(),
22. PackageManager.GET\_META\_DATA)).toString();
23. } catch (Exception e) {
24. }
25. // 快捷方式名称
26. shortcut.putExtra(Intent.EXTRA\_SHORTCUT\_NAME, title);
27. // 不允许重复创建（不一定有效）
28. shortcut.putExtra("duplicate", false);
29. // 快捷方式的图标
30. Parcelable iconResource = Intent.ShortcutIconResource.fromContext(cx,
31. R.drawable.ic\_launcher);
32. shortcut.putExtra(Intent.EXTRA\_SHORTCUT\_ICON\_RESOURCE, iconResource);
33. cx.sendBroadcast(shortcut);
34. }
35. /\**
36. * 删除当前应用的桌面快捷方式
37. \*
38. * @param cx
39. \*/
40. public static void delShortcut(Context cx) {
41. Intent shortcut = new Intent(
42. "com.android.launcher.action.UNINSTALL\_SHORTCUT");
43. // 获取当前应用名称
44. String title = null;
45. try {
46. final PackageManager pm = cx.getPackageManager();
47. title = pm.getApplicationLabel(
48. pm.getApplicationInfo(cx.getPackageName(),
49. PackageManager.GET\_META\_DATA)).toString();
50. } catch (Exception e) {
51. }
52. // 快捷方式名称
53. shortcut.putExtra(Intent.EXTRA\_SHORTCUT\_NAME, title);
54. Intent shortcutIntent = cx.getPackageManager()
55. .getLaunchIntentForPackage(cx.getPackageName());
56. shortcut.putExtra(Intent.EXTRA\_SHORTCUT\_INTENT, shortcutIntent);
57. cx.sendBroadcast(shortcut);
58. }
59. /\**
60. * 判断当前应用在桌面是否有桌面快捷方式
61. \*
62. * @param cx
63. \*/
64. public static boolean hasShortcut(Context cx) {
65. boolean result = false;
66. String title = null;
67. try {
68. final PackageManager pm = cx.getPackageManager();
69. title = pm.getApplicationLabel(
70. pm.getApplicationInfo(cx.getPackageName(),
71. PackageManager.GET\_META\_DATA)).toString();
72. } catch (Exception e) {
73. }
74. final String uriStr;
75. if (android.os.Build.VERSION.SDK\_INT &lt; 8) {
76. uriStr = "content://com.android.launcher.settings/favorites?notify=true";
77. } else {
78. uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
79. }
80. final Uri CONTENT\_URI = Uri.parse(uriStr);
81. final Cursor c = cx.getContentResolver().query(CONTENT\_URI, null,
82. "title=?", new String\[] { title }, null);
83. if (c != null && c.getCount() &gt; 0) {
84. result = true;
85. }
86. return result;
87. }
88. }

其中涉及的3个权限

 

 

**\[html]** [view plain](http://blog.csdn.net/lonely_fireworks/article/details/7964183 "view plain")[copy](http://blog.csdn.net/lonely_fireworks/article/details/7964183 "copy")

 

1. &lt;uses-permission android:name="com.android.launcher.permission.INSTALL\_SHORTCUT" /&gt;
2. &lt;uses-permission android:name="com.android.launcher.permission.UNINSTALL\_SHORTCUT" /&gt;
3. &lt;uses-permission android:name="com.android.launcher.permission.READ\_SETTINGS" /&gt;

 

通过这个类，你就可以轻而易举的为应用程序添加快捷图标。

 

转载自：[http://blog.csdn.net/lonely\_fireworks/article/details/7964183](http://blog.csdn.net/lonely_fireworks/article/details/7964183)