zoukankan      html  css  js  c++  java
  • 添加(创建)和删除及判断是否存在桌面快捷方式


    /**
    * 判断桌面是否已添加快捷方式
    *
    * @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);
    }

    转载于:http://orgcent.com/android-add-del-shortcut-desktop/

  • 相关阅读:
    SSAS aggregation 的作用及其使用
    liblinear使用总结
    python绝对路径相对路径函数
    libsvm使用总结
    一次scrapy成功停止的信息
    简单总结scrapy使用方法
    python编码格式
    一次scrapy失败的提示信息:由于连接方在一段时间后没有正确答复或连接的主机没有反 应,连接尝试失败
    17.1 MySQL主从介绍 17.2 准备工作 17.3 配置主 17.4 配置从 17.5 测试主从同步
    16.1 Tomcat介绍 16.2 安装jdk 16.3 安装Tomcat
  • 原文地址:https://www.cnblogs.com/shortboy/p/2605071.html
Copyright © 2011-2022 走看看