MainActivity.java
package com.aiden.screenoff;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final int ADMIN_INTENT = 15;
private static final String description = "Rokid Glass Launcher";
private DevicePolicyManager mDevicePolicyManager;
private ComponentName mComponentName;
private String TAG ="fyg ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** **/
mDevicePolicyManager = (DevicePolicyManager)getSystemService(
Context.DEVICE_POLICY_SERVICE);
mComponentName = new ComponentName(this, MyAdminReceiver.class);
Button btnEnableAdmin = (Button) findViewById(R.id.btnEnableAdmin);
Button btnDisableAdmin = (Button) findViewById(R.id.btnDisableAdmin);
Button btnLock = (Button) findViewById(R.id.btnLock);
btnEnableAdmin.setOnClickListener(this);
btnDisableAdmin.setOnClickListener(this);
btnLock.setOnClickListener(this);
if (!mDevicePolicyManager.isAdminActive(mComponentName)) {
Toast.makeText(this,"Please get Device-Admin privilege first.",Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.DevAdmMsg)
.setTitle("Get Device Admin");
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
Log.d(TAG, "onClick: OKay");
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,description);
startActivityForResult(intent, ADMIN_INTENT);
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
Log.d(TAG, "onClick: Cancel");
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnEnableAdmin:
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,description);
startActivityForResult(intent, ADMIN_INTENT);
break;
case R.id.btnDisableAdmin:
mDevicePolicyManager.removeActiveAdmin(mComponentName);
Toast.makeText(getApplicationContext(), "Device-Admin registration removed", Toast.LENGTH_SHORT).show();
break;
case R.id.btnLock:
boolean isAdmin = mDevicePolicyManager.isAdminActive(mComponentName);
if (isAdmin) {
mDevicePolicyManager.lockNow();
}else{
Toast.makeText(getApplicationContext(), "Not Registered as admin", Toast.LENGTH_SHORT).show();
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ADMIN_INTENT) {
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(), "Registered As Device-Admin", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Failed to register as Admin", Toast.LENGTH_SHORT).show();
}
}
}
}
MyAdminReceiver.java如下:
package com.aiden.screenoff;
import android.app.admin.DeviceAdminReceiver;
public class MyAdminReceiver extends DeviceAdminReceiver {
}
Layout文件activity_main.xml内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context="com.aiden.screenoff.MainActivity">
<Button
android:id="@+id/btnEnableAdmin"
android:text="Enable Admin"
android:textSize="20dp"
android:layout_width="200dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnDisableAdmin"
android:text="Disable Admin"
android:textSize="20dp"
android:layout_width="200dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnLock"
android:text="Lock"
android:textSize="20dp"
android:layout_width="200dp"
android:layout_height="wrap_content" />
</LinearLayout>
res/values/trings.xml
<resources>
<string name="app_name">Screenoff</string>
<string name="DevAdmMsg">Device Admin privilege is required </string>
<string name="ok">OK</string>
<string name="cancel">Cancel</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aiden.screenoff">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="MyAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
</application>
</manifest>
res/xml/admin.xml内容:
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
<uses-policies>
<force-lock />
</uses-policies>
</device-admin>