其他分享
首页 > 其他分享> > 安卓电量Debug提示

安卓电量Debug提示

作者:互联网

根据雨松大大的文章,代码如下:

具体结果输出为:
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
        }
    }

标签:object,return,安卓,float,thermal,电量,static,Debug,new
来源: https://blog.csdn.net/qq_33064771/article/details/116748700