2012-03-15 51 views
0

我寫這樣的:問題用的NSTimer的iOS SDK

[NSTimer scheduledTimerWithTimeInterval:900 target:self selector:@selector(CallGetCounts) userInfo:nil repeats:YES]; 

這意味着我不想重複我的定時器,每5分鐘,但我的計時器不重複,不能夠找到原因

可以在任何請告訴我答案。

我在「AppDelegate」 - >「- (void)applicationDidEnterBackground:(UIApplication *)application」中寫了這個方法。

+0

i相測試 「[的NSTimer scheduledTimerWithTimeInterval:1.0目標:自選擇器:@selector(CallGetCounts)USERINFO:無重複:YES];」也在那個時候,我的計時器不重複 – user1179681 2012-03-15 09:59:03

+0

什麼樣的對象是「自我」?它被釋放了嗎?它實際上是否響應'CallGetCounts'方法?我們可以看到你的一些代碼嗎? – jtbandes 2012-03-15 10:00:34

+0

我在「AppDelegate」 - >「 - (void)applicationDidEnterBackground:(UIApplication *)application」方法中寫了這個。 – user1179681 2012-03-15 10:02:06

回答

2

在後臺中,您的代碼將被暫停,並且在您的應用程序再次到達前臺之前不會收到計時器事件。

您應該在預定的時間間隔後進行預定的本地通知,以便從後臺喚醒。但是,他們會向用戶顯示首先必須接受的彈出式窗口或橫幅。

下面是關於如何做到這一點的一些步驟:

// When you want to schedule: 
UILocalNotification* localNotification = [[[UILocalNotification alloc] init] autorelease]; 
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; // seconds 
localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
localNotification.alertBody = @"Body text"; 
localNotification.alertAction = @"Button text"; 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

// when it's fired it will call your AppDelegate's didReceiveLocalNotification 
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)localNotification 
{ 
    // you can handle timer event here and eventually re-schedule your local notification 
} 
1

通常當應用程序進入後臺時,它會被掛起,因此根本不會執行。特別是,NSTimers不會開火。如果您希望在後臺執行某些操作,則需要將應用配置爲在後臺運行,並使用執行您想要執行的任務的已批准方法之一。運行NSTimers不是支持的任務之一。

我建議你閱讀iOS編程指南,特別是Background Execution and Multitasking部分。

1

UILocalNotification實例觸發彈出框(和喚醒你的應用程序)時,它觸發按您設定的時間,如果你真的選擇了UILocalNotification然後Here在S.O線程討論的很好的教程鏈接。希望那些會幫助你。