獲取設(shè)備的電池狀態(tài)(一)—— UIDevice API獲取

版本記錄

版本號(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ù)~~~

美女
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市湾蔓,隨后出現(xiàn)的幾起案子默责,更是在濱河造成了極大的恐慌杖虾,老刑警劉巖奇适,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件柠衅,死亡現(xiàn)場(chǎng)離奇詭異茄茁,居然都是意外死亡裙顽,警方通過(guò)查閱死者的電腦和手機(jī)愈犹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)勋锤,“玉大人侥祭,你說(shuō)我怎么就攤上這事谈宛。” “怎么了恢筝?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我勋拟,道長(zhǎng)敢靡,這世上最難降的妖魔是什么挂滓? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任赶站,我火速辦了婚禮,結(jié)果婚禮上纺念,老公的妹妹穿的比我還像新娘。我一直安慰自己陷谱,他們只是感情好烙博,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著渣窜,像睡著了一般乔宿。 火紅的嫁衣襯著肌膚如雪冬阳。 梳的紋絲不亂的頭發(fā)上蛤虐,一...
    開(kāi)封第一講書(shū)人閱讀 51,365評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼饲常。 笑死蹲堂,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的贝淤。 我是一名探鬼主播柒竞,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼播聪!你這毒婦竟也來(lái)了朽基?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤离陶,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后招刨,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體霎俩,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年沉眶,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了打却。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡谎倔,死狀恐怖柳击,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情传藏,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布彤守,位于F島的核電站毯侦,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏具垫。R本人自食惡果不足惜侈离,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望筝蚕。 院中可真熱鬧卦碾,春花似錦、人聲如沸起宽。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)坯沪。三九已至绿映,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背叉弦。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工丐一, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人淹冰。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓库车,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親樱拴。 傳聞我的和親對(duì)象是個(gè)殘疾皇子柠衍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容