实现隐藏桌面图标(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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值