首页 技术 正文
技术 2022年11月15日
0 收藏 436 点赞 4,830 浏览 2235 个字

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

首先我在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 }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,852