Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created June 7, 2018 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaurel/a2fa0ec3863bb411d8e572b243fa1c7c to your computer and use it in GitHub Desktop.
Save ssaurel/a2fa0ec3863bb411d8e572b243fa1c7c to your computer and use it in GitHub Desktop.
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