需要的权限:

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

/**    * 快捷方式工具类     */public class ShortCutUtils {    /**     * 添加当前应用的桌面快捷方式     * @param cx     */    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();        } catch (Exception e) {        }        // 快捷方式名称        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);        // 不允许重复创建(不一定有效)        shortcut.putExtra("duplicate", false);        // 快捷方式的图标        Parcelable iconResource = Intent.ShortcutIconResource.fromContext(cx,                R.drawable.ic_launcher);        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);        cx.sendBroadcast(shortcut);    }    /**     * 删除当前应用的桌面快捷方式     * @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();        } 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     */    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;    }}

更多相关文章

  1. android studio 3.0 修改release生成的apk名称
  2. 老外写的在桌面添加快捷方式(DELPHI XE5 ANDROID)
  3. Android跳转到通讯录获取用户名称和手机号码的实现思路
  4. android创建桌面快捷方式(启动目标非项目的启动页)
  5. android 回到桌面
  6. 终于找到一个类似wince 远程桌面控制android设备的软件——Andro
  7. android 在新建短信时,加入名称为","(英文逗号)的联系人时,应用崩
  8. Android之添加快捷方式(Shortcut)到手机桌面
  9. Android 修改U盘名称

随机推荐

  1. Android之RAS加密算法测试
  2. Android中的px,in,pt,dp,dip,sp
  3. 如何向android程序添加音效或者音频文件
  4. smartwindow
  5. Android属性动画(ObjectAnimation)
  6. Android阴影背景
  7. Android联网方式判断
  8. android 简单登陆注册的实现
  9. Android支持的音频格式
  10. android基础控件(4)GridView实现网格视图