如何保持的NSTimer当应用程序中输入的背景? [英] How keep NSTimer when application entering background?

查看:110
本文介绍了如何保持的NSTimer当应用程序中输入的背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里,因为没有找到我的问题的任何溶液:(

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).

目前的code正常工作时,应用程序在前台。

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自动停止计时器..
我正在寻找一种方式来调用方法,当应用程序在后台发送DATAS ..

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:

<一个href=\"http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW20\">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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆