1.系统状态栏添加电量百分比显示
我们要修改SystemUI这个应用,看过源码才知道,其实状态栏是分3个显示的,
锁屏的时候是一个,对应的类是KeyguardStatusBarView,它是一个RelativeLayout,
对应的布局是Keyguard_status_bar.xml
进入后是一个,对应的类是PhoneStatusBar,它是BaseStatusBar的子类
对应的布局是status_bar.xml.xml
下拉通知菜单的时候又是一个,对应的类是StatusBarHeaderView,它是一个RelativeLayout
它对应的布局是Status_bar_expanded_header.xml
要想监听到电量的变化,必须实现BatteryController.BatteryStateChangeCallback这个接口,
它有两个方法,其中onBatteryLevelChanged方法就是监听电量变化的
BatteryController是一个BroadcastReceiver,当电量变化时可以收到
PowerManager.ACTION_POWER_SAVE_MODE_CHANGED这个action,
这时就会走onBatteryLevelChanged方法,所有我们只要实现这个接口就可以监听到。
KeyguardStatusBarView,StatusBarHeaderView和PhoneStatusBar是有实现,
PhoneStatusBar对应的布局是status_bar.xml,在里面加一个textview在自定义电池图标的下面,
然后在onBatteryLevelChanged()方法中去实现电量显示
PhoneStatusBar中:
添加显示电量的textview
<include layout="@layout/system_icons" /> <TextView android:id="@+id/battery_level" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginStart="@dimen/header_battery_margin_expanded" android:paddingEnd="@dimen/battery_level_padding_end" android:textColor="#ffffff" android:textSize="@dimen/battery_level_text_size" android:importantForAccessibility="noHideDescendants"/> <com.android.systemui.statusbar.policy.Clock android:id="@+id/clock"
@Override public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) { String percentage = NumberFormat.getPercentInstance().format((double) level / 100.0); mBatteryLevel.setText(percentage); }
如果想要控制电量百分比的显示,只需要写一个ContentObserver做一个开关来监听改变来控制
在packages/apps/Settings/src/com/mediatek/settings/fuelgauge/PowerUsageExts.java 中添加switchpreference
// add by w for battery percentage
mBatteryPercentPrf = new SwitchPreference(mContext);
mBatteryPercentPrf.setKey(KEY_BATTERY_PERCENTAGE_ENABLE);
mBatteryPercentPrf.setTitle(R.string.battery_percentage_title);
mBatteryPercentPrf.setOrder(PREFERENCE_ORDER_FIRST);
mBatteryPercentPrf.setChecked(true);
mPowerUsageScreen.addPreference(mBatteryPercentPrf);
//add end
在public boolean onPowerUsageExtItemsClick()方法中添加
// add by w for battery percentage
if (KEY_BATTERY_PERCENTAGE_ENABLE.equals(preference.getKey())) {
if (preference instanceof SwitchPreference) {
SwitchPreference pref = (SwitchPreference) preference;
int bgState = pref.isChecked() ? 1 : 0;
Log.d(TAG, "battery percentage enable state: " + bgState);
Settings.System.putInt(mContext.getContentResolver(),
"BATTERY_PERCENTAGE_ENABLE", bgState);
Intent intent = new Intent(ACTION_BATTERY_PERCENTAGE_ENABLE);
intent.putExtra("state",bgState);
if (mBatteryPercentPrf != null) {
mBatteryPercentPrf.setChecked(pref.isChecked());
}
mContext.sendBroadcast(intent);
}
// If user click on PowerSaving preference just return here
return true;
}
//add end
在KeyguardStatusBarView和PhoneStatusBar中添加接收广播即可
在KeyguardStatusBarView中:
<FrameLayout android:id="@+id/system_icons_container" android:layout_width="wrap_content" android:layout_height="@dimen/status_bar_height" android:layout_gravity="center_vertical" > <include layout="@layout/system_icons" /> </FrameLayout> <TextView android:id="@+id/battery_level" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginStart="@dimen/header_battery_margin_keyguard" android:paddingEnd="@dimen/battery_level_padding_end" android:textColor="#ffffff" android:visibility="gone" android:textSize="@dimen/battery_level_text_size" android:importantForAccessibility="noHideDescendants"/>
@Override public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) { String percentage = NumberFormat.getPercentInstance().format((double) level / 100.0); mBatteryLevel.setText(percentage); boolean changed = mBatteryCharging != charging; mBatteryCharging = charging; if (changed) { updateVisibilities(); } }
private void updateVisibilities() { if (mMultiUserSwitch.getParent() != this && !mKeyguardUserSwitcherShowing) { if (mMultiUserSwitch.getParent() != null) { getOverlay().remove(mMultiUserSwitch); } addView(mMultiUserSwitch, 0); } else if (mMultiUserSwitch.getParent() == this && mKeyguardUserSwitcherShowing) { removeView(mMultiUserSwitch); } mBatteryLevel.setVisibility(mBatteryCharging ? View.VISIBLE : View.GONE); }可以看到,在KeyguardStatusBarView中加了一个限制条件控制电量百分比显示,在充电的时候才显示
StatusBarHeaderView中类似
当然,你也可以选择一个类实现BatteryController.BatteryStateChangeCallback这个接口,仿照上面的写法
PS:6.0代码