我后来是这么解决不知道行不行,能够长期的在后台执行

首先我在xx-info.plist 里的 "Required background modes" 里增加"App provides Voice over IP services"

然后在delegate里增加下面代码,原理是进入后台时程序会在600秒那样结束任务,我做的就是在结束任务前新开一个任务。再结束旧任务,这样就一直的在后台执行,希望可能帮助到很多其它的人。我也查了非常久才找到这种方法的。

UIBackgroundTaskIdentifier
backgroundTaskIdentifier;
02 UIBackgroundTaskIdentifier
oldBackgroundTaskIdentifier;
03  
04 -
(BOOL) isMultitaskingSupported{
05      
06     BOOL
result = NO;
07      
08     if ([[UIDevice
currentDevice]
09           
10          respondsToSelector:@selector(isMultitaskingSupported)]){
result = [[UIDevice currentDevice] isMultitaskingSupported];
11          
12     }
13      
14     return result;
15      
16 }
17  
18 -
(
void)
timerMethod:(NSTimer *)paramSender{
19     count++;
20     if (count
% 500 == 0) {       
21         UIApplication
*application = [UIApplication sharedApplication];
22          
23         //开启一个新的后台
24          
25         backgroundTaskIdentifier
= [application beginBackgroundTaskWithExpirationHandler:^{
26    
27         }];
28         //结束旧的后台任务
29         [application
endBackgroundTask:backgroundTaskIdentifier];
30         oldBackgroundTaskIdentifier
= backgroundTaskIdentifier;
31     }
32     NSLog(@"%ld",count);
33 }
34 -
(
void)applicationDidEnterBackground:(UIApplication
*)application
35 {
36     if ([self
isMultitaskingSupported] == NO){
37          
38         return;
}
39     //开启一个后台任务
40      
41     backgroundTaskIdentifier
= [application beginBackgroundTaskWithExpirationHandler:^{       
42     }];
43     oldBackgroundTaskIdentifier
= backgroundTaskIdentifier;
44     if ([self.myTimer
isValid]) {
45         [self.myTimer
invalidate];
46     }
47     self.myTimer
= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod:) userInfo:nil repeats:YES];
48 }
49  
50 -
(
void)applicationWillEnterForeground:(UIApplication
*)application
51 {
52     if (backgroundTaskIdentifier
!= UIBackgroundTaskInvalid){
53         [application
endBackgroundTask:backgroundTaskIdentifier];
54         if ([self.myTimer
isValid]) {
55             [self.myTimer
invalidate];
56         }
57     }
58 }
59  
60 -
(
void)applicationWillEnterForeground:(UIApplication
*)application
61 {
62     if (backgroundTaskIdentifier
!= UIBackgroundTaskInvalid){
63         [application
endBackgroundTask:backgroundTaskIdentifier];
64         if ([self.myTimer
isValid]) {
65             [self.myTimer
invalidate];
66         }
67     }
68 }

iOS 项目一直在后台执行的更多相关文章

  1. IOS 后台执行

    在IOS后台执行是本文要介绍的内容,大多数应用程序进入后台状态不久后转入暂停状态.在这种状态下,应用程序不执行任何代码,并有可能在任意时候从内存中删除.应用程序提供特定的服务,用户可以请求后台执行时间 ...

  2. IOS后台执行机制 与 动作

    当用户按下"Home"键或者系统启动另外一个应用时,前台foreground应用首先切换到Inactive状态,然后切换到Background状态.此转换将会导致先后调用应用代理的 ...

  3. IOS 后台执行 播放音乐

    iOS 4開始引入的multitask.我们能够实现像ipod程序那样在后台播放音频了. 假设音频操作是用苹果官方的AVFoundation.framework实现.像用AvAudioPlayer.A ...

  4. IOS后台执行

    大多数应用程序进入后台状态不久后转入暂停状态.在这种状态下,应用程序不执行任何代码,并有可能在任意时候从内存中删除.应用程序提供特定的服务,用户可以请求后台执行时间,以提供这些服务. 判断是否支持多线 ...

  5. IOS开发之----详解在IOS后台执行

    文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来运行相关任务,也就是说可以在后台运行一小段时间. 还有三种类型的可以运行在后以,1.音乐2.location 3.voip 文二 ...

  6. IOS的后台执行

    写在前面给大家推荐一个不错的站点  www.joblai.com 本文章由央广传媒开发部 冯宝瑞整理.哈哈 http://www.cocoachina.com/bbs/read.php? tid=14 ...

  7. ios项目里扒出来的json文件

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...

  8. iOS项目开发实战——通过Http Get方式与server通信

    移动client往往须要同后台server进行通信,上传或者下载数据,最经常使用到的方式就是Http Get,如今我们来学习在iOS项目中使用Get方式同server进行通信. [一]server端实 ...

  9. iOS项目Info.plist中关键字汇总

    1. Application does not run in background (键名:UIApplicationExistsOnSuspend) 自从iOS4.0之后,当你在应用程序执行的时候按 ...

随机推荐

  1. Number of Airplanes in the Sky

    Given an interval list which are flying and landing time of the flight. How many airplanes are on th ...

  2. CentOS安装SVN客户端(rpm)

    http://mirrors.163.com/centos/6/os/x86_64/Packages/ 1.检查是已经安装了svn: rpm -qa subversion subversion-1.7 ...

  3. yum安装软件报错:curl#6 - "Could not resolve host: mirrorlist.centos.org; Temporary failure in name resolut

    # yum install -y epel-release Loaded plugins: fastestmirror Repository base is listed more than once ...

  4. Java 把异常传递给控制台

    最简答而又不用写多少代码就能保护异常信息的方法,就是把它们从main()传递到控制台,对于简单的程序可以像这样: package exceptions; //: exceptions/MainExce ...

  5. java innerclasses(内部类)

    可以将一个类定义在另一个类的内部,这就是内部类 创建内部类的方式---把类的定义置于外部类的里面 典型的情况是,外部类将有一个方法,该方法返回一个指向内部类的引用,就像在to()和contents() ...

  6. 解决MySQL忘记root密码

    网上有很多关于忘记MySQL root密码的一些文章,里面都有写怎么去解决,但有时觉得写得太恶心,要么一字不漏的抄别人的,要么就说得不清不楚,好了,不吐槽了,以下是解决的整个过程. 首先我们要知道忘记 ...

  7. 打FFT时中发现的卡常技巧

    题目:洛谷P1919 A*B Problem 加强版 我的代码完全借鉴boshi,然而他380ms我880ms...于是我通过彻底的卡(chao)常(dai)数(ma)成功优化到了380ms,都是改了 ...

  8. 【AtCoder】ARC084

    C - Snuke Festival 对于每个B二分求出几个A比它小记为sum 然后对于每个C就是比它小的B的sum的和 #include <bits/stdc++.h> #define ...

  9. Storm目录树和任务提交过程

    Storm组件本地目录树 Storm zookeeper目录树 Storm任务提交的过程

  10. spark sql中将数据保存成parquet,json格式

    val df = sqlContext.load("/opt/modules/spark1.3.1/examples/src/main/resources/people.json" ...