2012-03-15 4 views
0

は、私は次のように書いている:私は、すべての5分間の私のタイマーを繰り返したいということ問題が

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

が、私のタイマーは、その理由を見つけることができない、繰り返されていない

することができます任意の私は答えを教えてください。

AppDelegate」 - >「- (void)applicationDidEnterBackground:(UIApplication *)application」の方法でこれを書きました。

+0

私がテストした "[NSTimer scheduledTimerWithTimeInterval:1.0ターゲット:自己セレクタ:@selector(CallGetCounts)のUserInfo:繰り返しゼロ:YES];"また、その時私のタイマーは繰り返されていません – user1179681

+0

どのような種類のオブジェクトが「自己」ですか?それは解放されていますか?それは実際に 'CallGetCounts'メソッドに応答しますか?あなたのコードの一部を見ることができますか? – jtbandes

+0

"AppDelegate" - > " - (void)applicationDidEnterBackground:(UIApplication *)application"メソッドでこれを書きました。 – user1179681

答えて

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は起動しません。バックグラウンドで何かが起きるようにするには、アプリをバックグラウンドで実行するように設定し、実行するタスクを実行する承認された方法の1つを使用する必要があります。実行中のNSTimersはサポートされているタスクの1つではありません。

iOSプログラミングガイド、特にBackground Execution and Multitaskingセクションを確認することをおすすめします。

1

UILocalNotification火災ポップアップボックスのインスタンス(とあなたのアプリを起こさ)、それはあなたが設定した時間ごとにトリガするたびに、あなたが本当に選択した場合、UILocalNotificationHereがS.Oスレッドで議論良いチュートリアルのリンクです。それらがあなたを助けることを願っています。