Menu
NOTE - As of Knox 3.0, the preferred way to call Knox APIs is to upgrade an Android Work Profile to a Knox Workspace. This tutorial uses Android's Device Manager to call Knox APIs. See Upgrade Android Work Profile to Knox Workspace for more information.

Manage device applications

Now that you have set up the device admin receiver and activated your license, you can use the APIs to manage other device apps.

Show application status in Logview

This method outputs all the enabled and disabled applications in a list.

private void showAppState (boolean state) {
        try {
            // Instantiate the EnterpriseDeviceManager class
            EnterpriseDeviceManager edm = EnterpriseDeviceManager.getInstance(this);

            // getApplicationStateList() gets the list of enabled or disabled applications based on state.
            String[] packageList = edm.getApplicationPolicy().getApplicationStateList(state);
            if (packageList == null) { // If an empty array returns, no enabled/disabled apps received
                if (state) {
                    mUtils.log(getString(R.string.no_enabled_apps));
                }
                else {
                    mUtils.log(getString(R.string.no_disabled_apps));
                }
            } else { // If not an empty array, display the contents whether its enabled or disabled apps
                if (state) {
                    mUtils.log(getString(R.string.showing_enabled_apps));
                } else {
                    mUtils.log(getString(R.string.showing_disabled_apps));
                }
                for (String packageName : packageList) {
                    mUtils.log(packageName);
                }
                mUtils.log(getString(R.string.finished_showing_app_list));
            }
        } catch(SecurityException e) {
            mUtils.processException(e, TAG);
        }
    }

Enable all the disabled applications

private void enableAllDisabledApps () {
        // Instantiate the EnterpriseDeviceManager class
        EnterpriseDeviceManager enterpriseDeviceManager = EnterpriseDeviceManager.getInstance(this);
        // Get the ApplicationPolicy class where the setApplicationStateList method lives
        ApplicationPolicy applicationPolicy = enterpriseDeviceManager.getApplicationPolicy();
        try {
            // Get an array of all the disabled packages
            String [] disabledPackages = applicationPolicy.getApplicationStateList(false);

            // Enable packages in the array received in previous line, get an array of successfully enabled packages
            String [] enabledPackages = applicationPolicy.setApplicationStateList(disabledPackages, true);
            if (enabledPackages == null) {
                mUtils.log(getString(R.string.failed_enable_all_apps));
            } else {
                for (String enabledPackage : enabledPackages) {
                    mUtils.log(getString(R.string.enabled_package, enabledPackage));
                }
            }
        } catch (SecurityException e) {
            mUtils.processException(e, TAG);
        }
    }        

Prompt the user for a package name to enable or disable.

 private void promptUserForPackageName (final boolean state) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        if (state) { // Set the dialog's title to enable or disable depending on state
            builder.setTitle(getString(R.string.enable_given_app));
        } else {
            builder.setTitle(getString(R.string.disable_given_app));
        }

        View viewInflated = LayoutInflater.from(this).inflate(R.layout.prompt_user_package_name, (ViewGroup) findViewById(R.id.prompt_user_package_name), false);
        final EditText PackageNamedtxt = (EditText) viewInflated.findViewById(R.id.PackageNamedtxt);

        builder.setView(viewInflated);
        builder.setPositiveButton(getString(R.string.option_confirm), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String packageName = PackageNamedtxt.getText().toString(); // Get the user provided package name

                if (state) { // Enable or disable the given package name depending on state
                    enableGivenApp(packageName);
                } else {
                    disableGivenApp(packageName);
                }
                dialog.dismiss();
            }
        });
        builder.setNegativeButton(getString(R.string.option_cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.show();
    }     

Enable given application

private void enableGivenApp (String packageName) {
        // Instantiate the EnterpriseDeviceManager class
        EnterpriseDeviceManager enterpriseDeviceManager = EnterpriseDeviceManager.getInstance(this);
        // Get the ApplicationPolicy class where the setEnableApplication method lives
        ApplicationPolicy applicationPolicy = enterpriseDeviceManager.getApplicationPolicy();
        try {
            // Enable the given app package name
            boolean appEnabled = applicationPolicy.setEnableApplication(packageName);

            mUtils.log(getResources().getString(R.string.enabled_app_result, appEnabled));
        } catch (Exception e) {
            mUtils.processException(e, TAG);
        }
    }       

Disable given application

private void disableGivenApp (String packageName) {
        // Instantiate the EnterpriseDeviceManager class
        EnterpriseDeviceManager enterpriseDeviceManager = EnterpriseDeviceManager.getInstance(this);
        // Get the ApplicationPolicy class where the setDisableApplication method lives
        ApplicationPolicy applicationPolicy = enterpriseDeviceManager.getApplicationPolicy();
        try {
            // Disable the given app package name
            boolean appDisabled = applicationPolicy.setDisableApplication(packageName);

            mUtils.log(getResources().getString(R.string.disabled_app_result, appDisabled));
        } catch (Exception e) {
            mUtils.processException(e, TAG);
        }
    }

Declare permissions in the manifest

Declare the following permissions in AndroidManifest.xml.

<uses-permission android:name="com.example.supportlibclient.SUPPORT_PERMISSION" />
<uses-permission android:name="com.samsung.android.knox.permission.KNOX_APP_MGMT"/>

Tutorial Progress

You are 8/9 done! Go to the next step.