Android开发:显示电池电量

时间:2011-07-03 17:23:59

标签: android power-management

我一直在寻找这个,只发现真的搞砸了。 是不是有一种简单的方法可以在吐司或Textview上显示21%的电池电量? 或者我该如何实现这一目标?

//西蒙

5 个答案:

答案 0 :(得分:14)

要立即获得电池电量,请致电:

registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

(注意:从内存中输入,请根据需要调整)

这将返回一个Intent,其中包含BatteryManager类记录的各种附加内容。使用BatteryManager.EXTRA_LEVELBatteryManager.EXTRA_SCALE确定剩余的百分比。

如果您需要了解电池更换后的一段时间,请使用答案中概述的BroadcastReceiver方法@Augusto。

答案 1 :(得分:10)

如果您的意思是更改仿真器上的电池状态,请执行以下操作。通过telnet连接到仿真器并更改状态和容量

> telnet localhost 5554
Android Console: type 'help' for a list of commands
OK
power ac off
OK
power discharging
OK
power capacity 21
OK
exit
>

啊,你可以在Ted提到的页面上做它所说的,然后用一个处理程序来表示祝酒。这是您需要添加到活动中的代码。

private Handler handler = new Handler;

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(final Context context, Intent intent) {
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
        Log.i(TAG, "level: " + level + "; scale: " + scale);
        int percent = (level*100)/scale;

        final String text = String.valueOf(percent) + "%";
        handler.post( new Runnable() {

            public void run() {
                Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
            }
        });

    }
};

答案 2 :(得分:6)

    public static String batteryLevel(Context context)
    {
        Intent intent  = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));   
        int    level   = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int    scale   = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
        int    percent = (level*100)/scale;
        return String.valueOf(percent) + "%";
    }

答案 3 :(得分:0)

你可以用它。

private void batteryLevel() {
        BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                //context.unregisterReceiver(this);
                int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                int level = -1;
                if (rawlevel >= 0 && scale > 0) {
                    level = (rawlevel * 100) / scale;
                }
                batterLevel.setText("Battery Level Remaining: " + level + "%");
            }
        };
        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryLevelReceiver, batteryLevelFilter);
    }

答案 4 :(得分:0)

我使用了这样的组合:

 BatteryManager bm = (BatteryManager) getActivity().getSystemService(BATTERY_SERVICE);
 int batLevel = 100;
 int level;
 int scale;
 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
      batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
 }
 if(batLevel == 0){
     Intent intent  = getActivity().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
     level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
     scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
     batLevel = (level*100)/scale;
}

之所以这样,是因为在配备棉花糖“ batLevel”的Nexus 5设备上始终返回0。那完全解决了!

祝你好运