Android13默认开启电池百分比数字显示Framework

有个小需求,需要实现。

查了很多平台,就是没有Android13的。

通过查阅Framework代码。终于找到了如何默认显示。

QSSI.13/frameworks/base/packages/SystemUI/src/com/android/systemui/battery/BatteryMeterView.java 

 void updateShowPercent() {
        final boolean showing = mBatteryPercentView != null;
        // TODO(b/140051051)
        final boolean systemSetting = 0 != whitelistIpcs(() -> Settings.System
                .getIntForUser(getContext().getContentResolver(),
                        SHOW_BATTERY_PERCENT, 1, UserHandle.USER_CURRENT)); //修改 0 为不显示数字, 1 为显示数字。
        boolean shouldShow = (mShowPercentAvailable && systemSetting && mShowPercentMode != MODE_OFF)
                || mShowPercentMode == MODE_ON
                || mShowPercentMode == MODE_ESTIMATE;
        shouldShow = shouldShow && !mBatteryStateUnknown;

        if (shouldShow) {
            if (!showing) {
                mBatteryPercentView = loadPercentView();
                if (mPercentageStyleId != 0) { // Only set if specified as attribute
                    mBatteryPercentView.setTextAppearance(mPercentageStyleId);
                }
                if (mTextColor != 0)
                    mBatteryPercentView.setTextColor(mTextColor);
                updatePercentText();
                addView(mBatteryPercentView,
                        new ViewGroup.LayoutParams(
                                LayoutParams.WRAP_CONTENT,
                                LayoutParams.MATCH_PARENT));
            }
        } else {
            if (showing) {
                removeView(mBatteryPercentView);
                mBatteryPercentView = null;
            }
        }
    }

QSSI.13/packages/apps/Settings/src/com/android/settings/display/BatteryPercentagePreferenceController.java

  @Override
    public void updateState(Preference preference) {
        int setting = Settings.System.getInt(mContext.getContentResolver(),
                SHOW_BATTERY_PERCENT, 1);  // 0 为不显示数字, 1为显示数字

        ((SwitchPreference) preference).setChecked(setting == 1);
    }

修改2个地方,一个是Setting,一个是SystemUI显示。

通过,0,1 的切换,可以修改默认显示电池数字百分比,或不显示电池数字百分比的效果。

你可能感兴趣的:(java,开发语言,Android,Android13,Framework)