android关闭背光
android关闭背光
- Android11
- 方法1(可能关不完还是会有点亮):
- 方法2(这个涉及系统权限):
- 2.1调试:
- 2.2处理问题:
- 2.2.1 处理核心代码
- 2.2.2 验证 遇到问题
- 2.2.3 验证 遇到问题检查要操作文件得权限:
- 2.2.4 检查Slinux权限
- 2.3 解决问题
- 2.3.1 文件权限得处理
- 2.3.2 Slinux权限处理
- 2.3.2 .1 查看缺少什么权限
- 2.3.2 .2 缺什么权限添加什么权限
- 2.3.2 .3.1 解决Neverallow(会引起失败项,如果有这方面限制得直接跳转 2.3.2 .3.2)
- 2.3.2 .3.2 解决Neverallow,并添加自定义type(不会引起失败项)
- 2.3.2 .4 查看文件是否为快捷
- 2.3.3 这样修改后同时也导致了一个新的问题(状态栏亮度条,调节失效)
Android11
方法1(可能关不完还是会有点亮):
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;try{int brightness_val = Settings.System.getInt(context.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS);}catch(SettingNotFoundException e){e.printStackTrace();}
按照这个方案,设置屏幕亮度,系统亮度只会变暗,并不会完全关闭背光,所以就有了方案2
方法2(这个涉及系统权限):
2.1调试:
adb root;
adb remount;
adb shell
echo 0 > /sys/class/backlight/***/brightness 屏幕亮度完全关闭这就是我们需要得功能
2.2处理问题:
到此测试完成后,拿到这个功能后我脑子就闪过这是一个文件,既然文件,内容修改,那一定就能想到File流处理(现在还不确定是否有效):
2.2.1 处理核心代码
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import java.io.PrintStream;
import java.io.FileNotFoundException;
import android.os.Handler;private static final String PRODUCTINFO_DIR = "/sys/class/backlight/***";private File BOOT_VERSION_FILE = new File(PRODUCTINFO_DIR, "brightness");/*核心方法 start*/private boolean modifyFile(int string,String path) {PrintStream stream=null;try {stream=new PrintStream(path);stream.print(string);android.util.Log.d(TAG,"Write brightness success!");return true;} catch (FileNotFoundException e) {android.util.Log.d(TAG,"Write brightness faile!");e.printStackTrace();} finally {Log.d(TAG, "Write brightness close.");stream.close();}return false;}/*核心方法 end*//* 调用 start*/int brightness_val =0;//获取当前亮度,try{brightness_val= Settings.System.getInt(mContext.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS);}catch(SettingNotFoundException e){e.printStackTrace();}final int brightness_val_put = brightness_val;//临时关闭背光亮度,为全灭modifyFile(0,"/sys/devices/platform/***/backlight/***/brightness");//500ms 后屏幕亮起new Handler().postDelayed(new Runnable() {public void run(){//将临时亮度还原modifyFile(brightness_val_put,"/sys/class/backlight/***/brightness");}}, 500);/* 调用 end*/
2.2.2 验证 遇到问题
编译后push 进手机验证,没有任何效果。打印log查看一下
adb logcat -v time
08-13 09:32:51.911 W/System.err(15160): java.io.FileNotFoundException: /sys/class/backlight/***/brightness: open failed: EACCES (Permission denied)
08-13 09:32:51.911 W/System.err(15160): at libcore.io.IoBridge.open(IoBridge.java:492)
08-13 09:32:51.912 W/System.err(15160): at java.io.FileOutputStream.<init>(FileOutputStream.java:236)
08-13 09:32:51.912 W/System.err(15160): at java.io.FileOutputStream.<init>(FileOutputStream.java:125)
08-13 09:32:51.912 W/System.err(15160): at java.io.PrintStream.<init>(PrintStream.java:215)
08-13 09:32:51.915 W/System.err(15160): Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
08-13 09:32:51.915 W/System.err(15160): at libcore.io.Linux.open(Native Method)
08-13 09:32:51.915 W/System.err(15160): at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
08-13 09:32:51.915 W/System.err(15160): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
08-13 09:32:51.915 W/System.err(15160): at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
08-13 09:32:51.916 W/System.err(15160): at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7806)
08-13 09:32:51.916 W/System.err(15160): at libcore.io.IoBridge.open(IoBridge.java:478)
怎么办呢,去网上搜索了一番说是缺少某些权限(进入源码里查看,以下权限都添加了,并不是这个得原因,还是记录一下吧万一以后遇到呢)
检查是否在 AndroidManifest.xml 文件中添加了以下两个权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
如果用的Android 10.0及以上的Android设备,请动态申请权限,并在 AndroidManifest.xml中application标签内加上android:requestLegacyExternalStorage="true"
<application...android:requestLegacyExternalStorage="true"...>...
</application>
2.2.3 验证 遇到问题检查要操作文件得权限:
adb root;
adb remount;
adb shell
ls -lZ >/sys/class/backlight/***/brightness
-rw-rw-r-- 1 system system u:object_r:sysfs:s0 4096 2021-08-13 10:13 /sys/class/backlight/***/brightness
0664(-rw-rw-r–),权限不够?那我们尝试将此文件的权限更改一下,执行第5步,
5. chmod 666 /sys/class/backlight/***/brightness
验证一下,还是没有效果。
2.2.4 检查Slinux权限
adb root;
adb remount;
adb shell setenforce 0;
再次尝试,验证ok,
2.3 解决问题
经过测试,与文件的权限和Slinux均有关系。
2.3.1 文件权限得处理
grep -nr “/sys/class/backlight/xxx/brightness”
device\xxx\mpool\module\display\main.rc
找到如下代码:
chmod 0664 /sys/class/backlight/***/brightness
将其改为:
chmod 666 /sys/class/backlight/xxx/brightness
编译进手机查看文件权限处理,是否生效
adb root;
adb remount;
adb shell
ls -lZ >/sys/class/backlight/xxx/brightness
-rw-rw-rw-- 1 system system u:object_r:sysfs:s0 4096 2021-08-13 10:13 /sys/class/backlight/xxx/brightness
文件权限,已解决,
2.3.2 Slinux权限处理
2.3.2 .1 查看缺少什么权限
adb root;
adb remount;
adb shell setenforce 0;
adb shell cat /proc/kmsg | grep avc 得到以下log
<38>[11151.737068] c0 type=1400 audit(1628831973.735:4146): avc: denied { write } for comm="进程名" name="brightness" dev="sysfs" ino=24255 scontext=u:r:system_app:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=1
<38>[11151.737427] c0 type=1400 audit(1628831973.735:4147): avc: denied { open } for comm="进程名" path="/sys/class/backlight/xxx/brightness" dev="sysfs" ino=24255 scontext=u:r:system_app:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=1
<38>[11151.739510] c0 type=1400 audit(1628831973.735:4148): avc: denied { getattr } for comm="进程名" path="/sys/class/backlight/xxx/brightness" dev="sysfs" ino=24255 scontext=u:r:system_app:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=1
2.3.2 .2 缺什么权限添加什么权限
device\xxx\mpool\module\app\msepolicy\vendor\system_app.te
添加如下代码:
allow system_app sysfs:file { write open getattr };
编译,得时候报错,报错信息如下:
libsepol.report_failure: neverallow on line 97 of system/sepolicy/private/coredomain.te (or line 36549 of policy.conf) violated by allow system_app sysfs:file { write };
libsepol.report_failure: neverallow on line 514 of system/sepolicy/public/app.te (or line 9969 of policy.conf) violated by allow system_app sysfs:file { write };
libsepol.check_assertions: 2 neverallow failures occurred
Error while expanding policy
原因是 Google 不允许应用进程写 sysfs 类型的文件
2.3.2 .3.1 解决Neverallow(会引起失败项,如果有这方面限制得直接跳转 2.3.2 .3.2)
system/sepolicy/public/app.te
system/sepolicy/prebuilts/api/28.0/public/app.te
(这两个文件都需要修改内容必须保持一致否则也会编译不过)
Write to various pseudo file systems.
--neverallow { appdomain -bluetooth -nfc }
++neverallow { appdomain -bluetooth -nfc -system_app}sysfs:dir_file_class_set write;
到此为止整个方案结束,如果担心失败项的继续往下看!!!!!
2.3.2 .3.2 解决Neverallow,并添加自定义type(不会引起失败项)
device\sprd\mpool\sepolicy\system\public\file.te
sysfs_xxx(可以自定义)
++type sysfs_xxx, fs_type,sysfs_type;
device\sprd\mpool\sepolicy\system\private\file_contexts
++ /sys/class/backlight/xxx/brightness u:object_r:sysfs_xxx:s0
device\xxx\mpool\module\app\msepolicy\vendor\system_app.te
-- allow system_app sysfs:file { write open getattr };
++ allow system_app sysfs_xxx:file { write open getattr};
编译验证,还是没有效果
2.3.2 .4 查看文件是否为快捷
执行 2.3.2 .1 操作,log如下:
<36>[ 1019.507778] c3 type=1400 audit(1628852975.491:879): avc: denied { write } for comm="xxx.xxx" name="brightness" dev="sysfs" ino=24255 scontext=u:r:platform_app:s0:c512,c768 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0 app=xxx.xxx.xxx
<36>[ 57.933396] c1 type=1400 audit(1628852013.903:148): avc: denied { open } for comm="xxx.xxx" name="brightness" dev="sysfs" ino=24255 scontext=u:r:platform_app:s0:c512,c768 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0 app=xxx.xxx.xxx
<38>[ 92.602822] c1 type=1400 audit(1628843243.579:316): avc: denied { getattr } for comm="xxx.xxx" name="brightness" dev="sysfs" ino=24255 scontext=u:r:platform_app:s0:c512,c768 tcontext=u:object_r:sysfs:s0 tclass=file permissive=1 app=xxx.xxx.xxx
在 device\xxx\mpool\module\app\msepolicy\vendor\platform_app.te 添加
++ allow platform_app sysfs_xxx:file { write open getattr};
编译还是无效,不是说缺什么权限加什么权限吗?怎么会没效果呢。
通过adb命令进入sys/class/leds这个目录下用ls -l发现
/sys/class/backlight/只是devices/platform/xxx/backlight/xxxx创建得软连接
130|A70:/ # ls -l /sys/class/backlight/
total 0
lrwxrwxrwx 1 root root 0 2021-08-14 20:27 xxx-> ../../devices/platform/xxx/backlight/xxxx
修改以下代码:
device\xxx\mpool\sepolicy\system\private\file_contexts
-- /sys/class/backlight/xxx/brightness u:object_r:sysfs_xxx:s0
++/sys/devices/platform/xxx/backlight/xxx/brightness u:object_r:sysfs_xxx:s0
编译验证ok。结束!!!!!!!!!!!!!!!!!!!!!!
2.3.3 这样修改后同时也导致了一个新的问题(状态栏亮度条,调节失效)
调节状态栏亮度条,打印avc log抛出一下异常
<36>[ 188.094015] c0 type=1400 audit(1629299395.023:344): avc: denied { read write open getattr } for comm="进程名" path="/sys/devices/platform/xxx/backlight/xxx/brightness" dev="sysfs" ino=23184 scontext=u:r:hal_light_default:s0 tcontext=u:object_r:sysfs_xxx:s0 tclass=file permissive=0
根据缺什么权限就加什么权限的原则
device\xxx\mpool\module\light\msepolicy\vendor\hal_light_default.te
allow hal_combined_default sysfs_xxx:file { read write open getattr };
编译验证还是没反应重上面操作,调节状态栏亮度条,打印avc log还是抛出异常:
<36>[ 188.094015] c0 type=1400 audit(1629299395.023:344): avc: denied { read write open getattr } for comm="进程名" path="/sys/devices/platform/xxx/backlight/xxx/brightness" dev="sysfs" ino=23184 scontext=u:r:hal_combined_default:s0 tcontext=u:object_r:sysfs_xxx:s0 tclass=file permissive=0
\device\xxx\mpool\sepolicy\vendor\hal_combined_default .te
allow hal_combined_default sysfs_demo:file { read write open getattr };
验证,ok
结束!!!!!!!!!!!!!!!!!!!
android关闭背光相关推荐
- android lcd 背光控制流程
此文章是网上看到的,在MTK平台基础上的背光控制流程的分析.个人觉得写得比较详细,于是截取部分内容转载出来. 不过话说在前头,对于lcd而言,决定显示效果的有几个因素:pwm,gama(屏幕灰度),饱 ...
- android关闭应用及获取运营商ISP,判断sim卡属于哪个运营商
android关闭应用及获取运营商ISP 1.AndroidManifest.xml <?xml version="1.0" encoding="utf-8&quo ...
- Android 关闭屏幕方法
Android关闭屏幕管理类: mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_ ...
- Android关闭屏幕时不锁屏
Android关闭屏幕时不锁屏 权限 <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/ ...
- android关闭或开启移动网络数据(关闭后,设备不可以上网,但可以打电话和发短信)...
//android关闭或开启移动网络数据(关闭后,设备不可以上网,但可以打电话和发短信) public void setMobileDataEnabled(boolean enabled) { Con ...
- android的背光调节
android的背光调节是利用pwm波形进行调节的,源码在 drivers\video\backlight\pwm_bl.c
- Android中背光系统架构
Mg Android中背光系统架构 Email:wei7758@126.com Blog:http://blog.csdn.net/yinwei520 Author:Yww Time:2011-9-2 ...
- android关闭屏幕背光,Android 的背光控制
echo 23 > /sys/devices/platform/atmel_lcdfb.0/backlight/backlight/brightness http://android.git.k ...
- android 背光控制,Android P背光策略分析(一)
本文主要讲述android 9.0系统对背光的处理,文章分为三点讲述: 1.背光的初始化 2.手动背光调节 3.自动背光调节 一.初始化 1.1 服务的启动(DisplayManagerService ...
最新文章
- 用JTAG将mcs文件烧写到flash中
- C语言经典例35-字符串反转
- 转载 为什么要有ibgp
- Sklearn,TensorFlow,keras模型保存与读取
- pytorch切片,numpy切片的总结,以及数组切片常用操作的总结
- 《阿里巴巴Android开发手册》正式发布,献给移动开发者的新年礼物
- uva 3n+1 基础题
- 应用随机过程张波商豪_学术简报五相逆变器非正弦双随机空间矢量脉宽调制策略...
- java学习——线程
- SAXParseException An invalid XML character 问题的解决
- 13-微信小程序商城 产品简介布局(微信小程序商城开发、小程序毕业设计、小程序源代码)(黄菊华-微信小程序开发教程)
- GPS精确授时方法研究-基于ublox GPS
- 【笔记】项目工作中总结(三)
- 电信移动信号测试软件,移动、联通、电信(信号强度大比拼)
- 学校计算机机房解说词,学校会议室解说词.doc
- java pdf转图片原理_pdf转图片程序(java实现)
- 让模糊图片变清晰:滴滴云推出图像超分辨API,新用户免费调用100次
- 好玩的ES--第三篇之过滤查询,整合SpringBoot
- Batch Normalization (BN层)-----批归一化
- 2G和3G网络制式上下行频率、速率比较