我想当我的应用程序处于后台时触发 http 请求

发布于 2024-09-25 18:05:09 字数 126 浏览 2 评论 0原文

我想设置一个闹钟,同时当应用程序进入后台时,闹钟应该继续运行,当闹钟时间到期时,我想触发一个http请求。这可能吗?如果是这样我应该使用 ASIHttprequest 吗?如果我使用 ASIHttprequest 我的应用程序会被拒绝吗?

I want to set a alarm in the mean while when app goes background,Alarm should keep on running,When the alarm time expires,I want trigger a http request.Is that possible? If so should I use ASIHttprequest? If I use ASIHttprequest will my app will be rejected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

探春 2024-10-02 18:05:09

我不确定为什么您需要在应用程序处于后台时进行隐形下载。当你的应用程序运行时这样做有什么问题吗?也就是说...

是的,这是可能的。您可以使用多种技巧来保持应用程序运行,而我
n 背景;最简单的是“voip”,前提是您有一个开放的套接字(或者只是一个 UDP 套接字,或者您可能只是告诉它您有一个 keepalive)和“音频”(但您必须保持音频播放,即使您播放的只是静音)。

我不建议使用“voip”,除非您已经测试过电池消耗(如果您仅连接/绑定到本地主机,则可能是可以接受的)。 AFAIK,Skype 使用“voip”;它显着缩短电池寿命(iPhone 4 上大约 16-24 小时;我还没等多久就发现了)。

我不建议使用“音频”——我认为你需要播放一些东西(例如静音)才能让它工作,这可能会停止 iPod 应用程序。它几乎肯定会耗尽电池电量。

如果你想延迟下载超过几分钟,我认为简单的“后台任务”不起作用,因为iOS给了你一个最大时间(除非我错了,它是基于你的应用程序使用的CPU时间) ,但我对此表示怀疑)。您可以使用以下内容进行检查:

-(void)applicationDidEnterBackground:(UIApplication*)application
{
  UIBackgroundTaskIdentifier taskId = [application beginBackgroundTaskWithExpirationHandler:^{NSLog(@"Task expired");}];
  NSTimeInterval backgroundTime = application.backgroundTimeRemaining;
  NSLog(@"Background task for %g seconds ...", (double)backgroundTime);
  [NSThread sleepForTimeInterval:backgroundTime - 1];
  NSLog(@"Background task finishing with %g seconds remaining ...", (double)application.backgroundTimeRemaining);
  [application endBackgroundTask:UIBackgroundTaskIdentifier];
}

请想想儿童电池!

I'm not sure why you need to do the stealth download while your app is backgrounded. What's wrong with doing it while your app is running? That said...

Yes, it's possible. There are various tricks you can use to keep your app running while i
n the background; the easiest are "voip" provided you have an open socket (or just a UDP socket, or you might just tell it you have a keepalive) and "audio" (but you have to keep audio playing, even if all you play is silence).

I do not recommend using "voip" unless you have tested battery drain (it might be acceptable if you only connect/bind to localhost). AFAIK, Skype uses "voip"; it significantly reduces battery life (around 16-24 hours on iPhone 4; I haven't waited long enough to find out).

I do not recommend using "audio" — I think you need to play something (e.g. silence) for it to work, which probably stops the iPod app. It almost certainly drains the battery.

If you want to delay the download for more than a few minutes, I don't think simple "background tasks" will work, since iOS gives you a maximum time (unless I'm wrong and it's based on CPU time used by your app, but I doubt it). You can check with something like this:

-(void)applicationDidEnterBackground:(UIApplication*)application
{
  UIBackgroundTaskIdentifier taskId = [application beginBackgroundTaskWithExpirationHandler:^{NSLog(@"Task expired");}];
  NSTimeInterval backgroundTime = application.backgroundTimeRemaining;
  NSLog(@"Background task for %g seconds ...", (double)backgroundTime);
  [NSThread sleepForTimeInterval:backgroundTime - 1];
  NSLog(@"Background task finishing with %g seconds remaining ...", (double)application.backgroundTimeRemaining);
  [application endBackgroundTask:UIBackgroundTaskIdentifier];
}

Please, think of the children battery!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文