安卓电量Debug提示
程序员文章站
2022-06-13 21:10:47
...
根据雨松大大的文章,代码如下:
具体结果输出为:
1.池总容量:Power.capacity(毫安)
2.电压:Power.voltage(伏)
3.实时电流:curE(毫安)
4.实时平均功率:avgE * Power.voltage
5.满电量能玩:(Power.capacity / avgE).ToString(“f2”)(小时)
public class Power
{
static public float electricity
{
get
{
#if UNITY_ANDROID
//获取电流(微安),避免频繁获取,取一次大概2毫秒
float electricity = (float)manager.Call<int>("getIntProperty", PARAM_BATTERY);
//小于1W就认为它的单位是毫安,否则认为是微安
return ToMA(electricity);
#else
return -1f;
#endif
}
}
//获取电压 伏
static public float voltage { get; private set; }
//获取电池总容量 毫安
static public int capacity { get; private set; }
//获取实时电流参数
static object[] PARAM_BATTERY = new object[] { 2 }; //BatteryManager.BATTERY_PROPERTY_CURRENT_NOW)
static AndroidJavaObject manager;
static Power()
{
#if UNITY_ANDROID
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
manager = currActivity.Call<AndroidJavaObject>("getSystemService", new object[] { "batterymanager" });
capacity = (int)(ToMA((float)manager.Call<int>("getIntProperty", new object[] { 1 })) / ((float)manager.Call<int>("getIntProperty", new object[] { 4 })/100f)); //BATTERY_PROPERTY_CHARGE_COUNTER 1 BATTERY_PROPERTY_CAPACITY 4
AndroidJavaObject receive = currActivity.Call<AndroidJavaObject>("registerReceiver", new object[] { null,new AndroidJavaObject("android.content.IntentFilter", new object[] { "android.intent.action.BATTERY_CHANGED" }) });
if (receive != null)
{
voltage = (float)receive.Call<int>("getIntExtra", new object[] { "voltage",0 })/1000f; //BatteryManager.EXTRA_VOLTAGE
}
#endif
}
static float ToMA(float maOrua)
{
return maOrua < 10000 ? maOrua : maOrua / 1000f;
}
static public string temperature
{
get
{
#if UNITY_ANDROID
try
{
var type = File.ReadAllText($"/sys/class/thermal/thermal_zone9/type").Replace("\n", "");
if (float.TryParse(File.ReadAllText($"/sys/class/thermal/thermal_zone9/temp"), out var freq))
{
return type + ":" + (freq / 1000f);
}
}
catch
{
return "Not Support";
}
return "Not Support";
#else
return "Not Support";
#endif
}
}
static public string gtemperature
{
get
{
#if UNITY_ANDROID
try
{
var type = File.ReadAllText($"/sys/class/thermal/thermal_zone15/type").Replace("\n", "");
if (float.TryParse(File.ReadAllText($"/sys/class/thermal/thermal_zone15/temp"), out var freq))
{
return type + ":" + (freq / 1000f);
}
}
catch
{
return "Not Support";
}
return "Not Support";
#else
return "Not Support";
#endif
}
}
上一篇: 2019最新推荐的10个自助建站平台
推荐阅读
-
关于ADB的Android Debug Bridge(安卓调试桥)那些事
-
谷歌失误:让安卓用户设备耗电量猛增
-
必升!安卓10.0这功能省50%电量 不用羡慕果粉
-
微信提示音怎么更改 苹果/安卓微信提示铃声修改图文教程
-
编译安卓源码提示:TypeError: argument should be integer or bytes-like object, not ‘str‘
-
安卓版微信阿豆提示音怎么改?
-
实现安卓Spinner控件hint提示效果
-
关于ADB的Android Debug Bridge(安卓调试桥)那些事
-
安卓开发登录输入框智能提示历史登录账号
-
Win11安卓子系统提示IP地址不可用怎么办?