手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android添加(创建)、删除及判断是否存在桌面快捷方式的方法
Android添加(创建)、删除及判断是否存在桌面快捷方式的方法
摘要:本文实例讲述了Android添加(创建)、删除及判断是否存在桌面快捷方式的方法。分享给大家供大家参考。具体实现方法如下:/***判断桌面是否...

本文实例讲述了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"; } else { uriStr = "content://com.android.launcher2.settings/favorites"; } 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; } /** * 删除当前应用的桌面快捷方式 * * @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(); Log.v("test", "title:" + title); } 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); } /** * 为当前应用添加桌面快捷方式 * * @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(); Log.v("test", "title:" + title); } catch (Exception e) { } // 快捷方式名称 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); // 不允许重复创建(不一定有效) shortcut.putExtra("duplicate", false); // 快捷方式的图标 Parcelable iconResource = Intent.ShortcutIconResource.fromContext(cx, R.drawable.icon); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); cx.sendBroadcast(shortcut); }

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

【Android添加(创建)、删除及判断是否存在桌面快捷方式的方法】相关文章:

android打开应用所在的市场页面进行评分操作的方法

android动态布局之动态加入TextView和ListView的方法

Android判断包名和类名是否存在的方法

android中ListView多次刷新重复执行getView的解决方法

Android ListView数据绑定显示的三种解决方法

基于Android中手势交互的实现方法

解析Android开发优化之:对界面UI的优化详解(三)

Android控件系列之Button以及Android监听器使用介绍

Android界面刷新的方法分享

解析Android应用启动后自动创建桌面快捷方式的实现方法

精品推荐
分类导航