Android 添加、移除和判断 桌面快捷方式图标

思路:

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

 

步骤:

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

  需要添加的权限如下:

   <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"/>
    <uses-permission android:name="com.android.launcher2.permission.READ_SETTINGS"/>
    <uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS"/>

  核心代码为:

 1   /**
 2      * 添加当前应用的桌面快捷方式
 3      *
 4      * @param context
 5      */
 6     public static void addShortcut(Context context, int appIcon) {
 7         Intent shortcut = new Intent(
 8                 "com.android.launcher.action.INSTALL_SHORTCUT");
 9 
10         Intent shortcutIntent = context.getPackageManager()
11                 .getLaunchIntentForPackage(context.getPackageName());
12         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
13         // 获取当前应用名称
14         String title = null;
15         try {
16             final PackageManager pm = context.getPackageManager();
17             title = pm.getApplicationLabel(
18                     pm.getApplicationInfo(context.getPackageName(),
19                             PackageManager.GET_META_DATA)).toString();
20         } catch (Exception e) {
21         }
22         // 快捷方式名称
23         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
24         // 不允许重复创建(不一定有效)
25         shortcut.putExtra("duplicate", false);
26         // 快捷方式的图标
27         Parcelable iconResource = Intent.ShortcutIconResource.fromContext(context,
28                 appIcon);
29         shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
30 
31         context.sendBroadcast(shortcut);
32     }
33 
34     /**
35      * 删除当前应用的桌面快捷方式
36      *
37      * @param context
38      */
39     public static void delShortcut(Context context) {
40         Intent shortcut = new Intent(
41                 "com.android.launcher.action.UNINSTALL_SHORTCUT");
42 
43         // 获取当前应用名称
44         String title = null;
45         try {
46             final PackageManager pm = context.getPackageManager();
47             title = pm.getApplicationLabel(
48                     pm.getApplicationInfo(context.getPackageName(),
49                             PackageManager.GET_META_DATA)).toString();
50         } catch (Exception e) {
51         }
52         // 快捷方式名称
53         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
54         Intent shortcutIntent = context.getPackageManager()
55                 .getLaunchIntentForPackage(context.getPackageName());
56         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
57         context.sendBroadcast(shortcut);
58     }
59 
60     /**
61      * 判断当前应用在桌面是否有桌面快捷方式
62      *
63      * @param context
64      */
65     public static boolean hasShortcut(Context context) {
66         boolean result = false;
67         String title = null;
68         try {
69             final PackageManager pm = context.getPackageManager();
70             title = pm.getApplicationLabel(
71                     pm.getApplicationInfo(context.getPackageName(),
72                             PackageManager.GET_META_DATA)).toString();
73         } catch (Exception e) {
74 
75         }
76 
77         final String uriStr;
78         if (android.os.Build.VERSION.SDK_INT < 8) {
79             uriStr = "content://com.android.launcher.settings/favorites?notify=true";
80         } else if (android.os.Build.VERSION.SDK_INT < 19) {
81             uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
82         } else {
83             uriStr = "content://com.android.launcher3.settings/favorites?notify=true";
84         }
85         final Uri CONTENT_URI = Uri.parse(uriStr);
86         final Cursor c = context.getContentResolver().query(CONTENT_URI, null,
87                 "title=?", new String[]{title}, null);
88         if (c != null && c.getCount() > 0) {
89             result = true;
90         }
91         return result;
92     }

 

posted @ 2016-07-23 10:36  AnglusWang  阅读(10492)  评论(0编辑  收藏  举报