How to get real time battery level on iOS with a 1% granularity

后端 未结 7 1746
清酒与你
清酒与你 2020-12-05 03:19

I\'m using this function to get current battery level of device:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
UIDevice *myDevice = [UIDevice c         


        
7条回答
  •  广开言路
    2020-12-05 03:55

    The answers above are very good, but they are all in Obj-C, I have used these with other examples to do the same task on MonoTouch, so I am putting my code here in case anybody needs it:

    try
    {
        UIDevice.CurrentDevice.BatteryMonitoringEnabled = true;
        _Battery.Level = (int)(UIDevice.CurrentDevice.BatteryLevel * IOSBatteryLevelScalingFactor);
        _Battery.State = UIDevice.CurrentDevice.BatteryState;
    }
    catch (Exception e)
    {
        ExceptionHandler.HandleException(e, "BatteryState.Update");
        throw new BatteryUpdateException();
    }
    finally
    {
        UIDevice.CurrentDevice.BatteryMonitoringEnabled = false;
    }
    

    I also have a full post on my blog to give all the details in here

提交回复
热议问题