package id.dell.clean.helper;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
public class BatteryUtil {
public static final String TAG = "BatteryUtil";
public static String technology;
public static int voltage;
public static int level;
public static double temperature;
public static String status;
public static String health;
public static String plugged;
public static double batteryCapacity = 0;
/**
* 获取电池容量
*/
public static void getBatteryCapacity(Context context) {
Object mPowerProfile;
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();
}
}
/**
*通过接收系统广播获取电池的信息
*/
public static void ReceiverBatteryOtherInfo(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);
}
}
![](https://csdnimg.cn/release/blogv2/dist/components/img/blogType.png)
Androd color
03-09
104
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
![](https://csdnimg.cn/release/blogv2/dist/components/img/blogType.png)
Android权限明细
01-12
217
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-08
227
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-04
563
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-04
405
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-04
2759
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-04
887
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-06
168
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-16
287
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-16
658
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-10
1487
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-09
3239
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交