本文实例讲述了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?notify=true";} else {uriStr = "content://com.android.launcher2.settings/favorites?notify=true";}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程序设计有所帮助。

更多相关文章

  1. android获取自定义控件位置坐标,屏幕尺寸,标题栏,状态栏高度
  2. Android(安卓)FrameWork——PackageManager框架
  3. Android原生调用mui里面的js如何实现
  4. wakelock的使用
  5. Android之xml解析和json解析
  6. activity跳转黑屏但不透明桌面问题
  7. Android可平移缩放旋转的ImageView的实现
  8. Android(安卓)如何用HttpClient 以Get方式获取数据并添加http头
  9. Dalvik——基本Dalvik VM调用

随机推荐

  1. 【android】 调用别的应用的activity
  2. TextView的省略号(elipsized属性)工作原理
  3. Security and Permissions安全与权限(三)
  4. Android(安卓)MVP 模式应用实例
  5. Android中使用Timer配合postInvalidate()
  6. Android(安卓)ViewFlipper实现页面的滑动
  7. android CoordinatorLayout使用
  8. 自定义Dialog步骤
  9. android中怎样适应多个屏幕
  10. Android手机拍照(Android学习随笔九)