How to hide application icon?

To Hide app icon from launcher programatically you can do this

    PackageManager packageManager = context.getPackageManager();
    ComponentName componentName = new ComponentName(context,
            LauncherActivity.class);
    packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);
For example, We want to disable camera-related activities if there is no camera.
Firstly, register receiver in manifest file, This receiver runs when BOOT_COMPLETED intent is received.
        <receiver android:name=".DisableCameraReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
Then create a receiver class
public class DisableCameraReceiver extends BroadcastReceiver {
    private static final String TAG = "DisableCameraReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        // Disable camera-related activities if there is no camera.
        boolean needCameraActivity = hasCamera();

        if (!needCameraActivity) {
            Log.i(TAG, "disable all camera activities");
            ComponentName name = new ComponentName(context, "com.android.camera.Camera");
            PackageManager pm = context.getPackageManager();


            int enable_state = pm.getComponentEnabledSetting(name);
            // We need the DONT_KILL_APP flag, otherwise we will be killed immediately because we are in the same app.
            if (enable_state != PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
               pm.setComponentEnabledSetting(name,
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);
            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值