adb+DisplayToggle.dex实现手机屏幕关闭但不休眠

使用过 scrcpy、QTScrcpy、ALINK的人应该都知道,这些应用连上手机后可以关闭手机屏幕,继续操控手机, 这篇文章就是介绍手机如何息屏不休眠

  • 问题发起者
    来自scrcpy的issues讨论

  • 只用手机实现息屏不休眠

  1. 手机开启开发者模式

  2. 在开发者选项中打开USB调试, 如果有禁止权限监控(Android12)也打开

  3. 准备好手机上能执行adb命令的工具, 比如tasker、termux

  4. 按照这篇文章编译出DisplayToggle.dex

    • 懒人直用 提取码:1234, 我在华为机(安卓10)上编译的, 复制到一加(安卓12)可用

    • 这篇文章已经介绍的很详细了, termux傻瓜式编译, pc上编译准备好java,dx,android-tools再按命令编译即可

  5. 上面文章里介绍了在termux和电脑上使用, 我在这里介绍下如果在tasker中使用

    • 我使用的tasker是5.12.21汉化版

    • 在tasker中新建任务, 选择"代码"-“ADB WIFI”

    • 在命令中输入 CLASSPATH=/storage/emulated/0/Download/Bluetooth/DisplayToggle.dex app_process / DisplayToggle 0, 注意CLASSPATH后面跟的是DisplayToggle.dex的实际地址,最后的0代表关闭屏幕,

    • 如果你没有进行过usb的调试授权, 执行adb WIFI任务的时候, 应该会提示你授权

    • 使用adb关闭屏幕后, 安卓系统并没有认为屏幕已关闭, 如果你想手动亮屏, 得按电源键两次

    • 设置系统不休眠

      • adb命令: adb shell settings put system screen_off_timeout 2147483647

      • tasker中ADB WIFI命令: settings put system screen_off_timeout 2147483647

      • 2147483647是指屏幕关闭的毫秒数, 这是最大值, 约为25天

下面是我从文章中复制过来的内容, 加了翻译

Description 描述

Turn ON/OFF the display of your Android phone, like scrcpy, using ADB Shell or Root.
使用 ADB Shell 或 Root 打开/关闭 Android 手机的显示,例如 scrcpy。

Check Reddit Tasker for Tasker users.
检查 Reddit Tasker 中的 Tasker 用户。

Termux users can continue to the next section.
Termux 用户可以继续下一部分。

Building 建筑

You can follow these steps to build it in Termux.
您可以按照以下步骤在 Termux 中构建它。

pkg upgrade -y
pkg install -y wget openjdk-17 dx android-tools
cd; wget -O android.jar "https://github.com/Sable/android-platforms/blob/master/android-30/android.jar?raw=true"
nano DisplayToggle.java

Now copy paste this:-
现在复制粘贴以下内容:-

import android.os.Build;
import android.os.IBinder;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
public class DisplayToggle {

    private static final Class<?> CLASS;
	static {
        	try {
            		CLASS = Class.forName("android.view.SurfaceControl");
		} catch (ClassNotFoundException e) {
        	throw new AssertionError(e);
     	 }
    }

    public static void main(String... args) throws Exception {
	System.out.print("Display mode: "+args[0]);
	Method method = CLASS.getMethod("setDisplayPowerMode", IBinder.class, int.class);
	try {
            method.invoke(null, getBuiltInDisplay(),Integer.parseInt(args[0]));
        } catch (InvocationTargetException | IllegalAccessException e) {
	    e.printStackTrace();
        }
    }

    private static Method getGetBuiltInDisplayMethod() throws NoSuchMethodException {
       Method getBuiltInDisplayMethod;
            // the method signature has changed in Android Q
            // <https://github.com/Genymobile/scrcpy/issues/586>
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                getBuiltInDisplayMethod = CLASS.getMethod("getBuiltInDisplay", int.class);
            } else {
                getBuiltInDisplayMethod = CLASS.getMethod("getInternalDisplayToken");
            }
        return getBuiltInDisplayMethod;
    }

    public static IBinder getBuiltInDisplay() {
        try {
            Method method = getGetBuiltInDisplayMethod();
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                // call getBuiltInDisplay(0)
                return (IBinder) method.invoke(null, 0);
            }
            // call getInternalDisplayToken()
            return (IBinder) method.invoke(null);
           } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Then press CTRL+X+Y+ENTER and save it.
然后按 CTRL+X+Y+ENTER 并保存。

And then create its Java CLASS file:-
然后创建它的 Java CLASS 文件:-

javac -Xlint:none -source 1.7 -target 1.7 -cp android.jar DisplayToggle.java

Then DEX it using:-
然后使用以下方法对其进行 DEX:-

dx --dex --output DisplayToggle.dex DisplayToggle.class

You have compiled the DisplayToggle.dex file (around 2.5kb).
您已经编译了 DisplayToggle.dex 文件(大约 2.5kb)。

Now copy it to internal storage to use it later.
现在将其复制到内部存储以供以后使用。

termux-setup-storage
cp -f DisplayToggle.dex /storage/emulated/0

How To Use It

如何使用它

In an ADB Shell (from Termux or PC):-
在 ADB Shell 中(来自 Termux 或 PC):-

To Turn Display OFF

关闭显示

adb shell CLASSPATH=/storage/emulated/0/DisplayToggle.dex app_process / DisplayToggle 0

To Turn Display ON

打开显示

adb shell CLASSPATH=/storage/emulated/0/DisplayToggle.dex app_process / DisplayToggle 2

Note:- 笔记:-

In Termux if you are rooted, you can just replace every adb shell with su -c.
在 Termux 中,如果您已获得 root 权限,则只需将每个 adb shell 替换为 su -c 即可。

2 个赞

我也在小众提问过

音媒播放器本身是可以熄屏工作的,目前想不出仍有哪些需要熄屏工作。

能否指定app熄屏工作?

  1. 有些影视APP(当然是指那些免费观看影视的APP啦),缓存的时候黑屏是不下载的
  2. 如果盲人使用是没有必要亮屏的。
  3. 连接电脑并投屏使用使用时。
  4. 游戏挂机。
  5. 开车导航只听声音?
  6. Youtube 音乐息屏听音乐。

firefox可以做到。

这个功能你可以配合tasker来实现, 比如打开微信时执行DisplayToggle 0 关闭屏幕

至于其它用途, 比较适用于一些服务类的应用, 需要一直开着的, 毕竟安卓机的休眠机制会导致休眠后服务停止运行

像我就挂了青龙面板

有现成的app,adb或者shizuku授权即可

2 个赞