应用程序进入后台时如何保持NSTimer? [英] How keep NSTimer when application entering background?
问题描述
我来这里是因为我的问题没有找到任何解决方案:(
I'm here because a didn't find any solutions for my issue :(
我正在做一个简单的应用程序,我必须(通过套接字)向服务器发送一些信息(如 GPS l/L、精度、电池电量等).
I'm doing an simple application in which i have to send (by socket) some informations to a server (like GPS l/L, accuracy, Battery level, etc).
当前代码在应用程序处于前台时工作正常.
The current code works fine when application is in foreground.
myTimer = [NSTimer scheduledTimerWithTimeInterval: 2.0 target:self
selector: @selector(sendPosToServer:) userInfo:nil repeats: YES];
myTimer2 = [NSTimer scheduledTimerWithTimeInterval: 3.0 target:self
selector: @selector(sendBatteryToServer:) userInfo:nil repeats: YES];
myTimer3 = [NSTimer scheduledTimerWithTimeInterval: 5.0 target:self
selector: @selector(sendResToServer:) userInfo:nil repeats: YES];
myTimer4 = [NSTimer scheduledTimerWithTimeInterval: 5.0 target:self
selector: @selector(sendQResToServer:) userInfo:nil repeats: YES];
myTimer5 = [NSTimer scheduledTimerWithTimeInterval: 3.0 target:self
selector: @selector(sendPrecisionToServer:) userInfo:nil repeats: YES];
所有这些方法都被调用了.
All thoses methods are called.
但是当应用程序进入后台时,所有计时器都无效......我读过iOS会自动停止计时器......我正在寻找一种在应用程序处于后台时调用方法和发送数据的方法..
But when application enter in background, all timer are invalidate... I've read that iOS automatically stop timers.. I'm looking for a way to call methods and send datas when application is in background..
我需要你的帮助:)
谢谢大家!!
推荐答案
你需要阅读如何在后台运行任务的指南:
You need to read the guide on how to run tasks in the background:
这是我的一个应用程序的 applicationDidEnterBackground.当我把它放到后台时,它会做一些磁盘缓存维护:
Here is my applicationDidEnterBackground for one of my apps. When I put it to the background, it does some disk cache maintenance:
- (void)applicationDidEnterBackground:(UIApplication *)application {
//As we are going into the background, I want to start a background task to clean up the disk caches
if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance
__block UIBackgroundTaskIdentifier background_task; //Create a task object
background_task = [application beginBackgroundTaskWithExpirationHandler: ^{
[application endBackgroundTask:background_task]; //Tell the system that we are done with the tasks
background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
//System will be shutting down the app at any point in time now
}];
//Background tasks require you to use asyncrous tasks
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Perform your tasks that your application requires
//I do what i need to do here.... synchronously...
[application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
});
}
}
}
这篇关于应用程序进入后台时如何保持NSTimer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!