实现隐藏桌面图标(PackageManager.setComponentEnabledSetting()方法)

实例:如何隐藏桌面图标

方法setComponenetEnabledSetting()方法介绍

——————————隐藏桌面图标——————————

    public void hideIcon(Context context, Class cls) {
        PackageManager packageManager = context.getPackageManager();// 获取PackageManager
        ComponentName componentName = new ComponentName(this, cls);// 获取类名对于的组件名称
        int componentState = packageManager
                .getComponentEnabledSetting(componentName);// 返回组件的启动状态
        if (componentState == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
                || componentState == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
            packageManager.setComponentEnabledSetting(componentName,
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);
        } else {
            packageManager.setComponentEnabledSetting(componentName,
                    PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
                    PackageManager.DONT_KILL_APP);
        }
    }

—————————————–setComponentEnabledSetting()———————————–

    /**
     * Set the enabled setting for a package component (activity, receiver, service, provider).
     * This setting will override any enabled state which may have been set by the component in its
     * manifest.
     *
     * @param componentName The component to enable
     * @param newState The new enabled state for the component.  The legal values for this state
     *                 are:
     *                   {@link #**COMPONENT_ENABLED_STATE_ENABLED**},//可见的
     *                   {@link #**COMPONENT_ENABLED_STATE_DISABLED**}//不可见
     *                   and
     *                   {@link #**COMPONENT_ENABLED_STATE_DEFAULT**}//默认的,可见
     *                 The last one removes the setting, thereby restoring the component's state to
     *                 whatever was set in it's manifest (or enabled, by default).
     * @param flags Optional behavior flags: {@link #DONT_KILL_APP} or 0.
     */
    public abstract void setComponentEnabledSetting(ComponentName componentName,
            int newState, int flags);

你可能感兴趣的:(android)