if([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground){ [Logger logIntofilenamed:@"log" withContent:[Nsstring stringWithFormat:@" state %ld,background time remaining %.2f",(long)[[UIApplication sharedApplication] applicationState],[[UIApplication sharedApplication] backgroundTimeRemaining ]] andPrettyTime:true]; UIApplication* app = [UIApplication sharedApplication]; __block uibackgroundtaskIDentifIEr task = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:task]; task = uibackgroundtaskInvalID; [Logger logIntofilenamed:@"log" withContent:@" expiration handler executed " andPrettyTime:true]; }]; }
根据文档,如果app在前台,backgroundTimeRemaining的值可能很大:
While the app is running in the foreground,the value in this property
remains suitably large.
但这不是一个例子.我的方法被执行,因为应用程序状态等于UIApplicationStateBackground.
我在这里错过了什么?
A value of backgroundTimeRemaining can be big if app is in the
foreground.
在提供正确的backgroundTimeRemaining之前,applicationDIDEnterBackground需要在状态之间进行一些时间.这应该在~180左右开始.使用dispatch_async在异步线程中运行它并过滤掉Foreground值(> 180).
- (voID)applicationDIDEnterBackground:(UIApplication *)application{ if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { NSLog(@"Multitasking Supported"); __block uibackgroundtaskIDentifIEr background_task; background_task = [application beginBackgroundTaskWithExpirationHandler:^ { //Clean up code. Tell the system that we are done. [application endBackgroundTask: background_task]; background_task = uibackgroundtaskInvalID; }]; //To make the code block asynchronous dispatch_async(dispatch_get_main_queue(),^{ //### background task starts NSLog(@"Running in the background\n"); while(TRUE) { //#### Filter Foreground Values if ([[UIApplication sharedApplication] backgroundTimeRemaining] < 180) { NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]); [NSThread sleepForTimeInterval:1]; //wait for 1 sec } } //#### background task ends //Clean up code. Tell the system that we are done. [application endBackgroundTask: background_task]; background_task = uibackgroundtaskInvalID; }); } else { NSLog(@"Multitasking Not Supported"); }}总结
以上是内存溢出为你收集整理的ios – 为什么backgroundTimeRemaining给出DBL_MAX,即使应用程序状态是UIApplicationStateBackground?全部内容,希望文章能够帮你解决ios – 为什么backgroundTimeRemaining给出DBL_MAX,即使应用程序状态是UIApplicationStateBackground?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)