2015-06-23 67 views
0

当batterlevel达到100%(测试我设置了11%或更多)并且充电器未被移除时,我想响铃。UIDeviceBatteryLevelDidChangeNotification当应用程序进入后台时的问题

它可以在应用程序处于前景时正常工作,但在应用程序到达背景时停止。

任何人都可以帮忙。

我的代码如下面的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

当你的应用程序进入后台模式 - 几乎所有的代码被暂停。您需要做额外的工作才能使用UILocalNotifications或类似的工具来获取关于未来事件的通知,而应用仍处于后台。

+0

感谢您的回复。 是的,我知道,我尝试了几个选项,但没有工作。你能帮我解决这个问题:在iOS7和iOS 8.x平台上写什么来让它工作? – nbt45

+0

@Tander您无法使用UILocalNotifications唤醒您的应用程序。 –

相关问题