Initialize the app
Here, you’ll 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 initializeonClickListeners
to call the respective buttons:mToggleAdminBtn
activateLicenseBtn
deactivateLicenseBtn
activateBackwardsCompatibleKeyBtn
showEnabledPackagesBtn
showDisabledPackagesBtn
enableAllDisabledPackages
enablePackageBtn
disablePackageBtn
toggleBatteryOptimizationWhitelistBtn
toggleForceStopBlacklistBtn
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 mDevicePolicyManager;
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 = findViewById(R.id.logview_id);
logView.setMovementMethod(new ScrollingMovementMethod());
mToggleAdminBtn = findViewById(R.id.toggleAdminBtn);
Button activateLicenseBtn = findViewById(R.id.activateLicenseBtn);
Button deactivateLicenseBtn = findViewById(R.id.deactivateLicenseBtn);
Button showEnabledPackagesBtn = findViewById(R.id.showEnabledPackagesBtn);
Button showDisabledPackagesBtn = findViewById(R.id.showDisabledPackagesBtn);
Button enableAllDisabledPackagesBtn = findViewById(R.id.enableAllDisabledPackagesBtn);
Button enablePackageBtn = findViewById(R.id.enablePackageBtn);
Button disablePackageBtn = findViewById(R.id.disablePackageBtn);
Button activateBackwardsCompatibleKeyBtn = findViewById(R.id.activateBackwardsCompatibleKeyBtn);
Button toggleBatteryOptimizationWhitelistBtn = findViewById(R.id.toggleBatteryOptimizationWhitelistBtn);
Button toggleForceStopBlacklistBtn = findViewById(R.id.toggleForceStopBlacklistBtn);
mDeviceAdmin = new ComponentName(MainActivity.this, SampleAdminReceiver.class);
mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
mUtils = new Utils(logView, TAG);
mToggleAdminBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {toggleAdmin();
}
});
activateLicenseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
activateLicense();
}
});
deactivateLicenseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deactivateLicense();
}
});
activateBackwardsCompatibleKeyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
activateBackwardsCompatibleKey();
}
});
// Backwards Compatibility button is only visible if the device is
// on at least Knox version 2.5 AND lower than Knox version 2.8 (Knox API 17 to 21)
if(EnterpriseDeviceManager.getAPILevel() < EnterpriseDeviceManager.KNOX_VERSION_CODES.KNOX_2_8 &&
EnterpriseDeviceManager.getAPILevel() >= EnterpriseDeviceManager.KNOX_VERSION_CODES.KNOX_2_5) {
activateBackwardsCompatibleKeyBtn.setVisibility(View.VISIBLE);
}
showEnabledPackagesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPackageState(true);
}
});
showDisabledPackagesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPackageState(false);
}
});
enableAllDisabledPackagesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
enableAllDisabledPackages();
}
});
enablePackageBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promptUserForPackageToEnableOrDisable(true);
}
});
disablePackageBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promptUserForPackageToEnableOrDisable(false);
}
});
toggleBatteryOptimizationWhitelistBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promptUserPackageForBatteryWhitelist();
}
});
toggleForceStopBlacklistBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promptUserPackageForForceStopBlacklist();
}
});
}
//the logic for onClickListener functions are done here
}
Tutorial progress
You’ve completed 2/7 steps!
NextOn this page
Is this page helpful?