Instantly share code, notes, and snippets.

Embed
What would you like to do?
Main Activity Part 2 for the Lock Screen Device App on the SSaurel's Channel
package com.ssaurel.lockdevice;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
// ...
@Override
public void onClick(View view) {
if (view == lock) {
boolean active = devicePolicyManager.isAdminActive(compName);
if (active) {
devicePolicyManager.lockNow();
} else {
Toast.makeText(this, "You need to enable the Admin Device Features", Toast.LENGTH_SHORT).show();
}
} else if (view == enable) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, compName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Additional text explaining why we need this permission");
startActivityForResult(intent, RESULT_ENABLE);
} else if (view == disable) {
devicePolicyManager.removeActiveAdmin(compName);
disable.setVisibility(View.GONE);
enable.setVisibility(View.VISIBLE);
}
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment