17

使ってます :

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

バッテリーの状態を確認します。充電が完了するまでの残り時間を探す方法を探しています。例:残り1時間20分。プログラムでそれを見つけるにはどうすればよいですか?

4

1 に答える 1

10

公式ドキュメントにも、クラスのクラスダンプされたプライベートヘッダーにも、このためのメソッドは見つかりませんでしたUIDevice

だから私たちは何かを考え出さなければなりません。私が現時点で考えている最善の「解決策」は、ダウンロード時間を見積もるときに採用したアプローチに似ています。ダウンロード/充電の平均速度を計算し、残りの量(データまたは充電)をその速度で除算します。

[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
}
于 2012-12-16T07:22:53.633 に答える