iOS中我的应用程序的电池消耗检查 [英] battery consumption check for my app in iOS

查看:90
本文介绍了iOS中我的应用程序的电池消耗检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取我的应用的电池消耗详细信息.
我已经使用了用于跟踪的工具,并且我的能源使用量级别为1/20.
1/20是什么?

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);

  • API允许您注册以接收有关电池电量变化的通知.它仅报告以5%的递增或递减变化,但您可以使用计时器并测量两次更改(或初始电池电量和第一次更改)之间的时间.注册通知的方法如下:

  • 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];
    

  • 第一个通知会告诉您当前状态,例如拔下,充电或充满电.每当达到5%的增量时,就会触发秒.

    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.

    在我看来,如果给您的只是更改通知(上下更改5%),那么准确度就不是您可以很好或快速地计算出来的.如果设备不执行任何操作,则5%的更改可能会花费很长时间.

    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.

    也许您可以使用计时器监视[[UIDevice currentDevice] batteryLevel],但是,尽管我没有尝试过,但我认为它只会以相同的5%增量进行更新.

    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.

    来自: iphone:计算电池寿命

    1. 这是一个很好的例子: http: //blog.coriolis.ch/2009/02/14/以编程方式读取电池级别/

    这篇关于iOS中我的应用程序的电池消耗检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆