我想在击球手达到100%时发出警报(测试我将其设置为11%或更多)并且没有移除充电器。
当应用程序处于前台时它可以正常工作,但当应用程序到达后台时停止。
请有人帮忙吗。
我的代码如下所示为viewcontroller。
//
// MainViewController.m
// GoGreen
//
// Created by nbtmac on 25/05/15.
//
//
#import "MainViewController.h"
@interface MainViewController ()
{
int timerCount;
float batteryLevel;
NSString *userID;
NSDate *chargedAt;
NSInteger chargedHr;
NSInteger chargedMin;
NSTimer* alarmTimer;
SystemSoundID _soundId;
}
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.tabBarController.tabBar setHidden:false];
[self createSoundId];
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tabBarController.tabBar setHidden:false];
self.navigationItem.hidesBackButton = true;
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(batteryLevelChanged:)
name:UIDeviceBatteryLevelDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(batteryStateChanged:)
name:UIDeviceBatteryStateDidChangeNotification object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark - Misc Methods
- (void) createSoundId
{
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Loud_Alarm_Clock_Buzzer" ofType:@"wav"];
if ([[NSFileManager defaultManager] fileExistsAtPath:soundPath])
{
NSURL* soundPathURL = [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundPathURL, &_soundId);
}
}
-(void) alarmTimer:(NSTimer*) alarmTimer
{
timerCount++;
AudioServicesPlaySystemSound(_soundId);
}
- (void)batteryLevelChanged:(NSNotification *)notification
{
batteryLevel = (([UIDevice currentDevice].batteryLevel) * 100);
if (batteryLevel >= 11.00)
{
NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
chargedHr = [components hour];
chargedMin = [components minute];
if (timerCount < 1)
{
alarmTimer = [NSTimer scheduledTimerWithTimeInterval: 60.0 * 1 target: self
selector: @selector(alarmTimer:) userInfo: nil repeats: YES];
}
[UIApplication sharedApplication].idleTimerDisabled = YES;
}
}
- (void)batteryStateChanged:(NSNotification *)notification
{
UIDeviceBatteryState currentState = [UIDevice currentDevice].batteryState;
if (currentState == UIDeviceBatteryStateFull)
{
}
else if (currentState == UIDeviceBatteryStateUnplugged)
{
[alarmTimer invalidate];
[UIApplication sharedApplication].idleTimerDisabled = NO;
batteryLevel = (([UIDevice currentDevice].batteryLevel) * 100);
NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
NSInteger unPlugHr = [components hour];
NSInteger unPlugMin = [components minute];
NSInteger totUnPlugMins = (unPlugHr * 60) + unPlugMin;
NSInteger totChargedMins = (chargedHr * 60) + chargedMin + 1;
if ((batteryLevel >= 100.00) && (totChargedMins > totUnPlugMins))
{
if(appDelegate().isInternetReachable)
{
}
else
{
[[Util okAlert:@"Error" message:@"Please check your internet connection."] show];
}
}
}
}
@end
由于
Nirav
答案 0 :(得分:0)
当您的应用进入后台模式时 - 几乎所有代码都会被暂停。您需要为此工作做额外的工作才能使用UILocalNotifications
或类似内容,以便在应用程序保留在后台时获得有关未来事件的通知。