Android Programming Get system Hide service Implement lock screen method _android

Source: Internet
Author: User
Tags myadmin

This article describes the Android programming acquisition system hidden service implementation of the lock screen method. Share to everyone for your reference, specific as follows:

realization Principle: When press the lock screen key, will emit a broadcast, when the interface receives a broadcast can realize the lock frequency. We can invoke the Locknow method in the Idevicepolicymanager service to send a broadcast to implement the lock screen.

Idevicepolicymanager is hidden by the system and needs to be reflected to get the service.

Steps:

1. Create a MyAdmin broadcast recipient inherits Deviceadminreceiver
2. Through reflection, get Idevicepolicymanager service, Idevicepolicymanager through Aidl to get out.
3. Registered broadcast receiver for admin device
4. Access to methods in the service

Effect Chart:

Registered 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 Get Service, register permissions, implement lock screen:

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 view) {try {//get to the SDK hidden service method by reflection = Class.forName ("Android.os.
      ServiceManager "). GetMethod (" GetService ", String.class);
       IBinder binder = (ibinder) method.invoke (null,//activate service new object[] {context.device_policy_service});
       Mservice = IDevicePolicyManager.Stub.asInterface (binder);
       Defines the name of the component componentname Madminname = new ComponentName (this, myadmin.class); Registration permission if (mservice!= null) {//Determine if the custom broadcast recipient is registered as a deviceadmin permission if (!mservice.isadminactive (Madminname))
                {Intent Intent = new Intent (devicepolicymanager.action_add_device_admin); Intent.putextra (devicepolicymanager.extra_device_aDMIN, Madminname);
              StartActivity (Intent);
          //Call Service Implementation lock screen Mservice.locknow ();
       Set the unlock password Mservice.resetpassword ("123", 0);
    } catch (Exception e) {e.printstacktrace ();

 }
  }
}

Aidl:

* * * * * Copyright, the Android Open Source Project * * * * licensed under the Apache License, Version 2.0 (the "Licen
Se ");
* * may not use this file except in compliance with the License. * * Obtain a copy of the License at * * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * unless required by app Licable or agreed to in writing, software * * Distributed under the License is distributed on ' as is ' basis, * * with
Out warranties or CONDITIONS of any KIND, either express OR implied.
* * 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 the 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 ();

 }

I hope this article will help you with your Android programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.