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);
}
}
}
}