把应用程序升级为拥有系统管理员权限,写一个广播接收者,给该广播接收者去申请系统管理员的权限,让操作系统给广播接收者授权。(其实就去激活系统的一个授权的组件)让用户自己激活。
锁屏步骤:
1.创建 MyAdmin 的广播接受者 继承 DeviceAdminReceiver
package com.example.lockscreen; import android.app.admin.DeviceAdminReceiver; public class MyAdmin extends DeviceAdminReceiver { }
2.清单文件中的配置:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lockscreen" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.lockscreen.MainActivity" 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=".MyAdmin" > <meta-data android:name="android.app.device_admin" android:resource="@xml/my_admin"/> <intent-filter > <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/> </intent-filter> </receiver> </application> </manifest>
3.在res文件建立xml/my_admin.xml文件:
<?xml version="1.0" encoding="utf-8" ?> <device-admin xmlns:android="http://schemas.android.com/apk/res/android"> <!-- limit-password 设置密码的规则 watch-login 监控屏幕解锁尝试次数 reset-password 更改屏幕解锁密码 force-lock 设备自动锁屏 wipe-data 删除全部的数据 --> <uses-policies> <limit-password/> <watch-login /> <reset-password /> <force-lock /> <wipe-data /> </uses-policies> </device-admin>
4.MainActivity代码:
package com.example.lockscreen; 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; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View v){ // 设备安全管理服务 2.2之前的版本是没有对外暴露的 只能通过反射技术获取 DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); // 申请权限 ComponentName componentName = new ComponentName(this, MyAdmin.class); // 判断该组件是否有系统管理员的权限 boolean isAdminActive = devicePolicyManager.isAdminActive(componentName); if(isAdminActive){ devicePolicyManager.lockNow(); // 锁屏 devicePolicyManager.resetPassword("123", 0); // 设置锁屏密码 // devicePolicyManager.wipeData(0); 恢复出厂设置 (建议大家不要在真机上测试) 模拟器不支持该操作 } else { Intent intent = new Intent(); // 指定动作名称 intent.setAction(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); // 指定给哪个组件授权 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); startActivity(intent); } } }
注:如果是在2.2版本之前的话,那么必须用反射来解决,
DevicePolicyManager mService = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class); IBinder binder = (IBinder) method.invoke(null,new Object[] { Context.DEVICE_POLICY_SERVICE }); mService = IDevicePolicyManager.Stub.asInterface(binder);
3.注册广播接受者为admin设备
ComponentName mAdminName = new ComponentName(this, MyAdmin.class); if (mService != null) { if (!mService.isAdminActive(mAdminName)) { Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,mAdminName); startActivity(intent); } }