- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的 iOS 应用程序中的多任务处理似乎有问题。我有一个 NSTimer 正在运行,它会更新屏幕上的一些内容,另一个 NSTimer 只会在定义的时间间隔后触发一次。
问题是:我想对那个火灾事件使用react。我的 App 是否在前台。
我使用以下代码来跟踪计时器:
-(void)backgroundThreadStarted {
NSAutoreleasePool* thePool = [[NSAutoreleasePool alloc] init];
// create a scheduled timer
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(backgroundThreadFire:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
m_isBackgroundThreadToTerminate = NO;
// create the runloop
double resolution = 300.0;
BOOL isRunning;
do {
// run the loop!
NSDate* theNextDate = [NSDate dateWithTimeIntervalSinceNow:resolution];
isRunning = [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:theNextDate];
// occasionally re-create the autorelease pool whilst program is running
[thePool release];
thePool = [[NSAutoreleasePool alloc] init];
} while(isRunning==YES && m_isBackgroundThreadToTerminate==NO);
[thePool release];
}
-(void)backgroundThreadFire:(id)sender {
// do repeated work every one second here
// when thread is to terminate, call [self backgroundThreadTerminate];
}
-(void)backgroundThreadTerminate {
m_isBackgroundThreadToTerminate = YES;
CFRunLoopStop([[NSRunLoop currentRunLoop] getCFRunLoop]);
}
(在此处找到:http://www.cocoadev.com/index.pl?RunLoop)
我在 -(void)applicationDidEnterBackground:(UIApplication *)application
上调用 backgroundThreadStarted
并在 上调用
并且我能够跟踪计时器。所以这很好用。backgroundThreadTerminate
-(void )applicationWillEnterForeground:(UIApplication *)application
不幸的是,回到应用程序后,整个屏幕都是黑色的。我尝试了不同的解决方案,并在谷歌上搜索了很多,但均无济于事。
如果我在看到这个黑屏时最小化应用程序,我可以看到应用程序在背景动画中显示。
我错过了什么?
如果我不执行此 backgroundThread 操作,屏幕显示正常。但是,我无法跟踪计时器。
提前致谢!
最佳答案
您使用的是 AppKit (OS X) 而不是 UIKit (iOS) 的代码。
您应该查看涉及处理 didEnterBackground 的后台 API,并在其中使用 beginBackgroundTaskWithExpirationHandler 创建后台任务:
后台线程不是必需的(它们用于在您的应用程序后台运行的东西,而您的应用程序仍在运行)。后台任务 是一种在应用程序不再位于前台时让常规应用程序代码运行(无 UI 交互)的方式。在本地通知服务之外,您将无法就此轻松地与用户沟通(该 API 更多的是为了在离开应用程序后让上传完成)。并且你的任务被限制在后台后的十分钟内。
关于iphone - applicationWillEnterForeground 后黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5963659/
我正在学习微处理器类(class),但我的代码有问题。 我需要编写一个程序代码,让一个词四处移动。 代码在 (80*25) 屏幕上正常工作(因为我的计算依赖于它) 但问题是:当我最大化显示屏幕时,一切
尝试使用 exoplayer v2.10.5 播放 m3u8 时变黑,这让我添加了或者我如何使用 exoplayer v2.10.5 和 gradle 3.5.3 播放 m3u 或者哪个版本的 exo
有几个现有的例子,但我想要一个使用 complex_filter 来实现目标的命令,而不需要做额外的事情,比如生成空白视频/音频文件。 环顾四周,这是迄今为止我想出的最好的: ffmpeg -i vi
服务是“允许服务与桌面交互”。 unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls,
我有一个显示youtube的本地ios应用程序,直到昨天该应用程序运行良好,但是现在该应用程序显示了youtube,只有当我触摸“播放”而不是播放视频时,它才显示黑屏。 我的应用程序没有更改,因此我认
我正在尝试向我的游戏添加一些基本的 HUD,但是当我尝试更改视角时,我可以看到文本(我之前提到过 HUD),但是在黑屏上。问题出在哪里? gl.glMatrixMode(GL_PROJ
我的转换不起作用,第一个 ImageView 显示了可绘制对象,但在运行动画时它只是以白屏结束。不确定我错过了什么。 主要布局 第二个 Activity 布局 主要 Activity pu
出于某种原因,当我尝试运行此代码时,我得到的只是黑屏。我怎样才能真正让它显示我想要显示的内容? 我正在使用 Eclipse,并且已将 lwjgl.jar 和 lwjgl_utils.jar 添加到构建
我正在遵循有关构建“弹出锁类型”游戏的教程,当我在模拟器中运行它时,会出现黑屏。我之前查过并尝试重置模拟器并重新启动计算机,但它不起作用有人可以帮助我并告诉我为什么会发生这种情况 import Spr
我正在尝试 MrSnakey 游戏。当我运行它时,它没有给我任何错误。但是,模拟器在启动时显示黑屏。你们不知道如何解决吗?解决此问题所需的有关我的项目的任何信息,请告诉我,我会在此处发布。 首先是 l
所以我遇到了相机卡住的问题。当它第一次被调用时,它工作得很好。并在按钮上显示我的图像。但是,如果我回到同一个按钮再次调用相机,或者调用相机的页面上的任何其他按钮,它会启动相机但不是黑屏。相机实际上并没
我几乎阅读了关于这个主题的所有帖子,但似乎没有一个有帮助。 我正在 try catch 当前屏幕的屏幕截图。为此,我正在使用 getDrawingCache。这是我的代码: mRootView.get
我已经用我命名为 Label 的类实现了 CCLabelProtocol 和 CRGBAProtocol: #import @interface Label : CCNode @property
晚上好。我为 iOS 开发了几年,自从 iOS8 发布以来,我遇到了一个奇怪的问题。我确信它会被修复,但现在是 8.1.2,而且它还在发生。在 iOS7 上一切正常,但在运行 iOS8 的手机上发生了
这是我的完整代码。当我右击几次(动画正常工作)时,屏幕卡住。我认为来自 if(SDL_GetTicks() - start_time format, 0, 0xFF, 0xFF ) );
我需要执行以下操作: 用户登录。 重定向至欢迎屏幕。 在加载大量记录时查看欢迎屏幕。 重定向到工作屏幕。 我正在寻找一种在 Action 类中执行类似操作的方法: public class LinkA
我在 KMZ 文件中有一个坐标数据集,我希望能够为用户提供使用 GMSPanoramaView(使用 1.6.0 版本的 Google-Maps-iOS-SDK)查看街景的选项).这是我的代码的样子:
在尝试将应用内 HTML 页面加载到我们的 UIWebView 时,我在 iOS9 上遇到了奇怪的行为 - 有时页面加载,有时只是显示为空白屏幕。 webViewDidFinishLoad 函数也在这
我试图显示我的收藏 View ,但我得到的只是黑屏。我知道我在某处遗漏了一些东西,但我似乎找不到它是什么。 #import "StoreCollectionViewController.h" #imp
我正在尝试从我们的 iOS 应用程序中删除所有 Storyboard,因为在使用 Git 的团队中工作时它们会变得一团糟。 我现在在 AppDelegate 的 application(_:didFi
我是一名优秀的程序员,十分优秀!