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: ToggleAdminbtn, ActivateLicencebtn, GrantPermissionsbtn, ToggleDefaultKioskbtn ToggleCustomKioskbtn.

NOTE – 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 Button mToggleDefaultKioskBtn;
    private Button mToggleCustomKioskBtn;
    private ComponentName mDeviceAdmin;
    private DevicePolicyManager mDPM;
    private SampleKioskReceiver mSampleKioskReceiver;
    private Utils mUtils;
    protected void onCreate(Bundle savedInstanceState) {
        //...called when the activity is starting. This is where most initialization should go.

        TextView LogView = (TextView) findViewById(;
        LogView.setMovementMethod(new ScrollingMovementMethod());
        mToggleAdminBtn = (Button) findViewById(;
        Button ActivateBtn = (Button) findViewById(;
        Button GrantPermissonsBtn = (Button) findViewById(;
        mToggleDefaultKioskBtn = (Button) findViewById(;
        mToggleCustomKioskBtn = (Button) findViewById( ;
        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() {
            public void onClick(View v) {toggleAdmin();
        ActivateBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
        GrantPermissonsBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){grantPermissions();
        mToggleDefaultKioskBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
        mToggleCustomKioskBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
        mSampleKioskReceiver = new SampleKioskReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction( KioskMode.ACTION_ENABLE_KIOSK_MODE_RESULT);
        registerReceiver(mSampleKioskReceiver, intentFilter);

	//the logic for onClickListener functions are done here


Tutorial Progress

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