gpt4 book ai didi

iphone - 在应用程序处于后台时计时器用完后,如何让应用程序发送 GPS 位置?

转载 作者:行者123 更新时间:2023-11-28 22:28:54 26 4
gpt4 key购买 nike

我想在应用程序处于后台时发送用户位置。在应用程序中,用户可以设置一个定时器,从 1 分钟到 24 小时不等。我曾尝试让该应用程序在后台运行,但它只运行了十分钟。如果我将计时器设置为超过十分钟,然后将应用程序置于后台,则该应用程序不会在后台运行超过十分钟。是否可以让应用程序运行超过十分钟?最多至少两个小时?

这是我尝试过的:

我将其添加到我的 plist 中:

enter image description here

我将其添加到我的 applicationDidEnterBackground: 方法中:

    __weak BADAppDelegate * weakSelf = self;
self.backgroundTaskIdentifier =
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

int timeTilDone = timerSeconds;// two hours

self.bgTimer =
[NSTimer timerWithTimeInterval:timeTilDone
target:weakSelf
selector:@selector(startTrackLocation)
userInfo:nil
repeats:NO];

[[NSRunLoop mainRunLoop] addTimer:self.bgTimer
forMode:NSDefaultRunLoopMode];


self.testTimer =
[NSTimer timerWithTimeInterval:60
target:weakSelf
selector:@selector(playSilentSound)
userInfo:nil
repeats:YES];

[[NSRunLoop mainRunLoop] addTimer:self.testTimer
forMode:NSDefaultRunLoopMode];


});

我试图通过每分钟播放一次声音来强制它存活

-(void)playSilentSound
{
if(self.silentSnd == nil)
{
NSString *soundFile;
if ((soundFile = [[NSBundle mainBundle] pathForResource:@"halfsec" ofType:@"caf"]))
{
NSURL *url = [[NSURL alloc] initFileURLWithPath:soundFile];
self.silentSnd = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
self.silentSnd.delegate = self;
[self.silentSnd setNumberOfLoops:1];
}
}

[self.silentSnd prepareToPlay];
[self.silentSnd play];
}

但这并没有帮助。

谢谢!

更新:

这是我的 locationManager 初始化代码:

    NSLog(@"StartTrackLocation");
if(self.locationManager == nil)
{
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.pausesLocationUpdatesAutomatically = NO;
[self.locationManager startUpdatingLocation];

最佳答案

在初始化时将 CLLocationManager 属性 pausesLocationUpdatesAutomatically 设置为 false。
如果设置为 true,Apple 认为用户不需要 GPS 并关闭该服务。

强烈建议您阅读 CLLocationManager Class reference 的每一行在每个主要的 ios 更新(下一个将是 7.0)

(虽然你的问题没有真正记录)

忘记你的方法 beginBackgroundTaskWithExpirationHandler,那不是必需的。

关于iphone - 在应用程序处于后台时计时器用完后,如何让应用程序发送 GPS 位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18063701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com