开启电量的百分比

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)) {

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值