My app runs an NSTimer that updates the screen, but I want to stop it when the user exits the program and start it up again if necessary when the user comes back in. It seems that -applicationDidEnterBackground
is called when the home button is pushed, but not if the user just locks the screen. I think -applicationDidResignActive
is called on screen lock. Is there a way to use these well? or do I just need put the code in both places. There must be a better way.
我的应用程序运行一个更新屏幕的NSTimer,但我希望在用户退出程序时停止它,并在用户重新进入时再次启动它。似乎在按下主页按钮时调用-applicationDidEnterBackground,但如果用户只是锁定屏幕则不会。我认为-applicationDidResignActive是在屏幕锁定上调用的。有没有办法好好利用这些?或者我只需要将代码放在两个地方。肯定有更好的办法。
1 个解决方案
#1
1
- (void)applicationWillResignActive:(UIApplication *)application
is called whenever your app is interrupted. This includes locking the screen, receiving a phone call or push notification (on iOS 3 or 4), as well as when the application is moved to the background. Putting your code there is sufficient to stop the timer for all cases.
- (void)applicationWillResignActive :( UIApplication *)应用程序在您的应用程序被中断时被调用。这包括锁定屏幕,接听电话或推送通知(在iOS 3或4上),以及应用程序移动到后台时。将代码放在那里就足以在所有情况下停止计时器。
For reactivating your timer, you should use - (void)applicationDidBecomeActive:(UIApplication *)application
, which is called whenever your application is reactivated, including when it is first launched.
要重新激活计时器,您应该使用 - (void)applicationDidBecomeActive:(UIApplication *)应用程序,该应用程序在您的应用程序重新激活时调用,包括首次启动时。
#1
1
- (void)applicationWillResignActive:(UIApplication *)application
is called whenever your app is interrupted. This includes locking the screen, receiving a phone call or push notification (on iOS 3 or 4), as well as when the application is moved to the background. Putting your code there is sufficient to stop the timer for all cases.
- (void)applicationWillResignActive :( UIApplication *)应用程序在您的应用程序被中断时被调用。这包括锁定屏幕,接听电话或推送通知(在iOS 3或4上),以及应用程序移动到后台时。将代码放在那里就足以在所有情况下停止计时器。
For reactivating your timer, you should use - (void)applicationDidBecomeActive:(UIApplication *)application
, which is called whenever your application is reactivated, including when it is first launched.
要重新激活计时器,您应该使用 - (void)applicationDidBecomeActive:(UIApplication *)应用程序,该应用程序在您的应用程序重新激活时调用,包括首次启动时。