电量百分比实现

原创 2015年11月18日 16:05:54

packages/apps/Settings/src/com/android/settings/fuelgauge/PowerUsageSummary.java中实现checkbox UI及数据值设置

//add by fangbo.xiong for Task 901010 Battery percentage bgn
import android.app.ActivityManagerNative;
import android.preference.CheckBoxPreference;
import android.provider.Settings;
//add by fangbo.xiong for Task 901010 Battery percentage end

public class PowerUsageSummary extends PreferenceFragment {
    ...    
    //add by fangbo.xiong for Task 901010 Battery percentage bgn
    private static final String KEY_BATTERY_STATUS_PERCENTAGE = "battery_status_percentage";
    ...
    //add by fangbo.xiong for Task 901010 Battery percentage bgn
    private CheckBoxPreference mBatteryStatusPercentagePref;
    //add by fangbo.xiong for Task 901010 Battery percentage end
    ...
    public void onCreate(Bundle icicle) {
        //add by fangbo.xiong for Task 901010 Battery percentage bgn
        mBatteryStatusPercentagePref = (CheckBoxPreference)((PreferenceScreen)mAppListGroup)
            .findPreference(KEY_BATTERY_STATUS_PERCENTAGE);
        mBatteryStatusPercentagePref.setEnabled(true);
        //add by fangbo.xiong for Task 901010 Battery percentage end
    }
    ...

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        //add by fangbo.xiong for Task 901010 Battery percentage bgn
        if (preference instanceof CheckBoxPreference && preference.getKey().equals(KEY_BATTERY_STATUS_PERCENTAGE)) {
            // save the value
            boolean isShowBatteryPercentage = Settings.System.getInt(this.getActivity()
                    .getContentResolver(), "show_statusbar_battery_percentage", 0) == 1;
            mBatteryStatusPercentagePref.setChecked(!isShowBatteryPercentage);
            int show_or_not = 0;
            // !isShowBatteryPercentage; isShowBatteryPercentage true,next value is false ,means 0;
            if (isShowBatteryPercentage) {
                show_or_not = 0;
            } else {
                show_or_not = 1;
            }
            Settings.System.putInt(this.getActivity().getContentResolver(),
                    "show_statusbar_battery_percentage", show_or_not);

            return super.onPreferenceTreeClick(preferenceScreen, preference);
        }
    }
    ...
    private void refreshStats() {

        //add by fangbo.xiong for Task 901010 Battery percentage bgn
        mBatteryStatusPercentagePref.setOrder(-2);
        mAppListGroup.addPreference(mBatteryStatusPercentagePref);
        boolean isShowBatteryPercentage = Settings.System.getInt(this.getActivity().getContentResolver(),
                "show_statusbar_battery_percentage", 0) == 1;
        mBatteryStatusPercentagePref.setChecked(isShowBatteryPercentage);
        //add by fangbo.xiong for Task 901010 Battery percentage end
    }
    ...
}

packages/apps/Settings/res/xml/power_usage_summary.xml 中定义UI元素

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
        android:title="@string/power_usage_summary_title"
        settings:keywords="@string/keywords_battery"
        android:key="app_list">
    <!--add by fangbo.xiong for Task 901010 Battery percentage bgn -->
    <CheckBoxPreference
        android:key="battery_status_percentage"
        android:persistent="false"
        android:title="@string/battery_status_percentage"/>
    <!--add by fangbo.xiong for Task 901010 Battery percentage end -->
</PreferenceScreen>


frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java


    private void loadSystemSettings(SQLiteDatabase db) {

        SQLiteStatement stmt = null;

            //add by fangbo.xiong for Task 901010 Battery percentage bgn
            loadBooleanSetting(stmt, "show_statusbar_battery_percentage", 
                        R.bool.def_SettingsProvider_battery_percentage);
            //add by fangbo.xiong for Task 901010 Battery percentage end
    }


frameworks/base/packages/SettingsProvider/res/values/defaults.xml

    <!-- add by fangbo.xiong for Task 901010 Battery percentage bgn-->
    <bool name="def_SettingsProvider_battery_percentage">false</bool>
    <!-- add by fangbo.xiong for Task 901010 Battery percentage end-->

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardStatusBarView.java
public class KeyguardStatusBarView extends RelativeLayout
        implements BatteryController.BatteryStateChangeCallback {
    @Override
    protected void onFinishInflate() {
        //add by fangbo.xiong for Task 901010 Battery percentage bgn
        boolean isShowBatteryPercentage = Settings.System.getInt(getContext()
                    .getContentResolver(), "show_statusbar_battery_percentage", 0) == 1;
        updateBatteryLevelVisibilities(isShowBatteryPercentage);
        //add by fangbo.xiong for Task 901010 Battery percentage end

    }

    private void updateVisibilities() {

        //add by fangbo.xiong for Task 901010 Battery percentage bgn
        //mBatteryLevel.setVisibility((mBatteryCharging ? View.VISIBLE : View.GONE);
        boolean isShowBatteryPercentage = Settings.System.getInt(getContext()
                    .getContentResolver(), "show_statusbar_battery_percentage", 0) == 1;
        updateBatteryLevelVisibilities(isShowBatteryPercentage);
        //add by fangbo.xiong for Task 901010 Battery percentage end

    }
    //add by fangbo.xiong for Task 901010 Battery percentage bgn
    public void updateBatteryLevelVisibilities(boolean aIsShowBatteryPercentage) {
        mBatteryLevel.setVisibility((mBatteryCharging || aIsShowBatteryPercentage) ? View.VISIBLE : View.GONE);
    }
    //add by fangbo.xiong for Task 901010 Battery percentage end
} 

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java


//add by fangbo.xiong for Task 901010 Battery percentage bgn
import android.provider.Settings;
//add by fangbo.xiong for Task 901010 Battery percentage end

public class PhoneStatusBar{
    //add by fangbo.xiong for Task 901010 Battery percentage bgn
    private static final String SHOW_BATTERY_PERCENTAGE = "show_statusbar_battery_percentage";

    TextView mBatterPercentageText;

    private final ContentObserver mBatteryPercentageObserver = new ContentObserver(mHandler) {
        @Override
        public void onChange(boolean selfChange) {
            updateBatteryLevelVisibilities();
        }
    };
    //add by fangbo.xiong for Task 901010 Battery percentage end

        //add by fangbo.xiong for Task 901010 Battery percentage bgn
        mBatterPercentageText = (TextView) mStatusBarView.findViewById(R.id.battery_level);
        boolean isShowBatteryPercentage = Settings.System.getInt(mContext.getContentResolver(),
                SHOW_BATTERY_PERCENTAGE, 0) == 1;
        if (isShowBatteryPercentage) {
            mBatterPercentageText.setVisibility(View.VISIBLE);
        } else {
            mBatterPercentageText.setVisibility(View.GONE);
        }
        mContext.getContentResolver().registerContentObserver(Settings.System.getUriFor(
                SHOW_BATTERY_PERCENTAGE),
                false, 
                mBatteryPercentageObserver, 
                UserHandle.USER_ALL);
        //add by fangbo.xiong for Task 901010 Battery percentage end

            @Override
            public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
                // noop
                //add by fangbo.xiong for Task 901010 Battery percentage bgn
                String percentage = java.text.NumberFormat.getPercentInstance().format((double) level / 100.0);
                mBatterPercentageText.setText(percentage);
                //add by fangbo.xiong for Task 901010 Battery percentage end

            }

     //add by fangbo.xiong for Task 901010 Battery percentage bgn
     private void updateBatteryLevelVisibilities() {
          if (null == mBatterPercentageText) {
                return ;
          }
          boolean isShowBatteryPercentage = Settings.System.getInt(
                                mContext.getContentResolver(), SHOW_BATTERY_PERCENTAGE, 0) == 1;
          if (isShowBatteryPercentage) {
                mBatterPercentageText.setVisibility(View.VISIBLE);
          } else {
                mBatterPercentageText.setVisibility(View.GONE);
          }
          mKeyguardStatusBar.updateBatteryLevelVisibilities(isShowBatteryPercentage);
     }
     //add by fangbo.xiong for Task 901010 Battery percentage end

}



Android 怎样控制电量百分比是否显示

我的android版本是5.1。 我是这样去实现的: 首先-----》我在priv-app/Settings里面的battery里面加一条SwitchPreference,这个是是否显示的开关。 ...
  • qinlei420
  • qinlei420
  • 2015年12月03日 17:34
  • 1452

L版本状态栏添加电量百分比

关于状态栏中添加电量百分比。 1, 添加view status_bar.xml  android:layout_width="wrap_content" android:layout_hei...
  • zhang_jun_xiang
  • zhang_jun_xiang
  • 2015年12月10日 09:45
  • 615

删除小电池图标及百分比

删除小电池图标及百分比 a、SystemUI/.../statusbar/policy/BatteryController.java mBatteryPercentageView.setVisib...
  • cnlihaijun
  • cnlihaijun
  • 2017年03月24日 09:49
  • 987

Android4.3状态栏增加电量百分比显示功能

涉及到的源码: BatteryController.java
  • fly_qj
  • fly_qj
  • 2014年09月28日 18:05
  • 6575

状态栏电量百分比默认关闭

1 . PowerUsageSummary.java onCreate方法     @Override     public void onCreate(Bundle icicle) { ...
  • liwenyang11111111111
  • liwenyang11111111111
  • 2017年04月17日 10:55
  • 415

Android MTK 修改默认显示电池百分比

//SystemUI中的获取状态项 frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\policy\ Batte...
  • Mr_kings
  • Mr_kings
  • 2016年07月01日 16:26
  • 2056

android锁屏界面电量百分比显示与主桌面同步

路径 frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\phone\KeyguardStatusBarView....
  • qq_32143319
  • qq_32143319
  • 2017年04月21日 15:59
  • 677

andrid5.0添加开关默认控制电量百分比显示

android5.0添加开关默认选中不选中的问题
  • xiaoyaoli123456
  • xiaoyaoli123456
  • 2017年09月23日 11:40
  • 169

安卓7.0上在状态栏上加电池百分比

》》》》》》     安卓7.0上的statusbar上是默认不带电池百分比的。需要我们加上去。有两个方法,先写第一个方法    1. 在frameworks/base/packages/Sys...
  • xiangzaixiansheng
  • xiangzaixiansheng
  • 2017年09月22日 10:51
  • 617

android6.0 添加电量百分比

要完成这个功能需要修改SystemUI和Settings两部分 一、Settings添加开关——控制是否打开此功能 我选择把开关添加到display模块下diff --git a/pack...
  • u012382509
  • u012382509
  • 2016年11月10日 10:46
  • 1801
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:电量百分比实现
举报原因:
原因补充:

(最多只允许输入30个字)