版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.08.06 |
前言
很多时候我们需要监听和获取电池的电量,ios提供了很多查看的方式,下面我们就一起来看一下。
功能要求
获取设备的电池状态和电量。
功能实现
获取设备的电池状态和电量可以使用UIDevice API
,下面我们看一下。
1. UIDevice API获取电池电量
先看一下一个重要的类UIDevice
UIDevice
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIDevice : NSObject
#if UIKIT_DEFINE_AS_PROPERTIES
@property(class, nonatomic, readonly) UIDevice *currentDevice;
#else
+ (UIDevice *)currentDevice;
#endif
@property(nonatomic,readonly,strong) NSString *name; // e.g. "My iPhone"
@property(nonatomic,readonly,strong) NSString *model; // e.g. @"iPhone", @"iPod touch"
@property(nonatomic,readonly,strong) NSString *localizedModel; // localized version of model
@property(nonatomic,readonly,strong) NSString *systemName; // e.g. @"iOS"
@property(nonatomic,readonly,strong) NSString *systemVersion; // e.g. @"4.0"
@property(nonatomic,readonly) UIDeviceOrientation orientation __TVOS_PROHIBITED; // return current device orientation. this will return UIDeviceOrientationUnknown unless device orientation notifications are being generated.
@property(nullable, nonatomic,readonly,strong) NSUUID *identifierForVendor NS_AVAILABLE_IOS(6_0); // a UUID that may be used to uniquely identify the device, same across apps from a single vendor.
@property(nonatomic,readonly,getter=isGeneratingDeviceOrientationNotifications) BOOL generatesDeviceOrientationNotifications __TVOS_PROHIBITED;
- (void)beginGeneratingDeviceOrientationNotifications __TVOS_PROHIBITED; // nestable
- (void)endGeneratingDeviceOrientationNotifications __TVOS_PROHIBITED;
@property(nonatomic,getter=isBatteryMonitoringEnabled) BOOL batteryMonitoringEnabled NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; // default is NO
@property(nonatomic,readonly) UIDeviceBatteryState batteryState NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; // UIDeviceBatteryStateUnknown if monitoring disabled
@property(nonatomic,readonly) float batteryLevel NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; // 0 .. 1.0. -1.0 if UIDeviceBatteryStateUnknown
@property(nonatomic,getter=isProximityMonitoringEnabled) BOOL proximityMonitoringEnabled NS_AVAILABLE_IOS(3_0); // default is NO
@property(nonatomic,readonly) BOOL proximityState NS_AVAILABLE_IOS(3_0); // always returns NO if no proximity detector
@property(nonatomic,readonly,getter=isMultitaskingSupported) BOOL multitaskingSupported NS_AVAILABLE_IOS(4_0);
@property(nonatomic,readonly) UIUserInterfaceIdiom userInterfaceIdiom NS_AVAILABLE_IOS(3_2);
- (void)playInputClick NS_AVAILABLE_IOS(4_2); // Plays a click only if an enabling input view is on-screen and user has enabled input clicks.
@end
@protocol UIInputViewAudioFeedback
@optional
@property (nonatomic, readonly) BOOL enableInputClicksWhenVisible; // If YES, an input view will enable playInputClick.
@end
/* The UI_USER_INTERFACE_IDIOM() function is provided for use when deploying to a version of the iOS less than 3.2. If the earliest version of iPhone/iOS that you will be deploying for is 3.2 or greater, you may use -[UIDevice userInterfaceIdiom] directly.
*/
static inline UIUserInterfaceIdiom UI_USER_INTERFACE_IDIOM() {
return ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ?
[[UIDevice currentDevice] userInterfaceIdiom] :
UIUserInterfaceIdiomPhone);
}
UIKIT_EXTERN NSNotificationName const UIDeviceOrientationDidChangeNotification __TVOS_PROHIBITED;
UIKIT_EXTERN NSNotificationName const UIDeviceBatteryStateDidChangeNotification NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
UIKIT_EXTERN NSNotificationName const UIDeviceBatteryLevelDidChangeNotification NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
UIKIT_EXTERN NSNotificationName const UIDeviceProximityStateDidChangeNotification NS_AVAILABLE_IOS(3_0);
NS_ASSUME_NONNULL_END
这里还要用到个枚举,是电池的状态。
下面看一下代码实现。
typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
UIDeviceBatteryStateUnknown,
UIDeviceBatteryStateUnplugged, // on battery, discharging
UIDeviceBatteryStateCharging, // plugged in, less than 100%
UIDeviceBatteryStateFull, // plugged in, at 100%
} __TVOS_PROHIBITED; // available in iPhone 3.0
这里分为四种状态:
-
UIDeviceBatteryStateUnknown
未知 -
UIDeviceBatteryStateUnplugged
放电 -
UIDeviceBatteryStateCharging
充电 -
UIDeviceBatteryStateFull
充满
2. 代码实现
下面我们就看一下代码实现。
#import "JJBatteryDeviceVC.h"
@interface JJBatteryDeviceVC ()
@end
@implementation JJBatteryDeviceVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStatusChanged:) name:UIDeviceBatteryStateDidChangeNotification object:nil];
}
#pragma mark - Action && Notification
- (void)batteryLevelChanged:(NSNotification *)noti
{
NSLog(@"电池电量 = %.2f",[UIDevice currentDevice].batteryLevel);
}
- (void)batteryStatusChanged:(NSNotification *)noti
{
NSLog(@"电池状态 = %ld",[UIDevice currentDevice].batteryState);
}
@end
功能验证
下面看一下输出结果。
2017-08-06 23:50:15.224520+0800 JJOC[7999:3191290] 电池电量 = 0.74
下面看一下效果图。
可以看见此时电量就是74%。
后记
未完,待续~~~