Android 获取系统电量信息

当有需求,程序启动测试低功耗问题时,获取系统电池,电量信息;

1、使用  BatteryManager:

 /**
     * 获取当前系统电量 %
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void getElectricity() {
        BatteryManager batterymanager = (BatteryManager) getSystemService(BATTERY_SERVICE);
        batterymanager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
        Log.e("aaa batterymanager",+batterymanager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)+"%");
    }

2、使用  IntentFilter :

Android 获取系统电量信息_第1张图片

    private void getElect() {
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent receiver = instance.registerReceiver(null, filter);
        int level = receiver.getIntExtra("level", 0);//获取当前电量
        int scale = receiver.getIntExtra("scale", 0);//获取总电量
        int status = receiver.getIntExtra("status", 0);//获取充电状态
        int voltage = receiver.getIntExtra("voltage", 0);//获取电压(mv)
        int temperature = receiver.getIntExtra("temperature", 0);//获取温度(数值)
        double t = temperature / 10.0;  //运算转换,电池摄氏温度,默认获取的非摄氏温度值

        Log.e("aaa level",""+level+"%");
        Log.e("aaa scale",""+scale);
        Log.e("aaa status",""+status);
        Log.e("aaa voltage",""+voltage);
        Log.e("aaa temperature",""+t);
    }

3、日志 log:

12-05 09:25:26.304 22484-22484/com.ped.rtp E/aaa batterymanager: 11%
12-05 09:25:26.313 22484-22484/com.ped.rtp E/aaa level: 11%
12-05 09:25:26.313 22484-22484/com.ped.rtp E/aaa scale: 100
12-05 09:25:26.313 22484-22484/com.ped.rtp E/aaa status: 2
12-05 09:25:26.313 22484-22484/com.ped.rtp E/aaa voltage: 3541
12-05 09:25:26.313 22484-22484/com.ped.rtp E/aaa temperature: 26.3

 

你可能感兴趣的:(Android,系统)