battery consumption check for my app in iOS

江枫思渺然 提交于 2019-12-12 01:27:25

问题


I need to get battery consumption details for my app.
I have used instruments for tracking and i am getting energy usage level as 1/20.
What is this 1/20?


回答1:


  1. Shortly :

    UIDevice *Device = [UIDevice currentDevice];
    [Device setBatteryMonitoringEnabled:YES];
    
     int state = [Device batteryState];
    NSLog(@"Now the status: %d",state);
     double batLeft = (float)[Device batteryLevel] * 100;
      NSLog(@"Charge left: %ld", batLeft);
    
  2. The API allows you to register to receive notifications for changes to the battery level. It only reports a change at 5% increments up or down, but you can use a timer and measure the time between two changes (or initial battery level and first change). Here's how you register for the notifications:

       // Use this call to get the current battery level as a float
      // [[UIDevice currentDevice] batteryLevel]
    
       [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
    
      [[NSNotificationCenter defaultCenter] addObserver:self
                                        selector:@selector(batteryStateDidChange:)
                                         name:UIDeviceBatteryStateDidChangeNotification
                                       object:nil];
    
       [[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(batteryLevelDidChange:)
                                         name:UIDeviceBatteryLevelDidChangeNotification
                                       object:nil];
    

The first notification tells you the current state, e.g. unplugged, charging, or full. The second will get triggered whenever a 5% increment is reached.

Seems to me that if all you're given is change notifications at 5% changes up or down, accuracy is not something you can calculate very well or quickly. A 5% change could take a very long time if the device isn't doing anything.

Maybe you can monitor [[UIDevice currentDevice] batteryLevel] with a timer, however, while I haven't tried it I think it only gets updated at this same 5% increment.

From: iphone: Calculating battery life

  1. Here is a good example : http://blog.coriolis.ch/2009/02/14/reading-the-battery-level-programmatically/


来源:https://stackoverflow.com/questions/40086495/battery-consumption-check-for-my-app-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!