简体   繁体   English

在iPhone中创建递归后台处理程序是否合法?(当应用进入后台时进行处理)

[英]Is it legal to create a recursive background handler in iphone?(processing when app enters the background)

If I register an app for background app in did enter background and make a background handler with approximately 10 mins to finish the task, after the time is over the handler will call finishing block and in this block again create the background handler you will get again 10 mins and so on, in this way application will remain in background. 如果我为后台应用程序注册了一个应用程序,但确实输入了背景,并用大约10分钟的时间使后台处理程序完成任务,则时间结束后,该处理程序将调用Finishing块,并在此块中再次创建后台处理程序,您将再次获得10分钟等,这样应用程序将保留在后台。 I mean is it OK to do that....? 我的意思是这样做....? or will apple object to this? 还是苹果会反对?

From the documentation : 文档中

Applications running background tasks have a finite amount of time in which to run them. 运行后台任务的应用程序在有限的时间内运行它们。 (You can find out how much time is available using the backgroundTimeRemaining property.) If you do not call endBackgroundTask: for each task before time expires, the system kills the application. (您可以使用backgroundTimeRemaining属性找出有多少时间可用。) 如果您没有在时间到期之前为每个任务调用endBackgroundTask :,系统将终止该应用程序。

So, no, you can't indefinitely run in the background. 因此,不,您不能无限期在后台运行。

Yeah thats correct, you cannot run infinitely in this way. 是的,没错,您不能以这种方式无限运行。 But i have found one more trick, when applicatuon enters background, start playing an audio with 0 volume :-) Your app will never get killed. 但是我发现了另一个技巧,当应用程序进入后台时,开始播放0音量的音频:-)您的应用程序将永远不会被杀死。

There is a VoIP app, Media5, which can receive in background incoming calls using UDP sockets. 有一个VoIP应用程序Media5,可以使用UDP套接字在后台接收来电。

Developers said they used a "trick" to mantain the app active forever and I'd exclude the silent audio playing option. 开发人员表示,他们使用“技巧”使该应用程序永久保持活动状态,而我将排除无声音频播放选项。 So the question is: what's that trick? 所以问题是:那是什么把戏?

Also Bria can receive with UDP in background. Bria也可以在后台使用UDP接收。

From Comment 20 at Issue 515: Background app support for iPhones with multitasking support : 摘自第515期的评论20 :具有多任务支持的iPhone的后台应用程序支持

I'm pretty sure that without playing continuously an audio file or logging GPS position you cannot keep alive UDP listening sockets in iOS 4.3+ (both in the main thread or in a secondary one). 我非常确定,如果不连续播放音频文件或记录GPS位置,则无法在iOS 4.3+(主线程或辅助线程)中保持活动的UDP侦听套接字。 If you play an audio play with AVAudioPlayer (after initializing an AudioSession before) in a nsrunloop every 5 seconds, the main thread is kept active and moreover it's NOT necessary to declare the audio background support in info.plist. 如果您每隔5秒钟在nsrunloop中使用AVAudioPlayer(在初始化AudioSession之前)播放音频播放,则主线程将保持活动状态,并且无需在info.plist中声明音频背景支持。

I think this is the "trick" used by Media5 and Bria. 我认为这是Media5和Bria使用的“技巧”。 I tried also creating a infinite TCP stream to the loopback interface declaring it as VoIP, just to see if also UDP socket is kept alive. 我还尝试向回送接口创建一个无限的TCP流,将其声明为VoIP,以查看UDP套接字是否也保持活动状态。 The answer is no, only TCP sockets works while in background (and with screen locked), UDP is closed in the meanwhile and cannot listen for incoming calls anymore. 答案是否定的,只有TCP套接字在后台(且屏幕锁定)时才起作用,同时UDP已关闭并且无法再监听传入的呼叫。

So the key point is that the main thread must be kept active... using a simple nstimer o infinite runloop alone is useless, since the watchdog kills the process after few seconds in background (except if you work in debug mode with GDB interface attached, it runs forever). 因此,关键点在于必须使主线程保持活动状态...仅使用简单的nstimer o无限运行循环是没有用的,因为看门狗会在后台运行几秒钟后杀死该进程(除非您在连接了GDB接口的调试模式下工作) ,它将永远运行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM