Android V5.1
1.直接显示在电池里
--- a/android/frameworks/base/packages/SystemUI/src/com/android/systemui/BatteryMeterView.java
+++ b/android/frameworks/base/packages/SystemUI/src/com/android/systemui/BatteryMeterView.java
@@ -203,7 +203,7 @@ public class BatteryMeterView extends View implements DemoMode,
colors.recycle();
atts.recycle();
mShowPercent = ENABLE_PERCENT && 0 != Settings.System.getInt(
- context.getContentResolver(), "status_bar_show_battery_percent", 0);
+ context.getContentResolver(), "status_bar_show_battery_percent", 1);
2.状态栏电量百分比控制
--- a/android/frameworks/base/packages/SystemUI/res/layout/system_icons.xml
+++ b/android/frameworks/base/packages/SystemUI/res/layout/system_icons.xml
@@ -32,9 +32,16 @@
android:layout_height="wrap_content"
android:layout_marginStart="2.5dp"/>
+ <TextView
+ android:id="@+id/pbattery"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ />
<!-- battery must be padded below to match assets -->
<com.android.systemui.BatteryMeterView android:id="@+id/battery"
android:layout_height="14.5dp"
android:layout_width="9.5dp"
android:layout_marginBottom="@dimen/battery_margin_bottom"/>
</LinearLayout>
--- a/android/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/android/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -798,6 +798,7 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
// Other icons
mLocationController = new LocationControllerImpl(mContext); // will post a notification
mBatteryController = new BatteryController(mContext);
+ mBatteryController.setPercentageView((TextView)mStatusBarWindow.findViewById(R.id.pbattery));
--- a/android/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryController.ja
+++ b/android/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryController.ja
@@ -27,6 +27,16 @@ import android.util.Log;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
+import android.content.ContentResolver;
+import android.database.ContentObserver;
+import android.os.Handler;
+import android.os.UserHandle;
+import android.view.View;
+import android.widget.TextView;
+import android.provider.Settings;
+import android.util.Log;
+import com.android.systemui.R;
+
public class BatteryController extends BroadcastReceiver {
private static final String TAG = "BatteryController";
@@ -35,19 +45,38 @@ public class BatteryController extends BroadcastReceiver {
private final ArrayList<BatteryStateChangeCallback> mChangeCallbacks = new ArrayList<>();
private final PowerManager mPowerManager;
+ private TextView mBatteryPercentageView;
+ private String mPercentage = "100%";
+ private Context mContext;
+
private int mLevel;
private boolean mPluggedIn;
private boolean mCharging;
private boolean mCharged;
private boolean mPowerSave;
+ private final Handler mHandler = new Handler();
+ public void setPercentageView(TextView v){
+ mBatteryPercentageView = v;
+ }
public BatteryController(Context context) {
mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
+ mContext = context;
+
+ ContentObserver obs = new ContentObserver(mHandler){
+ @Override
+ public void onChange(boolean selfChange){
+ updatePowerSave();
+ }
+ };
+ final ContentResolver resolver = context.getContentResolver();
+ updatePowerSave();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
filter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING);
+
context.registerReceiver(this, filter);
updatePowerSave();
@@ -85,6 +114,10 @@ public class BatteryController extends BroadcastReceiver {
mCharging = mCharged || status == BatteryManager.BATTERY_STATUS_CHARGING;
fireBatteryLevelChanged();
+ int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
+ mPercentage = String.valueOf(mLevel*100/scale)+"%";
+ mBatteryPercentageView.setText(mPercentage);
+ mBatteryPercentageView.setVisibility(View.VISIBLE);
} else if (action.equals(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)) {