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 WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

Android如何获取当前CPU频率和占用率

《Android如何获取当前CPU频率和占用率》最近在优化App的性能,需要获取当前CPU视频频率和占用率,所以本文小编就来和大家总结一下如何在Android中获取当前CPU频率和占用率吧... 最近在优化 App 的性能,需要获取当前 CPU视频频率和占用率,通过查询资料,大致思路如下:目前没有标准的

Spring Security注解方式权限控制过程

《SpringSecurity注解方式权限控制过程》:本文主要介绍SpringSecurity注解方式权限控制过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、摘要二、实现步骤2.1 在配置类中添加权限注解的支持2.2 创建Controller类2.3 Us

Python中如何控制小数点精度与对齐方式

《Python中如何控制小数点精度与对齐方式》在Python编程中,数据输出格式化是一个常见的需求,尤其是在涉及到小数点精度和对齐方式时,下面小编就来为大家介绍一下如何在Python中实现这些功能吧... 目录一、控制小数点精度1. 使用 round() 函数2. 使用字符串格式化二、控制对齐方式1. 使用

Springboot控制反转与Bean对象的方法

《Springboot控制反转与Bean对象的方法》文章介绍了SpringBoot中的控制反转(IoC)概念,描述了IoC容器如何管理Bean的生命周期和依赖关系,它详细讲解了Bean的注册过程,包括... 目录1 控制反转1.1 什么是控制反转1.2 SpringBoot中的控制反转2 Ioc容器对Bea

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤