Created
April 27, 2014 11:22
-
-
Save henryhuypham/11343236 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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