ios - 在 iOS 7 上 backgroundTimeRemaining 少于 10 秒
<p><p>我会定期获得约 9 秒的后台剩余时间。我相信它应该接近180秒。然而,这不是我所看到的。</p>
<pre><code>- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication *app = ;
double secondsToStayOpen = app.backgroundTimeRemaining;
NSLog(@"secondsToStayOpen %f ",secondsToStayOpen);
}
</code></pre>
<p>打印</p>
<pre><code>secondsToStayOpen 179.920931
secondsToStayOpen 9.959715
secondsToStayOpen 9.962670
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>对于 180 年代...您必须“创建”一个 iOS 后台任务。
请修改您的代码:</p> <pre><code>if([ respondsToSelector:@selector(isMultitaskingSupported)])
{
NSLog(@"Multitasking Supported");
__block UIBackgroundTaskIdentifier background_task;
background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
//Clean up code. Tell the system that we are done.
;
background_task = UIBackgroundTaskInvalid;
}];
//To make the code block asynchronous
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//### background task starts
NSLog(@"Running in the background\n");
while(TRUE)
{
NSLog(@"Background time Remaining: %f",[ backgroundTimeRemaining]);
; //wait for 1 sec
}
//#### background task ends
//Clean up code. Tell the system that we are done.
;
background_task = UIBackgroundTaskInvalid;
});
}
else
{
NSLog(@"Multitasking Not Supported");
}
</code></pre>
<p>从这里:<a href="http://hayageek.com/ios-background-task/" rel="noreferrer noopener nofollow">http://hayageek.com/ios-background-task/</a> </p></p>
<p style="font-size: 20px;">关于ios - 在 iOS 7 上 backgroundTimeRemaining 少于 10 秒,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/24542724/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/24542724/
</a>
</p>
页:
[1]