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, 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;
    @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 ActivateBtn = (Button) findViewById(R.id.ActivateLicencebtn);
        Button GrantPermissonsBtn = (Button) findViewById(R.id.GrantPermissionsbtn);
        mToggleDefaultKioskBtn = (Button) findViewById(R.id.ToggleDefaultKioskbtn);
        mToggleCustomKioskBtn = (Button) findViewById(R.id.ToggleCustomKioskbtn) ;
        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();
            }
        });
        ActivateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activateLicense();
            }
        });
        GrantPermissonsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){grantPermissions();
            }
        });
        mToggleDefaultKioskBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleKiosk();
            }
        });
        mToggleCustomKioskBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                promptCustomKioskSetting();
            }
        });
        mSampleKioskReceiver = new SampleKioskReceiver();
        mSampleKioskReceiver.setMainActivityHandler(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction( KioskMode.ACTION_ENABLE_KIOSK_MODE_RESULT);
        intentFilter.addAction(KioskMode.ACTION_DISABLE_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.