有没有办法检测是否显示电池电量警告?我向 UIApplicationDidBecomeActiveNotification 注册了一个通知,我想知道它是否是由于电池电量不足警告而触发的,因此我可以以不同的方式处理它。
问问题
691 次
1 回答
2
您可以以编程方式监控电池电量,当它达到一定水平时,您可以处理您的事件。
-(NSString*)batteryStateStatus:(UIDeviceBatteryState)state{
switch ( state )
{
case UIDeviceBatteryStateUnknown:
return @"Unknown";
break;
case UIDeviceBatteryStateUnplugged:
return @"Unplugged";
break;
case UIDeviceBatteryStateCharging:
return @"Charging";
case UIDeviceBatteryStateFull:
return @"Charged";
}
return nil;
}
-(NSString *)getBatteryPercent
{
CFTypeRef blob = IOPSCopyPowerSourcesInfo();
CFArrayRef sources = IOPSCopyPowerSourcesList(blob);
CFDictionaryRef pSource = NULL;
const void *psValue;
NSString *thePercent;
int i;
int curCapacity = 0;
int maxCapacity = 0;
int percent;
int numOfSources = CFArrayGetCount(sources);
//if (numOfSources == 0) return 1;
for (i = 0 ; i < numOfSources ; i++)
{
pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
//if (!pSource) return 2;
psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));
psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);
psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);
percent = (int)((double)curCapacity/(double)maxCapacity * 100);
}
return [NSString stringWithFormat:@"%d",percent];
}
于 2011-03-04T01:16:10.447 回答