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

2023-11-30 03:36

本文主要是介绍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

按键灯的权限问题:两个RC文件

1,/device/mediatek/mt67xx/init.mt6761.rc

on boot 下:

chmod 0664 /sys/class/leds/button-backlight/brightness
  chown system system /sys/class/leds/button-backlight/brightness

 2,/device/droi/项目名/init.project.rc

on post-fs-data下:

# button light
    chmod 0666 sys/class/leds/button-backlight/brightness
    chown system system sys/class/leds/button-backlight/brightness

 其实其他什么状态灯、扫码灯皆如此,只是节点名称不同。

这篇关于Android:控制按键灯亮灭【button-backlight】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/435468

相关文章

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Spring Security+JWT如何实现前后端分离权限控制

《SpringSecurity+JWT如何实现前后端分离权限控制》本篇将手把手教你用SpringSecurity+JWT搭建一套完整的登录认证与权限控制体系,具有很好的参考价值,希望对大家... 目录Spring Security+JWT实现前后端分离权限控制实战一、为什么要用 JWT?二、JWT 基本结构

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键