IOS的获取电量代码如下:
-
-
(int)getCurrentBatteryLevel
{
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive||[UIApplication sharedApplication].applicationState==UIApplicationStateInactive) {
void *result = nil;
object_getInstanceVariable([UIApplication sharedApplication], "_statusBar", &result);
id status = result;
for (id aview in [status subviews]) {
for (id bview in [aview subviews]) {
int batteryLevel = 0;
if ([NSStringFromClass([bview class]) caseInsensitiveCompare:@"UIStatusBarBatteryItemView"] == NSOrderedSame&&[[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
{
object_getInstanceVariable(bview, "_capacity", &result);
batteryLevel = (int)result;
NSLog(@"电池电量:%d",batteryLevel);
if (batteryLevel > 0 && batteryLevel <= 100) {
return batteryLevel;} else { return 0; } }
}
}
return 0;
} -
2 个回复
球风无痕 (作者)
没人帮忙吗?
DCloud_IOS_XTY