Implementation principle: When you press the screen lock key, a broadcast is sent, and a broadcast is received on the interface to implement the lock frequency. We can call the locknow method in the idevicepolicymanager service to send a broadcast to implement screen lock.
Idevicepolicymanager is hidden by the system and needs to be obtained through reflection.
Steps:
1. Create a broadcast receiver for MyAdmin to inherit deviceadminreceiver
2. Get the idevicepolicymanager service through reflection, and get the idevicepolicymanager through aidl.
3. register the broadcast receiver as the admin device.
4. Obtain methods in the service
:
Register the MyAdmin broadcast receiver:
<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>
My_admin.xml:
<?xml version="1.0" encoding="utf-8"?><device-admin xmlns:android="http://schemas.android.com/apk/res/android"> <uses-policies> <limit-password /> <watch-login /> <reset-password /> <force-lock /> <wipe-data /> </uses-policies></device-admin>
Reflection to get services, registration permissions, and screen lock:
Public class lockactivity extends activity {idevicepolicymanager mservice; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main);} // lock screen public void lock (view) {try {// obtain the SDK-hidden service method = Class through reflection. forname ("android. OS. servicemanager "). getmethod ("getservice", String. class); ibinder binder = (ibinder) method. invoke (n Ull, // activate service new object [] {context. device_policy_service}); mservice = idevicepolicymanager. stub. asinterface (binder); // define the component name componentname madminname = new componentname (this, MyAdmin. class); // register the permission if (mservice! = NULL) {// judge whether the custom broadcast receiver is registered as the deviceadmin permission if (! Mservice. isadminactive (madminname) {intent = new intent (devicepolicymanager. action_add_device_admin); intent. putextra (devicepolicymanager. extra_device_admin, madminname); startactivity (intent) ;}// call the service to implement mservice lock. locknow (); // set the mservice to unlock the password. resetpassword ("123", 0) ;}} catch (exception e) {e. printstacktrace ();}}}
Aidl:
/***** Copyright 2010, The Android Open Source Project**** Licensed under the Apache License, Version 2.0 (the "License");** you may not use this file except in compliance with the License.** You may obtain a copy of the License at**** http://www.apache.org/licenses/LICENSE-2.0**** Unless required by applicable law or agreed to in writing, software** distributed under the License is distributed on an "AS IS" BASIS,** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.** See the License for the specific language governing permissions and** limitations under the License.*/package android.app.admin;import android.content.ComponentName;/** * Internal IPC interface to the device policy service. * {@hide} */interface IDevicePolicyManager { void setPasswordQuality(in ComponentName who, int quality); int getPasswordQuality(in ComponentName who); void setPasswordMinimumLength(in ComponentName who, int length); int getPasswordMinimumLength(in ComponentName who); boolean isActivePasswordSufficient(); int getCurrentFailedPasswordAttempts(); void setMaximumFailedPasswordsForWipe(in ComponentName admin, int num); int getMaximumFailedPasswordsForWipe(in ComponentName admin); boolean resetPassword(String password, int flags); void setMaximumTimeToLock(in ComponentName who, long timeMs); long getMaximumTimeToLock(in ComponentName who); void lockNow(); void wipeData(int flags); void setActiveAdmin(in ComponentName policyReceiver); boolean isAdminActive(in ComponentName policyReceiver); List<ComponentName> getActiveAdmins(); boolean packageHasActiveAdmins(String packageName); void removeActiveAdmin(in ComponentName policyReceiver); void setActivePasswordState(int quality, int length); void reportFailedPasswordAttempt(); void reportSuccessfulPasswordAttempt();}