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

Android Settings 显示电池点亮百分比

如题,Android 原生 Settings 里有个 电池电量百分比 的选项,打开后电池电量百分比会显示在状态栏。
在这里插入图片描述
基于 Android 13 ,
代码在 ./packages/apps/Settings/src/com/android/settings/display/BatteryPercentagePreferenceController.java

/** Copyright (C) 2016 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
package com.android.settings.display;import static android.provider.Settings.System.SHOW_BATTERY_PERCENT;import android.app.settings.SettingsEnums;
import android.content.Context;
import android.provider.Settings;import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;import com.android.internal.R;
import com.android.settings.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.overlay.FeatureFactory;/*** A controller to manage the switch for showing battery percentage in the status bar.*/public class BatteryPercentagePreferenceController extends BasePreferenceController implementsPreferenceControllerMixin, Preference.OnPreferenceChangeListener {private Preference mPreference;public BatteryPercentagePreferenceController(Context context, String preferenceKey) {super(context, preferenceKey);}@Overridepublic void displayPreference(PreferenceScreen screen) {super.displayPreference(screen);mPreference = screen.findPreference(getPreferenceKey());if (!Utils.isBatteryPresent(mContext)) {// Disable battery percentageonPreferenceChange(mPreference, false /* newValue */);}}@Overridepublic int getAvailabilityStatus() {if (!Utils.isBatteryPresent(mContext)) {return CONDITIONALLY_UNAVAILABLE;}return mContext.getResources().getBoolean(R.bool.config_battery_percentage_setting_available) ? AVAILABLE: UNSUPPORTED_ON_DEVICE;}@Overridepublic void updateState(Preference preference) {int setting = Settings.System.getInt(mContext.getContentResolver(),SHOW_BATTERY_PERCENT, 0);((SwitchPreference) preference).setChecked(setting == 1);}@Overridepublic boolean onPreferenceChange(Preference preference, Object newValue) {boolean showPercentage = (Boolean) newValue;Settings.System.putInt(mContext.getContentResolver(), SHOW_BATTERY_PERCENT,showPercentage ? 1 : 0);FeatureFactory.getFactory(mContext).getMetricsFeatureProvider().action(mContext, SettingsEnums.OPEN_BATTERY_PERCENTAGE, showPercentage);return true;}
}

调整的关键代码是,

Settings.System.putInt(mContext.getContentResolver(), SHOW_BATTERY_PERCENT,showPercentage ? 1 : 0);

SHOW_BATTERY_PERCENT 定义在 ./frameworks/base/core/java/android/provider/Settings.java

public static final String SHOW_BATTERY_PERCENT = "status_bar_show_battery_percent";

adb 调试方法,
显示,

settings put system status_bar_show_battery_percent 1

不显示

settings put system status_bar_show_battery_percent 0

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

相关文章:

  • Windows记事本不显示下划线的原因及解决方法
  • 嵌入式软件工程师面试题——2025校招社招通用(C/C++)(四十六)
  • 【学网攻】 第(13)节 -- 动态路由(OSPF)
  • Asp.Net Core 获取应用程序相关目录
  • 文献速递:人工智能医学影像分割--- 深度学习分割骨盆骨骼:大规模CT数据集和基线模型
  • PaddleNLP的简单使用
  • 2. MySQL 多实例
  • 两个五层决策树和一个十层决策树的区别
  • 案例分析技巧-软件工程
  • 如何使用docker compose安装APITable并远程访问登录界面
  • 深入了解Matplotlib中的子图创建方法
  • 云计算运维 · 第三阶段 · git
  • 【幻兽帕鲁】开服务器,高性能高带宽(100mbps),免费!!!【学生党强推】
  • 微信小程序|推箱子小游戏
  • 【Linux】—— 信号的产生
  • 【算法】Hash 算法-关注优化细节
  • 回归预测 | Matlab实现CPO-SVR冠豪猪优化支持向量机的数据多输入单输出回归预测
  • Idea设置代理后无法clone git项目
  • tkMapper 通用mapper的批量更新 批量新增 官方实现 springboot项目 依赖引入
  • 【leetcode刷刷】回溯:77.组合
  • 【OOP】Python的OOP编程笔记
  • 一进一出模拟量信号隔离变送器
  • Mybatis-plus原生pages分页未生效的解决方案
  • 【linux】-centos7版本前后-变化篇
  • 001集—shapefile(.shp)格式详解——arcgis
  • ssrf服务器请求伪造漏洞(个人学习)
  • 【前端web入门第二天】03 表单-下拉菜单 文本域 label标签 按钮 【附注册信息综合案例】
  • 回响科技二面面试题解答
  • node学习过程中的终端命令
  • oracle版本号中的i,G,C代表什么含义