Back to top

Initialize the app

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.

Here, we set up the app with some basic functions

  • onCreate() — links the xml layout to the activities, set the enabled state of the buttons and declare and initialize onClickListeners to call respective buttons — ActivateAdmin, DeActivateAdmin,ActivateLicence, GrantPermisson, and ToggleCamera.

    Some of the methods that do not resolve are created later in the tutorial.

   public class MainActivity extends AppCompatActivity {

    private final String TAG = "MainActivity";
    static final int DEVICE_ADMIN_ADD_RESULT_ENABLE = 1;

    private Button mToggleAdminBtn;
    private ComponentName mDeviceAdmin;
    private DevicePolicyManager mDPM;
    private Utils mUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //...called when the activity is starting. This is where most initialization should go.
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView LogView = (TextView) findViewById(R.id.logview_id);
        LogView.setMovementMethod(new ScrollingMovementMethod());
        mToggleAdminBtn = (Button) findViewById(R.id.ToggleAdminbtn);
        Button ActivateLicenseBtn = (Button) findViewById(R.id.ActivateLicencebtn);
        Button ShowEnabledAppsBtn = (Button) findViewById(R.id.ShowEnabledAppsbtn);
        Button ShowDisabledAppsBtn = (Button) findViewById(R.id.ShowDisabledAppsbtn);
        Button EnableAllDisabledAppsBtn = (Button) findViewById(R.id.EnableAllDisabledAppsbtn);
        Button EnableGivenAppBtn = (Button) findViewById(R.id.EnableGivenAppbtn);
        Button DisableGivenAppBtn = (Button) findViewById(R.id.DisableGivenAppbtn);

        mDeviceAdmin = new ComponentName(MainActivity.this, SampleAdminReceiver.class);
        mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        mUtils = new Utils(LogView, TAG);

        // Check if device supports Knox SDK
        mUtils.checkApiLevel(24, this);

        mToggleAdminBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {toggleAdmin();
            }
        });
        ActivateLicenseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activateLicense();
            }
        });
        ShowEnabledAppsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAppState(true);
            }
        });
        ShowDisabledAppsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAppState(false);
            }
        });
        EnableAllDisabledAppsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { enableAllDisabledApps();
            }
        });
        EnableGivenAppBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { promptUserForPackageName(true);
            }
        });
        DisableGivenAppBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { promptUserForPackageName(false);
            }
        });
    }

    //the logic for onClickListener functions are done here


}

Tutorial progress

You are 5/10 done!

Next

On this page

Is this page helpful?