在后台和 Nstimer 中工作时出现问题?

发布于 2024-12-01 12:31:28 字数 1554 浏览 2 评论 0原文

您好,我有一个任务是使用 Nstimer 和后台进程开发应用程序。

我已经用计时器实现了后台进程。它执行得很好。但是当我第一次最小化应用程序时,我遇到了问题,当时它没有运行后台进程。尽量减少涂抹 3 至 4 次后。之后就可以顺利工作了。我还显示后台任务和计时器的代码如下。

 - (void)applicationDidEnterBackground:(UIApplication *)application {

    UIApplication*    app = [UIApplication sharedApplication];
    NSLog(@"Application enter in background");
    [NSTimer scheduledTimerWithTimeInterval:2.0f
                                     target:self
                                   selector:@selector(updateCounter:)
                                   userInfo:nil
                                    repeats:YES]; 
}

我的 updateCounter 方法如下:

    - (void)updateCounter:(NSTimer*)timer {

    NSString *id = [[UIDevice currentDevice] uniqueIdentifier];
    NSLog(@"uniqueid:%@",id);

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation];

    CLLocation *location = [locationManager location];

    // Configure the new event with information from the location
    CLLocationCoordinate2D coordinate = [location coordinate];

    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude]; 
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];

    NSLog(@"dLatitude : %@", latitude); 
    NSLog(@"dLongitude : %@",longitude);

}

是否有任何问题相关代码请帮助我解决它。

Hi i have atask to develop the application with Nstimer and background process.

I have already implement background process with timer. And it is excuting good.but i have problem when i minimize application first time at that time it is not running the background process. after minimizing application 3 to 4 times. After that it is working smoothly. i also display the code of background task and timer as follow.

 - (void)applicationDidEnterBackground:(UIApplication *)application {

    UIApplication*    app = [UIApplication sharedApplication];
    NSLog(@"Application enter in background");
    [NSTimer scheduledTimerWithTimeInterval:2.0f
                                     target:self
                                   selector:@selector(updateCounter:)
                                   userInfo:nil
                                    repeats:YES]; 
}

And My updateCounter method is as given follow:

    - (void)updateCounter:(NSTimer*)timer {

    NSString *id = [[UIDevice currentDevice] uniqueIdentifier];
    NSLog(@"uniqueid:%@",id);

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation];

    CLLocation *location = [locationManager location];

    // Configure the new event with information from the location
    CLLocationCoordinate2D coordinate = [location coordinate];

    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude]; 
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];

    NSLog(@"dLatitude : %@", latitude); 
    NSLog(@"dLongitude : %@",longitude);

}

Is their any problem related code Please help me to solve it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

停滞 2024-12-08 12:31:28

当应用程序处于后台状态时,NSTimer 会暂停。

您必须启动一些后台任务才能完成您想要的操作。但即便如此,应用程序置于后台后,您仍将受到一定时间的限制。

真正的后台行为仅适用于位置跟踪、VoIP 或音频应用程序。
其他应用程序必须面临限制:一旦进入后台,您就会有一定的时间来完成通过 beginBackgroundTaskWithExpirationHandler: (backgroundTimeRemaining) 启动的任务。

整个事情在iOS应用程序编程指南中描述,在后台执行代码,特别是这里

NSTimer are paused when the app is in background state.

You'll have to start some background task to do what you want. But even with that, you will be limited to a certain amount of time after the app was put in background.

Real backgrounding behavior is only granted for location tracking, VoIP or Audio apps.
Other apps must face limitations: once in background, you are given an amount of time to complete tasks you start with beginBackgroundTaskWithExpirationHandler: (backgroundTimeRemaining).

The whole thing is described in iOS Application Programming Guide, Executing Code in the Background, especially here.

魂牵梦绕锁你心扉 2024-12-08 12:31:28

首先:当您的应用程序进入后台时,计时器将无法按您的预期工作。
(取决于运行循环的可用性和超时)

从我从您的代码中收集到的信息来看,您似乎希望在应用程序在后台运行时进行位置更新。为此,您应该从此处查看指南:
https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW24

有多种方法可以在后台跟踪用户的位置,
其中一些实际上并不涉及定期跑步
背景:

  • 应用程序可以注册重大位置变化。
    (推荐)重大变化的定位服务提供了
    低功耗方式接收位置数据,强烈推荐用于
    不需要高精度位置数据的应用。有了这个
    服务,仅当用户位置时才会生成位置更新
    发生显着变化;因此,它非常适合社交应用或
    为用户提供非关键的、位置相关的应用程序
    信息。如果应用程序在更新时暂停,
    系统会在后台唤醒它来处理更新。如果
    应用程序启动此服务然后终止,系统
    当新位置变为时自动重新启动应用程序
    可用的。此服务仅在 iOS 4 及更高版本中可用
    包含蜂窝无线电的设备。
  • 申请可以继续
    使用标准定位服务。虽然不是为了跑步
    无限期地在后台,标准定位服务是
    适用于所有版本的 iOS 并提供常规更新
    应用程序正在运行,包括在后台运行时。
    但是,一旦应用程序被暂停或
    终止,并且新的位置更新不会导致应用程序
    被唤醒或重新启动。这种类型的服务适合以下情况:
    位置数据主要在应用程序位于
    前景。
  • 应用程序可以声明自己需要连续
    后台位置更新。需要定期的应用程序
    位置更新,无论是在前台还是后台,都应该添加
    将 UIBackgroundModes 键添加到其 Info.plist 文件中并设置值
    该键指向包含位置字符串的数组。这个选项是
    适用于提供特定服务的应用程序,例如
    导航服务,涉及让用户了解他或
    随时了解她的位置。密钥的存在
    应用程序的 Info.plist 文件告诉系统它应该允许
    应用程序根据需要在后台运行。

你受到鼓舞
使用重大位置变更服务或使用标准
服务节俭。定位服务需要积极使用 iOS
设备的板载无线电硬件。连续运行该硬件
会消耗大量的电力。如果您的应用程序有
不需要向用户提供精确、连续的位置信息
用户,最好使用那些最小化功耗的服务
消耗。这些低功耗服务中最主要的是
iOS 4 中引入的位置更改服务。该服务提供
定期位置更新,甚至可以唤醒后台
应用程序,或重新启动已终止的应用程序来交付它们。

对于需要定期提供更精确位置数据的应用程序
间隔,例如导航应用程序,您需要声明
应用程序作为连续的后台应用程序。这个选项是
可用于真正需要它的应用程序,但它是最少的
理想的选择,因为它大大增加了功耗。

First of all: Timers won't work as you expect when your app goes in background.
(depends on the availability of the run loop and your timeout)

From what i gather from your code, seems you like to have location update when the app is running in background. For this, you should check the guidelines from here:
https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW24

There are several ways to track the user’s location in the background,
some of which do not actually involve running regularly in the
background:

  • Applications can register for significant location changes.
    (Recommended) The significant-change location service offers a
    low-power way to receive location data and is highly recommended for
    applications that do not need high-precision location data. With this
    service, location updates are generated only when the user’s location
    changes significantly; thus, it is ideal for social applications or
    applications that provide the user with noncritical, location-relevant
    information. If the application is suspended when an update occurs,
    the system wakes it up in the background to handle the update. If the
    application starts this service and is then terminated, the system
    relaunches the application automatically when a new location becomes
    available. This service is available in iOS 4 and later, only on
    devices that contain a cellular radio.
  • Applications can continue to
    use the standard location services. Although not intended for running
    indefinitely in the background, the standard location services are
    available in all versions of iOS and provide the usual updates while
    the application is running, including while running in the background.
    However, updates stop as soon as the application is suspended or
    terminated, and new location updates do not cause the application to
    be woken up or relaunched. This type of service is appropriate when
    location data is used primarily when the application is in the
    foreground.
  • An application can declare itself as needing continuous
    background location updates. An application that needs regular
    location updates, both in the foreground and background, should add
    the UIBackgroundModes key to its Info.plist file and set the value of
    this key to an array containing the location string. This option is
    intended for applications that provide specific services, such as
    navigation services, that involve keeping the user informed of his or
    her location at all times. The presence of the key in the
    application’s Info.plist file tells the system that it should allow
    the application to run as needed in the background.

You are encouraged
to use the significant location change service or use the standard
services sparingly. Location services require the active use of an iOS
device’s onboard radio hardware. Running this hardware continuously
can consume a significant amount of power. If your application does
not need to provide precise and continuous location information to the
user, it is best to use those services that minimize power
consumption. Chief among these low-power services is the significant
location change service introduced in iOS 4. This service provides
periodic location updates and can even wake up a background
application, or relaunch a terminated application, to deliver them.

For applications that require more precise location data at regular
intervals, such as navigation applications, you need to declare the
application as a continuous background application. This option is
available for applications that truly need it, but it is the least
desirable option because it increases power usage considerably.

权谋诡计 2024-12-08 12:31:28

检查您的应用程序何时进入后台模式以及何时进入前台,计算此差异并将经过的时间添加到计时器中,以便获得大约的总时间。我也实现了同样的方法,并且最终效果很好。

Check when your app goes to background mode and when it came into foregroud, calculate this difference and add the elapsed time in your timer so that you get an approx total time. I have also implemented the same and its working pretty well in my end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文