Menu

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, EnterProKioskbtn, GetProKioskStatebtn ExitProKioskbtn.

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 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.ActivateLicensebtn);
        Button EnterProKioskBtn = (Button) findViewById(R.id.EnterProKioskbtn);
        Button GetProKioskStateBtn = (Button) findViewById(R.id.GetProKioskStatebtn);
        Button ExitProKioskBtn = (Button) findViewById(R.id.ExitProKioskbtn);
        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();
            }
        });
        EnterProKioskBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                enterProKioskMode();
            }
        });
        GetProKioskStateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getProKioskState();
            }
        });
        ExitProKioskBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exitProKioskMode();
            }
        });
    }

	//the logic for onClickListener functions are done here

}

Tutorial Progress

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