use*_*981 8 background scheduling foreground nstimer ios
我在这里因为没有找到任何解决方案我的问题:(
我正在做一个简单的应用程序,我必须将(通过套接字)一些信息发送到服务器(如GPS l/L,精度,电池电量等).
当应用程序在前台时,当前代码工作正常.
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];
Run Code Online (Sandbox Code Playgroud)
调用所有方法.
但是当应用程序进入后台时,所有计时器都无效...我已经读过iOS自动停止计时器..我正在寻找一种方法来调用方法并在应用程序处于后台时发送数据.
我需要你的帮助 :)
谢谢大家 !!
ban*_*isa 11
您需要阅读有关如何在后台运行任务的指南:
这是我的一个应用程序的applicationDidEnterBackground.当我把它放到后台时,它会进行一些磁盘缓存维护:
- (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
});
}
}
Run Code Online (Sandbox Code Playgroud)
}
来自Apple文档:
实现长时间运行的后台任务对于需要更多执行时间来实现的任务,您必须请求特定权限才能在后台运行它们而不会被挂起.在iOS中,只允许在后台运行特定的应用类型:
- 在后台播放用户可听内容的应用,例如音乐播放器应用
- 随时向用户通知其位置的应用,例如导航应用
- 支持互联网协议语音(VoIP)的应用
- 需要下载和处理新内容的报亭应用程序
- 从外部配件接收定期更新的应用程序
除非你的应用程序属于这些类别中的一个(听起来不是这样),否则它将无法在后台运行.
| 归档时间: |
|
| 查看次数: |
19894 次 |
| 最近记录: |