直接放到项目中可用:

import android.content.Context;import android.content.Intent;import android.content.pm.PackageManager;import android.database.Cursor;import android.net.Uri;import android.os.Parcelable;import cn.howie.base.ui.activity.R;/** * 添加快捷方式的工具类 */public class ShortCutUtil {/** * 为当前应用添加桌面快捷方式 * * @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;    title = cx.getString(R.string.app_name);    // 快捷方式名称    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;    title = cx.getString(R.string.app_name);    // 快捷方式名称    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 titleName *            快捷方式名称 * @return */public static boolean hasShortcut(Context cx) {    boolean result = false;    String[] tempStr = new String[2];    tempStr[0] = "title";    tempStr[1] = "iconResource";    // 获取当前应用名称    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;}}
要添加权限:

<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />或<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />



更多相关文章

  1. Android中获取应用程序(包)的信息-----PackageManager的使用(一)
  2. Android获取系统中的其他应用信息
  3. Android之Audio常用属性变量
  4. Android(安卓)异常后如何重启应用
  5. android如何禁止安装第三方应用
  6. Android(安卓)判断程序是否在前台运行
  7. AndroidManifest.xml配置文件选项详解
  8. Android(安卓)TimeLine 时间节点轴的实现
  9. android之File

随机推荐

  1. Freescale IMX6 Android (7): Android启
  2. Android应用盈利广告平台的嵌入方法详解
  3. 从架构师的角度分析Android Handler 源码
  4. android LinearLayout 单击动态改变背景
  5. 开放是一种博大的胸怀
  6. android 从文件制定位置读取数据
  7. Android(安卓)Studio报错unable to acces
  8. Android 音乐播放器的实现
  9. Android Wear开发中文教程
  10. Android打造不一样的新手引导页面(一)