ITeye博客

Android 创建和删除快捷方式以及判断是否有快捷方式

Java-Object · 2013-09-27 15:31 · Android技术 · 编程语言 · Markdown

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 plaincopy

 

  1. /**
  2. * @author wangli 快捷方式工具类
  3. */
  4. public class ShortCutUtils {
  5. /**
  6. * 添加当前应用的桌面快捷方式
  7. *
  8. * @param cx
  9. */
  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 < 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() > 0) {
  84. result = true;
  85. }
  86. return result;
  87. }
  88. }

其中涉及的3个权限

 

 

[html] view plaincopy

 

  1. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
  2. <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
  3. <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />

 

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

 

转载自:http://blog.csdn.net/lonely_fireworks/article/details/7964183

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。