Monitoring of iOS Batteries

Source: Internet
Author: User
Tags uikit


#import "ViewController.h"

#import <UIKit/UIKit.h>

@interface viewcontroller ()




@property (nonatomic,readonly) float batterylevel ns_available_ios (3_0);


//0.. 1.0. -1.0 If Uidevicebatterystateunknown it returns a floating-point value between 0.00-1.00.

@end


@implementation Viewcontroller


-(void) viewdidload {

[super viewdidload];

// turn on battery monitoring

[uidevice currentdevice]. batterymonitoringenabled = YES ;

// Get the status of the battery

< Span style= "color: #000000;" >    uidevicebatterystate Batterystate = [ Uidevice currentdevice ]. batterystate

// get the remaining power range between 0.000000 to 1.000000

cgfloat batterylevel = [uidevice currentdevice ]. Batterylevel ;

The state of//battery is divided into:

    /*

Uidevicebatterystateunknown, Unknown

uidevicebatterystateunplugged,// not charging

uidevicebatterystatecharging,// Charging now

uidevicebatterystatefull,// Full power

*/

if (batterystate = = uidevicebatterystateunknown) {

self. Dllabel . text = [nsstring stringwithformat:@ "%0.0f%%", batterylevel*];

NSLog(@ "Unknow");

}else{

NSLog(@ "know");

// The remaining power is displayed with a label .

self. Dllabel . text = [nsstring stringwithformat:@ "%0.0f%%", batterylevel*];

}

//

[Self getcurrentbatterylevel];

[Uidevice currentdevice].batterymonitoringenabled = YES;

[[Nsnotificationcenter Defaultcenter]

Addobserverforname:uidevicebatteryleveldidchangenotification

Object:nil Queue:[nsoperationqueue Mainqueue]

usingblock:^ (Nsnotification *notification) {

Level have changed

Dl=[uidevice Currentdevice].batterylevel;

//NSLog (@ " battery charge:%.2f", [Uidevice currentdevice].batterylevel);

//

Self.dlLabel.text = [NSString stringwithformat:@ "%f", [Uidevice currentdevice].batterylevel];

//

//     }];

Additional setup after loading the view, typically from a nib.

}



This article is from the "zhuoking" blog, make sure to keep this source http://9951038.blog.51cto.com/9941038/1831784

Monitoring of iOS Batteries

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.