您的位置:首页 > 移动开发 > IOS开发

获取ios系统的电池电量比例,是否充电中,是否充满

2015-08-20 14:43 477 查看
</pre><pre name="code" class="objc">    // Find the battery level
@try {
// Get the device
UIDevice *Device = [UIDevice currentDevice];
// Set battery monitoring on
Device.batteryMonitoringEnabled = YES;

// Set up the battery level float
float BatteryLevel = 0.0;
// Get the battery level
float BatteryCharge = [Device batteryLevel];

// Check to make sure the battery level is more than zero
if (BatteryCharge > 0.0f) {
// Make the battery level float equal to the charge * 100
BatteryLevel = BatteryCharge * 100;
} else {
// Unable to find the battery level
return -1;
}

// Output the battery level
return BatteryLevel;
}
@catch (NSException *exception) {
// Error out
return -1;
}

// is charging
@try {
// Get the device
UIDevice *Device = [UIDevice currentDevice];
// Set battery monitoring on
Device.batteryMonitoringEnabled = YES;

// Check the battery state
if ([Device batteryState] == UIDeviceBatteryStateCharging || [Device batteryState] == UIDeviceBatteryStateFull) {
// Device is charging
return true;
} else {
// Device is not charging
return false;
}
}
@catch (NSException *exception) {
// Error out
return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: