2011-12-23 3 views
3

내 iPhone 타이머 응용 프로그램에서타이머 : 백그라운드에서 타이머를 활성화하는 방법

타이머는 백그라운드에서 실행되어야합니다.

그래서, 나는 appdelegate에서 알림을 설정했습니다. 완벽하게 작동합니다 ... 타이머를 활성화하는보기 컨트롤러에서 메소드를 호출하고 있습니다.

보고 몇 가지 코드를 가지고

...

앱 위임하는 것은

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    /* 
    Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
    */ 

    NSLog(@"Time Remaining %d",(self.viewController.totalSeconds-self.viewController.totalCount)); 
    [self.viewController selectandnotify:(self.viewController.totalSeconds-self.viewController.totalCount)]; 
    [self.viewController stopTimer]; 
    [self.viewController startTimerAction]; 

} 

여기에 내가 방법 startTimerAction 내보기 컨트롤러에 메소드를 호출하고 ... 이것 좀보세요. ..

-(void)startTimerAction 
{ 
timer_main = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(ShowActicity) userInfo:nil repeats:YES]; 
} 

NSTimer 어느 여기 414,마다

- 방법은 각 초 ... 내보기 컨트롤러 아래 어느 한 후 호출 ShowActivity ...

-(void)ShowActicity 
{ 

    NSLog(@"Total Counts %d",totalCount); 
    if (totalCount == totalSeconds) { 
     if ([timer_main isValid]) { 
      [timer_main invalidate]; 
      isTimeOver = YES; 
      [self generateLog]; 
     } 
    } else { 
     totalCount++; 

     seconds =seconds + 1; 
     if(seconds > 59) 
     { 
      minutes = minutes + 1; 
      seconds= 0; 
     } 

}이 방법마다 호출하는 방법을

뷰 컨트롤러에서 .....

  • 어떻게 AppDelegate에 ...각 시간 showActivity 메서드를 호출 할 수 있습니다

    • 나는

  • 이 사실 나는이 응용 프로그램이 응용 프로그램의 경우보기 스위치를 실행하려면 .. 내 AppDelegate에있는 showActivity와 타이머를 만들 것을

  • 에 대한 대리자를 사용해야합니다. ....

내가 위임을하는 것이 좋은 옵션일까요?

다른 방법 .... 몇 가지 제안이

답변

4

일반적으로

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    UIApplication* app = [UIApplication sharedApplication]; 

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
     [app endBackgroundTask:bgTask]; 
     bgTask = UIBackgroundTaskInvalid; 
    }]; 

    // Start the long-running task and return immediately. 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     // Do the work associated with the task. 
     [self startTimerAction]; 
     [app endBackgroundTask:bgTask]; 
     bgTask = UIBackgroundTaskInvalid; 
    }); 
} 

http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW3

+0

감사합니다 Sachin를가되어 작동하지 않습니다 배경 타이머 .IN 실행 배경이 코드를 사용하게하십시오 좋은 대답, 내 문제가 완벽하게 해결되었습니다 ... 다시 감사 ... –

+0

환영 친구 ------ – Tendulkar

+0

bgTask는 무엇입니까? 말해주십시오 .. – Mani

관련 문제