版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2017.08.06 |
前言
很多時(shí)候我們需要監(jiān)聽(tīng)和獲取電池的電量抡爹,ios提供了很多查看的方式掩驱,下面我們就一起來(lái)看一下。
功能要求
獲取設(shè)備的電池狀態(tài)和電量冬竟。
功能實(shí)現(xiàn)
獲取設(shè)備的電池狀態(tài)和電量可以使用UIDevice API
昙篙,下面我們看一下。
1. UIDevice API獲取電池電量
先看一下一個(gè)重要的類(lèi)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 <NSObject>
@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
這里還要用到個(gè)枚舉诱咏,是電池的狀態(tài)苔可。
下面看一下代碼實(shí)現(xiàn)。
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
這里分為四種狀態(tài):
-
UIDeviceBatteryStateUnknown
未知 -
UIDeviceBatteryStateUnplugged
放電 -
UIDeviceBatteryStateCharging
充電 -
UIDeviceBatteryStateFull
充滿(mǎn)
2. 代碼實(shí)現(xiàn)
下面我們就看一下代碼實(shí)現(xiàn)袋狞。
#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(@"電池狀態(tài) = %ld",[UIDevice currentDevice].batteryState);
}
@end
功能驗(yàn)證
下面看一下輸出結(jié)果焚辅。
2017-08-06 23:50:15.224520+0800 JJOC[7999:3191290] 電池電量 = 0.74
下面看一下效果圖。
電池電量
可以看見(jiàn)此時(shí)電量就是74%苟鸯。
后記
未完同蜻,待續(xù)~~~
美女