27 lines
821 B
Java
27 lines
821 B
Java
package com.uiui.os.utils;
|
|
|
|
import android.content.Context;
|
|
import android.content.ContextWrapper;
|
|
import android.content.Intent;
|
|
import android.content.IntentFilter;
|
|
import android.os.BatteryManager;
|
|
import android.os.Build;
|
|
|
|
public class Utils {
|
|
/**
|
|
* 获取电量
|
|
*
|
|
* @param mContext
|
|
* @return
|
|
*/
|
|
synchronized public static int getBatteryLevel(Context mContext) {
|
|
if (Build.VERSION.SDK_INT >= 21) {
|
|
return ((BatteryManager) mContext.getSystemService(Context.BATTERY_SERVICE)).getIntProperty(4);
|
|
} else {
|
|
Intent intent = (new ContextWrapper(mContext)).registerReceiver(null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
|
|
return intent.getIntExtra("level", -1) * 100 / intent.getIntExtra("scale", -1);
|
|
}
|
|
}
|
|
|
|
}
|