Skip to content

Instantly share code, notes, and snippets.

@henryhuypham
Created April 27, 2014 11:22
Show Gist options
  • Save henryhuypham/11343236 to your computer and use it in GitHub Desktop.
Save henryhuypham/11343236 to your computer and use it in GitHub Desktop.
package com.example.testadmin;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private ComponentName componentName;
private DevicePolicyManager mDPM;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
componentName = new ComponentName(MainActivity.this, android.app.admin.DeviceAdminReceiver.class);
mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
Button osBt = (Button) findViewById(R.id.offScreen);
osBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mDPM.lockNow();
}
});
Button eoBt = (Button) findViewById(R.id.enableOff);
eoBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mDPM.isAdminActive(componentName))
return;
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Additional text explaining why this needs to be added.");
startActivityForResult(intent, 1001);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment