2012-12-16 3 views
17

완료 :시간, 내가 사용하고 아이폰 OS

UIDevice *myDevice = [UIDevice currentDevice]; 
[myDevice setBatteryMonitoringEnabled:YES]; 
float batLeft = [myDevice batteryLevel]; 
int i = [myDevice batteryState];  
int batinfo = batLeft * 100; 

는 배터리 상태를 찾을 수 있습니다. 나는 요금 청구가 완료 될 때까지 남은 시간을 찾는 방법을 찾고있다. 예 : 1 시간 20 분 남음. 프로그래밍 방식으로 어떻게 찾을 수 있습니까?

답변

8

나는 official documentation에있는 어떤 방법도 발견하지 못했고, 클래스 덤프 된 private header은 클래스는 아니었다.

그래서 우리는 뭔가 들고 오지해야합니다. 순간 최고 "용액"I 생각하고있는 다운로드 시간을 추정 할 때 촬영 방식과 비슷하다/다운로드의 평균 속도를 계산하고 청구하고 그 속도가 (데이터 또는 전하의) 잔류량 분할 :

[UIDevice currentDevice].batteryMonitoringEnabled = YES; 
float prevBatteryLev = [UIDevice currentDevice].batteryLevel; 
NSDate *startDate = [NSDate date]; 

[[NSNotificationCenter defaultCenter] 
    addObserver:self 
     selector:@selector(batteryCharged) 
      name:UIDeviceBatteryLevelDidChangeNotification 
     object:nil 
]; 

- (void)batteryCharged 
{ 
    float currBatteryLev = [UIDevice currentDevice].batteryLevel; 
    // calculate speed of chargement 
    float avgChgSpeed = (prevBatteryLev - currBatteryLev)/[startDate timeIntervalSinceNow]; 
    // get how much the battery needs to be charged yet 
    float remBatteryLev = 1.0 - currBatteryLev; 
    // divide the two to obtain the remaining charge time 
    NSTimeInterval remSeconds = remBatteryLev/avgChgSpeed; 
    // convert/format `remSeconds' as appropriate 
} 
+4

충전은 선형이라고 가정합니다. 그렇지 않습니다. 배터리가 완전히 충전되면 충전 속도가 크게 느려집니다. – duskwuff

+0

@duskwuff 예, 운영 체제에서보고 한 배터리 수준조차도 정확하지 않을 수 있습니다. 내가 왜 "평균"이라는 용어 **를 사용했다고 생각합니까? 젠장 –

+0

감사 H2CO3 ... 일이, 내가 batterCharge의 method.any 제안 – iscavengers