服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android添加(创建)、删除及判断是否存在桌面快捷方式的方法

Android添加(创建)、删除及判断是否存在桌面快捷方式的方法

2021-03-23 15:183H Android

这篇文章主要介绍了Android添加(创建)、删除及判断是否存在桌面快捷方式的方法,涉及Android针对桌面快捷方式的相关操作技巧,需要的朋友可以参考下

本文实例讲述了Android添加(创建)、删除及判断是否存在桌面快捷方式的方法。分享给大家供大家参考。具体实现方法如下:

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

希望本文所述对大家的Android程序设计有所帮助。

延伸 · 阅读

精彩推荐