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.

Initialize the app

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, GrantPermissonToggleCamera.
  •  

NOTE – some of the methods that do not resolve are created later in the tutorial.

   public class MainActivity extends AppCompatActivity {	   
   public static final String TAG = "MainActivity";
    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());
        mUtils = new Utils(LogView, TAG);

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

        Button CreateAndroidProfile = (Button) findViewById(R.id.CreateAndroidProfilebtn);
        CreateAndroidProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                createAndroidProfile();
            }
        });
        Button ActivateLicencebtn = (Button) findViewById(R.id.ActivateLicencebtn);
        ActivateLicencebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activateLicence();
            }
        });
        Button DeActivateLicencebtn = (Button) findViewById(R.id.DeActivateLicencebtn);
        DeActivateLicencebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deActivateLicence();
            }
        });
        Button ToggleCamerabtn = (Button) findViewById(R.id.ToggleCamerabtn);
        ToggleCamerabtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                toggleCameraState();
            }
        });

        // Check if the applcation is a Profile Owner
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        boolean isProfileOwner = devicePolicyManager.isProfileOwnerApp(getPackageName());
        if(isProfileOwner){
            ToggleCamerabtn.setEnabled(true);
            CreateAndroidProfile.setEnabled(false);
        } else {
            ActivateLicencebtn.setEnabled(false);
            DeActivateLicencebtn.setEnabled(false);
            ToggleCamerabtn.setEnabled(false);
        }
    }
}

Tutorial Progress

You are 5/10 done! Go to the next step.