效果图:
Caused by: java.lang.SecurityException: No active admin owned by uid 10038 for policy #3
该错误需要把应用程序升级为拥有系统管理员权限,写一个广播接收者,给该广播接收者去申请系统管理员的权限,让操作系统给广播接收者授权。(其实就去激活系统的一个授权的组件)让用户自己激活。
锁屏步骤:
1.创建 MyAdmin 的广播接受者 继承 DeviceAdminReceiver
1 package com.example.lockscreen; 2 3 import android.app.admin.DeviceAdminReceiver; 4 5 public class MyAdmin extends DeviceAdminReceiver { 6 7 }
2.清单文件中的配置:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.lockscreen" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="8" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name="com.example.lockscreen.MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 26 <receiver android:name=".MyAdmin" > 27 <meta-data android:name="android.app.device_admin" 28 android:resource="@xml/my_admin"/> 29 <intent-filter > 30 <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/> 31 </intent-filter> 32 </receiver> 33 </application> 34 35 </manifest>
3.在res文件建立xml/my_admin.xml文件:
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
4.MainActivity代码:
1 package com.example.lockscreen; 2 3 import android.app.Activity; 4 import android.app.admin.DevicePolicyManager; 5 import android.content.ComponentName; 6 import android.content.Context; 7 import android.content.Intent; 8 import android.os.Bundle; 9 import android.view.View; 10 11 public class MainActivity extends Activity { 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 } 18 19 public void onClick(View v){ 20 // 设备安全管理服务 2.2之前的版本是没有对外暴露的 只能通过反射技术获取 21 DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); 22 23 // 申请权限 24 ComponentName componentName = new ComponentName(this, MyAdmin.class); 25 // 判断该组件是否有系统管理员的权限 26 boolean isAdminActive = devicePolicyManager.isAdminActive(componentName); 27 if(isAdminActive){ 28 29 devicePolicyManager.lockNow(); // 锁屏 30 31 devicePolicyManager.resetPassword("123", 0); // 设置锁屏密码 32 // devicePolicyManager.wipeData(0); 恢复出厂设置 (建议大家不要在真机上测试) 模拟器不支持该操作 33 34 } else { 35 Intent intent = new Intent(); 36 // 指定动作名称 37 intent.setAction(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); 38 // 指定给哪个组件授权 39 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); 40 startActivity(intent); 41 } 42 43 44 } 45 }
注:如果是在2.2版本之前的话,那么必须用反射来解决,
1 DevicePolicyManager mService = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); 2 Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class); 3 IBinder binder = (IBinder) method.invoke(null,new Object[] { Context.DEVICE_POLICY_SERVICE }); 4 mService = IDevicePolicyManager.Stub.asInterface(binder); 5 6 7 8 3.注册广播接受者为admin设备 9 ComponentName mAdminName = new ComponentName(this, MyAdmin.class); 10 if (mService != null) { 11 if (!mService.isAdminActive(mAdminName)) { 12 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); 13 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,mAdminName); 14 startActivity(intent); 15 } 16 }