通过BatteryManager来获取关于电池的信息
实例
package com.example.softwarepatentdemo;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.TextView;import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;public class BatteryInfo extends AppCompatActivity {private final String TAG = "BatteryInfoStatus";private double batteryLevel;private double batteryVoltage;private String batteryHealth;private String batteryPlugged;private String batteryPresent;private String batteryStatus;private String batteryTechnology;private double batteryTemperature;private TextView batteryLevelContent,batteryVoltageContent,batteryHealthContent,batteryPluggedContent,batteryPresentContent,batteryStatusContent,batteryTechnologyContent,batteryTemperatureContent;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.battery_info);}@Overrideprotected void onResume() {super.onResume();initView();IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);registerReceiver(batteryLevelReceiver, intentFilter);}public void initView(){batteryLevelContent = findViewById(R.id.batteryLevel);batteryVoltageContent = findViewById(R.id.batteryVoltage);batteryHealthContent = findViewById(R.id.batteryHealth);batteryPluggedContent = findViewById(R.id.batteryPlugged);batteryPresentContent = findViewById(R.id.batteryPresent);batteryStatusContent = findViewById(R.id.batteryStatus);batteryTechnologyContent = findViewById(R.id.batteryTechnology);batteryTemperatureContent = findViewById(R.id.batteryTemperature);}@Overrideprotected void onDestroy() {super.onDestroy();unregisterReceiver(batteryLevelReceiver);}private BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {//Get battery levelbatteryLevel = getBatteryLevel(intent);batteryLevelContent.setText("" + batteryLevel);// Get battery voltagebatteryVoltage = getBatteryVoltage(intent);batteryVoltageContent.setText("" + batteryVoltage);// Get battery healthbatteryHealth = getBatteryHealth(intent);batteryHealthContent.setText("" + batteryHealth);// Get battery pluggedbatteryPlugged = getBatteryPlugged(intent);batteryPluggedContent.setText("" + batteryPlugged);// Get battery presentbatteryPresent = getBatteryPresent(intent);batteryPresentContent.setText("" + batteryPresent);// Get battery statusbatteryStatus = getBatteryStatus(intent);batteryStatusContent.setText("" + batteryStatus);// Get battery technologybatteryTechnology = getBatteryTechnology(intent);batteryTechnologyContent.setText("" + batteryTechnology);// Get battery temperaturebatteryTemperature = getBatteryTemperature(intent);batteryTemperatureContent.setText("" + batteryTemperature);}private double getBatteryTemperature(Intent intent) {int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); // EXTRA_TEMPERATURE 包含当前电池温度的整数double batteryTempDouble = temperature / 10.0;Log.d(TAG, "getBatteryTemperature: " + batteryTemperature);return batteryTempDouble;}private String getBatteryTechnology(Intent intent) {String technologyString = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY); // EXTRA_TECHNOLOGY 描述当前电池技术的字符串Log.d(TAG, "getBatteryTechnology: " + technologyString);return technologyString;}private String getBatteryStatus(Intent intent) {String statusString;int statusInt = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); // EXTRA_STATUS 包含当前状态常量的整数switch (statusInt){case BatteryManager.BATTERY_STATUS_CHARGING:statusString = "BATTERY_STATUS_CHARGING";break;case BatteryManager.BATTERY_STATUS_DISCHARGING:statusString = "BATTERY_STATUS_DISCHARGING";break;case BatteryManager.BATTERY_STATUS_FULL:statusString = "BATTERY_STATUS_FULL";break;case BatteryManager.BATTERY_STATUS_NOT_CHARGING:statusString = "BATTERY_STATUS_NOT_CHARGING";break;case BatteryManager.BATTERY_STATUS_UNKNOWN:statusString = "BATTERY_STATUS_UNKNOWN";break;default:statusString = "UNKNOWN";break;}Log.d(TAG, "getBatteryStatus: " + statusString);return statusString;}private String getBatteryPresent(Intent intent) {String presentString;boolean presentBool = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false); //EXTRA_PRESENT 布尔值,表示电池是否存在if (presentBool){presentString = "TRUE";}else {presentString = "FALSE";}Log.d(TAG, "getBatteryPresent: " + presentString);return presentString;}private String getBatteryPlugged(Intent intent) {String pluggedSrting;int pluggedInt = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); //EXTRA_PLUGGED 表示设备是否插入电源的整数; 0表示使用电池,其他常数是不同类型的电源。switch (pluggedInt){case BatteryManager.BATTERY_PLUGGED_AC: // 电源是一个交流充电器。pluggedSrting = "BATTERY_PLUGGED_AC";break;case BatteryManager.BATTERY_PLUGGED_USB: // 电源是一个USB端口pluggedSrting = "BATTERY_PLUGGED_USB";break;case BatteryManager.BATTERY_PLUGGED_WIRELESS: // 电源是无线的pluggedSrting = "BATTERY_PLUGGED_WIRELESS";break;default:pluggedSrting = "UNKNOWN";break;}Log.d(TAG, "getBatteryPlugged: " + pluggedSrting);return pluggedSrting;}private String getBatteryHealth(Intent intent) {String healthString;int healthInt = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);switch (healthInt){case BatteryManager.BATTERY_HEALTH_COLD: //BATTERY_HEALTH_COLDhealthString = "BATTERY_HEALD_COLD";break;case BatteryManager.BATTERY_HEALTH_DEAD:healthString = "BATTERY_HEALTH_DEAD";break;case BatteryManager.BATTERY_HEALTH_GOOD:healthString = "BATTERY_HEALTH_GOOD";break;case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:healthString = "BATTERY_HEALTH_OVER_VOLTAGE";break;case BatteryManager.BATTERY_HEALTH_OVERHEAT:healthString = "BATTERY_HEALTH_OVERHEAT";break;case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:healthString = "BATTERY_HEALTH_UNSPECIFIED_FAILURE";break;default:healthString = "UNKNOWN";break;}Log.d(TAG, "getBatteryHealth: " + healthString);return healthString;}private double getBatteryVoltage(Intent intent) {int voltageMV = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); // EXTRA_VOLTAGE 包含当前电池电压的整数。double voltageV = voltageMV / 1000.0;Log.d(TAG, "getBatteryVoltage: " + voltageV);return voltageV;}private double getBatteryLevel(Intent intent) {int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1); //EXTRA_LEVEL 包含当前电池电量的整数字段,从0到 EXTRA_SCALEint scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); // EXTRA_SCALE 包含最大电池电量的整数。double level = -1.0;if ((rawlevel >=0) && (scale >0)){level = (double) (rawlevel * 100) / scale;}Log.d(TAG, "getBatteryLevel: " + level);return level;}};
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="20dp"><RelativeLayoutandroid:id="@+id/relative_01"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:id="@+id/batteryLevel_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Level: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryLevel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryLevel_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout><RelativeLayoutandroid:id="@+id/relative_02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/relative_01"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/batteryVoltage_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Voltage: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryVoltage"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryVoltage_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout><RelativeLayoutandroid:id="@+id/relative_03"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/relative_02"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/batteryHealth_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Health: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryHealth"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryHealth_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout><RelativeLayoutandroid:id="@+id/relative_04"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/relative_03"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/batteryPlugged_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Plugged: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryPlugged"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryPlugged_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout><RelativeLayoutandroid:id="@+id/relative_05"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/relative_04"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/batteryPresent_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Present: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryPresent"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryPresent_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout><RelativeLayoutandroid:id="@+id/relative_06"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/relative_05"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/batteryStatus_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Status: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryStatus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryStatus_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout><RelativeLayoutandroid:id="@+id/relative_07"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/relative_06"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/batteryTechnology_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Technology: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryTechnology"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryTechnology_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout><RelativeLayoutandroid:id="@+id/relative_08"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/relative_07"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/batteryTemperature_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Temperature: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryTemperature"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryTemperature_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout>
</RelativeLayout>
result