Android 应用桌面快捷方式 创建 删除

来源:互联网 发布:ios mac地址伪装 编辑:程序博客网 时间:2024/05/21 10:08
 /**     * 创建桌面快捷方式     *      * @param context     * @param pkg     *            包名     * @return     */    public static boolean addShortCut(Activity context, String pkg) {        // 快捷方式名称        String name = "unknown";        String mainAct = null;        // 快捷图标ID        int iconIdentifier = -1;        PackageManager pkManager = context.getPackageManager();        // 创建Intent,用来Activity的查询        Intent intent = new Intent(Intent.ACTION_MAIN, null);        intent.addCategory(Intent.CATEGORY_LAUNCHER);        // 得到Activity的list        List<ResolveInfo> list = pkManager.queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);        for (ResolveInfo info : list) {            // 和传入的pkg一致时,进行处理            if (TextUtils.equals(info.activityInfo.packageName, pkg)) {                // 得到应用名称,做为快捷名                name = info.loadLabel(pkManager).toString();                // 得到应用图标                iconIdentifier = info.activityInfo.applicationInfo.icon;                // 得到该应用入口类的全类名                mainAct = info.activityInfo.name;                break;            }        }        if (TextUtils.isEmpty(mainAct)) {            return false;        }        Intent intents = new Intent(context, com.gome.ecmall.home.LaunchActivity.class);        // Intent intents = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());        // 图标和应用绑定,以便卸载时图标同时卸载        intents.setAction(Intent.ACTION_MAIN);        intents.addCategory(Intent.CATEGORY_LAUNCHER);        // 这俩个flags必须添加        intents.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);        intents.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);        // 创建快捷方式的Intent        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);        // 不允许重复创建快捷方式        shortcut.putExtra("duplicate", false);        // ComponentName comp = new ComponentName(pkg, mainAct);        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intents);        // shortcut.putExtra(        // Intent.EXTRA_SHORTCUT_INTENT,        // new Intent(Intent.ACTION_MAIN).setComponent(comp).setAction(Intent.ACTION_MAIN)        // .addCategory(Intent.CATEGORY_LAUNCHER));        Context pkgContext = null;        if (TextUtils.equals(pkg, context.getPackageName())) {            pkgContext = context;        } else {            try {                // 利用对应的PKG名称,构建Context                pkgContext = context.createPackageContext(pkg, Context.CONTEXT_IGNORE_SECURITY                        | Context.CONTEXT_INCLUDE_CODE);            } catch (NameNotFoundException e) {                e.printStackTrace();            }        }        // 添加快捷方式的图标        if (pkgContext != null) {            ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(pkgContext, iconIdentifier);            shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);        }        // 发送创建快捷方式的广播        // 需要在AndroidManifest.xml添加创建快捷方式的权限        // <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />        context.sendBroadcast(shortcut);        return true;    }
<pre name="code" class="java">/**     * 判断桌面是否已添.加快捷方式     *      * @param cx     * @param titleName     *            快捷方式名称     * @return     */    public static boolean hasShortcut(Activity mActivity) {        boolean result = false;        // 获取当前应用名称        String title = null;        try {            final PackageManager pm = mActivity.getPackageManager();            title = pm.getApplicationLabel(                    pm.getApplicationInfo(mActivity.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 = mActivity.getContentResolver().query(CONTENT_URI, null, "title=?", new String[] { title },                null);        if (c != null && c.getCount() > 0) {            result = true;        }        return result;    }    public static void delShortcut(Activity mActivity) {        Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");        // 快捷方式的名称        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mActivity.getString(R.string.app_name));        Intent intents = new Intent(mActivity, com.gome.ecmall.home.LaunchActivity.class);        // Intent intents = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());        // 图标和应用绑定,以便卸载时图标同时卸载        intents.setAction(Intent.ACTION_MAIN);        intents.addCategory(Intent.CATEGORY_LAUNCHER);        // 这俩个flags必须添加        intents.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);        intents.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intents);        mActivity.sendBroadcast(shortcut);        Intent delete = mActivity.getPackageManager().getLaunchIntentForPackage(mActivity.getPackageName());        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, delete);        mActivity.sendBroadcast(shortcut);    }


<pre name="code" class="java">/** *  应用信息【单独类】 */public class AppInfo {    private String appPkgName;    private String appLauncherClassName;    private String appName;    private Drawable appIcon;    public String getAppPkgName() {        return appPkgName;    }    public void setAppPkgName(String appPkgName) {        this.appPkgName = appPkgName;    }    public String getAppLauncherClassName() {        return appLauncherClassName;    }    public void setAppLauncherClassName(String appLauncherClassName) {        this.appLauncherClassName = appLauncherClassName;    }    public String getAppName() {        return appName;    }    public void setAppName(String appName) {        this.appName = appName;    }    public Drawable getAppIcon() {        return appIcon;    }    public void setAppIcon(Drawable appIcon) {        this.appIcon = appIcon;    }}



0 0