当前位置: 首页 > news >正文

Android:控制按键灯亮灭【button-backlight】

/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

1.导包
import java.io.DataOutputStream;
import java.io.FileOutputStream;

Handler mHandler3;

2.新建handler对象

public void init(Context context, IWindowManager windowManager,
            WindowManagerFuncs windowManagerFuncs) {

 mHandler3 = new Handler();

3.延时处理方法

public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {


        // Basic policy based on interactive state.
        int result;
        boolean isWakeKey = (policyFlags & WindowManagerPolicy.FLAG_WAKE) != 0
                || event.isWakeKey();
        //*/ 20210513. for backlight control
        if(interactive) {
            writeFile("/sys/class/leds/button-backlight/brightness", "1");
            mHandler3.removeMessages(0);
            mHandler3.postDelayed(new Runnable() {
                public void run() {
                    writeFile("/sys/class/leds/button-backlight/brightness", "0");
                }
            }, 5 * 1000);
        }
        //*/

        if (interactive || (isInjected && !isWakeKey)) {
            // When the device is interactive or the key is injected pass the
            // key to the application.
            result = ACTION_PASS_TO_USER;
            isWakeKey = false;

            if (interactive) {
                // If the screen is awake, but the button pressed was the one that woke the device
                // then don't pass it to the application
                if (keyCode == mPendingWakeKey && !down) {
                    result = 0;
                }
                // Reset the pending key
                mPendingWakeKey = PENDING_KEY_NULL;
            }
        } else if (!interactive && shouldDispatchInputWhenNonInteractive(event)) {
            // If we're currently dozing with the screen on and the keyguard showing, pass the key
            // to the application but preserve its wake key status to make sure we still move
            // from dozing to fully interactive if we would normally go from off to fully
            // interactive.
            result = ACTION_PASS_TO_USER;
            // Since we're dispatching the input, reset the pending key
            mPendingWakeKey = PENDING_KEY_NULL;
        } else {
            // When the screen is off and the key is not injected, determine whether
            // to wake the device but don't pass the key to the application.
            result = 0;
            if (isWakeKey && (!down || !isWakeKeyWhenScreenOff(keyCode))) {
                isWakeKey = false;
            }
            // Cache the wake key on down event so we can also avoid sending the up event to the app
            if (isWakeKey && down) {
                mPendingWakeKey = keyCode;
            }
        }

4.屏灭时写0

 @Override
    public void screenTurningOff(ScreenOffListener screenOffListener) {
        mWindowManagerFuncs.screenTurningOff(screenOffListener);
        synchronized (mLock) {
            if (mKeyguardDelegate != null) {
                mKeyguardDelegate.onScreenTurningOff();
            }
        }
        //*/ 20231017 for backlight control
        writeFile("/sys/class/leds/button-backlight/brightness", "0");
        //*/

    }
 

5.写节点方法

//*/

private void writeFile(String filePath, String line) {
        File a = new File(filePath);
        if (a.exists()) {
            try {
                FileOutputStream fs = new FileOutputStream(a);
                DataOutputStream ds = new DataOutputStream(fs);
                ds.write(line.getBytes());
                ds.flush();
                ds.close();
                fs.close();
            } catch (Exception ex) {
                Log.e(TAG, "writeFile() Exception: " + filePath);
            }
        } else {
            Log.d(TAG, "writeFile() File not exist: " + filePath);
            try {
                if (a.createNewFile()) {
                    Log.d(TAG, "writeFile() File created: " + filePath);
                    try {
                        FileOutputStream fs = new FileOutputStream(a);
                        DataOutputStream ds = new DataOutputStream(fs);
                        ds.write(line.getBytes());
                        ds.flush();
                        ds.close();
                        fs.close();
                    } catch (Exception ex) {
                        Log.e(TAG, "writeFile() Exception: " + filePath);
                    }
                } else {
                    Log.d(TAG, "writeFile() Create file fail: " + filePath);
                }
            } catch (IOException e) {
                Log.e(TAG, "writeFile() creatFile Exception: " + filePath);
            }
        }
    }
    //*/

最后别忘了底层的支持 :

defconfig / debug_defconfig文件

CONFIG_BUTTON_BACKLIGHT_SUPPORT_GPIO=y

http://www.lryc.cn/news/246700.html

相关文章:

  • 1、nmap常用命令
  • Redis缓存设计典型问题
  • 【python】python基础速通系列2-python程序中的积木块
  • 本地开启https,配置nodeJs服务
  • 项目中的svg图标的封装与使用
  • 文件服务器迁移
  • 虹科Pico汽车示波器 | 汽车免拆检修 | 2011款瑞麒M1车发动机起动困难、加速无力
  • 深度学习之图像分类(十五)DINAT: Dilated Neighborhood Attention Transformer详解(一)
  • 和数集团出席中科院上海高研院​第三十三期“高研交叉论坛”信息能源融合专场
  • GitHub----使用记录
  • 大数据平台/大数据技术与原理-实验报告--实战HDFS
  • C#中的事件(委托的发布和订阅、事件的发布和订阅、EventHandler类、Windows事件)
  • 世微 舞台灯深度调光 大功率 dc-dc降压恒流驱动IC APS54083
  • 【nlp】3.3 Transformer论文复现:2. 编码器部分(掩码张量、多头注意力机制、前馈全连接层)
  • vue3中shallowReactive与shallowRef
  • 蓝桥杯-动态规划-子数组问题
  • CDA一级备考思维导图
  • 【傻瓜级JS-DLL-WINCC-PLC交互】1.C#用windows窗体控件创建.net控件
  • Unity中Shader的BRDF解析(一)
  • 《软件工程原理与实践》复习总结与习题——软件工程概述
  • acwing算法基础之动态规划--线性DP和区间DP
  • 力扣 622.设计循环队列
  • 初识Linux(2).妈妈再也不用担心我Linux找不到门了。
  • 房屋租赁出售经纪人入驻小程序平台
  • 【计算方法与科学建模】矩阵特征值与特征向量的计算(五):乘幂法的加速(带有原点移位的乘幂法)
  • 2023年【起重机械指挥】考试题库及起重机械指挥考试资料
  • GoLang语言范围(Range)
  • 汽车电子 -- 车载ADAS之FCW(前方碰撞预警)
  • 爬虫系统Docker和Kubernetes部署运维最佳实践
  • 音视频5、libavformat-1