300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > android获取电池信息;android获取电池容量 技术 电压 电量 温度等信息

android获取电池信息;android获取电池容量 技术 电压 电量 温度等信息

时间:2021-12-12 13:26:19

相关推荐

android获取电池信息;android获取电池容量 技术 电压 电量 温度等信息

android获取电池信息;android获取电池容量、技术、电压、电量、温度等信息

1、这里我仅展示工具类,需要注意的是这里的部分值要刷新后才能显示,添加刷新UI的方法即可,而且电量温度等都是变化的,本身就需要刷新

public class BatteryUtil {private static final String TAG = "BatteryUtil";private static String technology;private static int voltage;private static int level;private static double temperature;private static String status;private static String health;private static String plugged;/*** 获取电池容量*/public static String getBatteryCapacity(Context context) {Object mPowerProfile;double batteryCapacity = 0;final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";try {mPowerProfile = Class.forName(POWER_PROFILE_CLASS).getConstructor(Context.class).newInstance(context);batteryCapacity = (double) Class.forName(POWER_PROFILE_CLASS).getMethod("getBatteryCapacity").invoke(mPowerProfile);} catch (Exception e) {e.printStackTrace();}return String.valueOf(batteryCapacity);}/***通过接收系统广播获取电池的信息*/public static void ReceiverBatteryOhterInfo(Context context) {IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);Intent receiver = context.registerReceiver(null, filter);technology =receiver.getStringExtra("technology"); //获取电池技术if(technology.equals("") || technology.equals(null)){technology="Unknown";}voltage = receiver.getIntExtra("voltage", 0); //获取电压(mv)level = receiver.getIntExtra("level", 0); //获取当前电量temperature = receiver.getIntExtra("temperature", 0)/10.0; //获取温度(数值)并转为电池摄氏温度//电池状态switch (receiver.getIntExtra("status",0)){case BatteryManager.BATTERY_HEALTH_UNKNOWN:status="未知";break;case BatteryManager.BATTERY_STATUS_CHARGING:status="充电中";break;case BatteryManager.BATTERY_STATUS_DISCHARGING:status="放电中";break;case BatteryManager.BATTERY_STATUS_NOT_CHARGING:status="未充电";break;case BatteryManager.BATTERY_STATUS_FULL:status="电池满";break;}android.util.Log.d(TAG,"status:"+status);//电池健康情况switch (receiver.getIntExtra("health",0)){case BatteryManager.BATTERY_HEALTH_UNKNOWN:health="未知";break;case BatteryManager.BATTERY_HEALTH_GOOD:health="良好";break;case BatteryManager.BATTERY_HEALTH_OVERHEAT:health="过热";break;case BatteryManager.BATTERY_HEALTH_DEAD:health="没电";break;case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:health="过电压";break;case BatteryManager.BATTERY_HEALTH_COLD:health="温度过低";break;case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:health="未知错误";break;}android.util.Log.d(TAG,"health:"+health);//充电类型switch (receiver.getIntExtra("plugged",0)){case BatteryManager.BATTERY_PLUGGED_AC:plugged="充电器";break;case BatteryManager.BATTERY_PLUGGED_USB:plugged="USB";break;case BatteryManager.BATTERY_PLUGGED_WIRELESS:plugged="无线充电";break;default:plugged="未充电";break;}android.util.Log.d(TAG,"plugged:"+plugged);}}

刷新界面方法:/jppipai/article/details/120798652?spm=1001..3001.5501

参考:/su749520/article/details/80655708

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。