Tooth uncle tutorial is easy to understand

Results show

The environment

Autojs version: 9.0.5

Android version: 10

MIUI version: 12.5.1

MT Manager: 2.9.9

Autojs8 and AutojS9 have different app directories after packaging, and the method of adding files is roughly the same.

If you are not familiar with mt manager, use AutoJS9 to follow this tutorial

Device Manager only works on lower versions of Android and Android-like phones

One-click screen lock process

  1. Package any app with AutoJS that can execute the script for the specified path
  2. Add the admin_policies.xml file in the res directory
  3. Add class MyAdmin to inherit DeviceAdminReceiver
  4. Use the MT manager to register broadcasts in the Androidmanifest.xml manifest file

The code on

1. Add the admin_policies. XML file to the res directory
<? xml version="1.0" encoding="utf-8"? ><device-admin xmlns:android="http://schemas.android.com/apk/res/android">
	<uses-policies>
		<force-lock />
		<watch-login />
	</uses-policies>
</device-admin>
Copy the code
2. There is a resource file in the listing
android:resource="@xml/admin_policies
Copy the code
3. Add resource indexes
  1. Click on the resources. Arsc
  2. Select the Arsc editor ++
  3. Expand the XML folder
  4. Select the XML
  5. Add a line res/admin_policies.xml
  6. Return, select type-info
  7. Add a line
4. Register broadcasts in the androidmanifest.xml manifest file
<receiver android:name="com.yashu.yashu.ApplicationService$MyAdmin" android:permission="android.permission.BIND_DEVICE_ADMIN">
	<meta-data android:name="android.app.device_admin" android:resource="@xml/admin_policies" />
	<intent-filter>
		<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
		<action android:name="android.app.action.ACTION_PASSWORD_FAILED" />
		<action android:name="android.app.action.ACTION_PASSWORD_SUCCEEDED" />
	</intent-filter>
</receiver>
Copy the code
5. Add class MyAdmin to inherit DeviceAdminReceiver. Autojs9 has 2 classes.dex, renamed classes3.dex to the packaged app
package com.yashu.yashu;

public class ApplicationService extends Service {

    public static class MyAdmin extends DeviceAdminReceiver {

        private static final String TAG = "MyAdmin";

        @Override
        public void onEnabled(@NonNull Context context, @NonNull Intent intent){... } @Override publicvoid onDisabled(@NonNull Context context, @NonNull Intent intent){... } @Override publicvoid onPasswordFailed(@NonNull Context context, @NonNull Intent intent, @NonNull UserHandle user) {

        }


        @Override
        public void onPasswordSucceeded(@NonNull Context context, @NonNull Intent intent) {

        }


        @Override
        public void onReceive(@NonNull Context context, @NonNull Intent intent){... }}... }Copy the code
6. The UI interface
ui.layout(
  <vertical>
    <text text="Uncle Teeth Tutorial is easy to understand" textSize="28sp" textColor="#fbfbfe" bg="#00afff" w="*" gravity="center"></text>
    <button id="Lock screen" textSize="60sp" margin="30">Lock screen</button>
    <button id="Remove Device Manager permissions" bg="#f5222d" textSize="60sp" margin="30"></button>
  </vertical>
);
Copy the code
7. Apply for device management permission
functionApplying for Device Manager Permission () {
  let intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
  intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
  intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Uncle Teeth Tutorial easy to learn");
  activity.startActivityForResult(intent, 11);
}
Copy the code
8. Remove the device management rights
devicePolicyManager.removeActiveAdmin(componentName);
Copy the code
9. The lock screen
devicePolicyManager.lockNow();
Copy the code

Quotes.

Thinking is the most important, other Baidu, Bing, StackOverflow, Android documents, AutoJS documents, is the last group to ask

— Tooth uncle tutorial

The statement

Some of the content comes from the web

This tutorial is for learning purposes only and is not allowed for other purposes