unity3d 获取电量电池状态等 android and ios
2017-11-09 18:55
746 查看
获取电量
C#
java
ios
获取电池状态
C#
java
IOS
C#
public static int GetBatteryLevel() { int level = 0; #if UNITY_ANDROID try { AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); AndroidJavaClass unityPluginLoader = new AndroidJavaClass("java类全名"); level = unityPluginLoader.CallStatic<int>("GetBatteryLevel", currentActivity); } catch (System.Exception e) { } #elif UNITY_IOS level = (int)_IOS_GetBatteryLevel(); #endif return level; }
java
public static int GetBatteryLevel(Activity currentActivity) { int level = 0; try { IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent intent = currentActivity.registerReceiver(null, ifilter); level = intent.getIntExtra("level", 0);// 获得当前电量 // int scale = intent.getIntExtra("scale", 0);//获得总电量 // int status = intent.getIntExtra("status", 0);//电池充电状态 // int health = intent.getIntExtra("health", 0);//电池健康状况 // int batteryV = intent.getIntExtra("voltage", 0); //电池电压(mv) // int temperature = intent.getIntExtra("temperature", 0); // //电池温度(数值) } catch (Exception e) { } } return level; }
ios
float _IOS_GetBatteryLevel() { UIDevice *myDevice = [UIDevice currentDevice]; [myDevice setBatteryMonitoringEnabled:YES]; float level = [myDevice batteryLevel] * 100; return level; }
获取电池状态
C#
public static bool GetBatteryChargingState() { bool isCharging = false; #if UNITY_ANDROID try { AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); AndroidJavaClass unityPluginLoader = new AndroidJavaClass("java类全名"); isCharging = unityPluginLoader.CallStatic<bool>("GetBatteryChargingState", currentActivity); } catch (System.Exception e) { } #elif UNITY_IOS isCharging = (bool)_IOS_GetBatteryChargingState(); #endif return isCharging; }
java
public static boolean GetBatteryChargingState(Activity currentActivity) { boolean isCharging = false; try { IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent intent = currentActivity.registerReceiver(null, ifilter); int status = intent.getIntExtra("status", 0);// 电池充电状态 if (status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL) { isCharging = true; } } catch (Exception e) { } } return isCharging; }
IOS
bool _IOS_GetBatteryChargingState() { UIDevice *myDevice = [UIDevice currentDevice]; [myDevice setBatteryMonitoringEnabled:YES]; bool isCharging = false; UIDeviceBatteryState state = [myDevice batteryState]; if(state == UIDeviceBatteryStateCharging) { isCharging = true; } return isCharging; }
相关文章推荐
- 获取Android设备电池电量状态
- 获取Android设备电池电量状态
- 获取Android设备电池电量状态
- 获取Android设备电池电量状态
- Android 获取 Wifi 信号强度,检测是 Wifi 或者流量,获取手机电池电量及充电状态
- android 获取充电状态 电池电量
- 获取Android设备电池电量状态
- Android 获取 Wifi 信号强度,检测是 Wifi 或者流量,获取手机电池电量及充电状态
- 获取Android设备电池电量状态
- Android监控电池电量和充电状态
- Android 获取电池电量
- Android获取手机电池电量用法实例
- Android获取电池充电状态的方式
- 如何获取iPhone电池的状态和当前的电量
- Android中获取电池电量
- Android官方开发文档Training系列课程中文版:电池续航时间优化之监测电池电量及充电状态
- Android中获取电池电量
- android中获取电池当前电量
- android 电池状态侦听显示 电量 充电动态显示
- Android开发:优化电池续航-监听电量和充电状态