GCD实现倒计时且后台运行不影响计数
2016-03-29 15:53
495 查看
利用苹果给出的三种类型的程序可以保持在后台运行:音频播放类AVFoundation 在Build Phases添加依赖库
1、步骤一:在Info.plist中,添加"Required background modes"键,value为:App plays audio
or streams audio/video using AirPlay
2、步骤二:
3、步骤三:将以下代码添加到appDelegate文件中的- (void)applicationDidEnterBackground:(UIApplication *)application函数,也可添加到在具体类中注册的应用进入后台后的通知方法
完成以上步骤你会发现,程序进入后台后仍可运行定时器!
附带GCD实现倒计时方法:
__block NSInteger timeout = totalTimeInterval;
// 拿到一个队列
dispatch_queue_t queue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0);
// 创建一个_timer放到队列里
_timer =
dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
0, 0, queue);
// 设置_timer的首次执行时间、执行时间间隔、精确度
dispatch_source_set_timer(_timer,
dispatch_walltime(NULL,
0),
1.0 * NSEC_PER_SEC,
0);
// 设置_timer执行的事件
typeof(self)
__weak wself = self;
dispatch_source_set_event_handler(_timer, ^{
if (timeout <=
0) {
// 倒计时结束,取消_timer
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
// 重置ItemContainer
if (_infiniteLoop) {
[wself setInfiniteCycleFromCurrentTime];
}
else {
[wself reloadItemsWithTimes:nil];
}
// 调用代理事件
if ([wself.delegate respondsToSelector:@selector(RBCountDownViewDidStopCounting:)]) {
[wself.delegate RBCountDownViewDidStopCounting:wself];
}
});
}
else {
// 计算显示时间数组
NSArray *times = [wself
calculateTimesWithTotalTimeInterval:timeout];
dispatch_async(dispatch_get_main_queue(), ^{
[wself reloadItemsWithTimes:times];
});
timeout--;
}
});
// 激活_timer
dispatch_resume(_timer);
参考文章:
http://blog.csdn.net/u013009873/article/details/50979415 http://jingyan.baidu.com/article/d8072ac47d3c00ec94cefd5b.html
1、步骤一:在Info.plist中,添加"Required background modes"键,value为:App plays audio
or streams audio/video using AirPlay
2、步骤二:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. NSError *setCategoryErr = nil; NSError *activationErr = nil; [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr]; [[AVAudioSession sharedInstance] setActive: YES error: &activationErr]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
3、步骤三:将以下代码添加到appDelegate文件中的- (void)applicationDidEnterBackground:(UIApplication *)application函数,也可添加到在具体类中注册的应用进入后台后的通知方法
- (void)applicationDidEnterBackground:(UIApplication *)application{ UIApplication* app = [UIApplication sharedApplication]; __block UIBackgroundTaskIdentifier bgTask; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ dispatch_async(dispatch_get_main_queue(), ^{ if (bgTask != UIBackgroundTaskInvalid) { bgTask = UIBackgroundTaskInvalid; } }); }]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_main_queue(), ^{ if (bgTask != UIBackgroundTaskInvalid) { bgTask = UIBackgroundTaskInvalid; } }); }); }
完成以上步骤你会发现,程序进入后台后仍可运行定时器!
附带GCD实现倒计时方法:
__block NSInteger timeout = totalTimeInterval;
// 拿到一个队列
dispatch_queue_t queue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0);
// 创建一个_timer放到队列里
_timer =
dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
0, 0, queue);
// 设置_timer的首次执行时间、执行时间间隔、精确度
dispatch_source_set_timer(_timer,
dispatch_walltime(NULL,
0),
1.0 * NSEC_PER_SEC,
0);
// 设置_timer执行的事件
typeof(self)
__weak wself = self;
dispatch_source_set_event_handler(_timer, ^{
if (timeout <=
0) {
// 倒计时结束,取消_timer
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
// 重置ItemContainer
if (_infiniteLoop) {
[wself setInfiniteCycleFromCurrentTime];
}
else {
[wself reloadItemsWithTimes:nil];
}
// 调用代理事件
if ([wself.delegate respondsToSelector:@selector(RBCountDownViewDidStopCounting:)]) {
[wself.delegate RBCountDownViewDidStopCounting:wself];
}
});
}
else {
// 计算显示时间数组
NSArray *times = [wself
calculateTimesWithTotalTimeInterval:timeout];
dispatch_async(dispatch_get_main_queue(), ^{
[wself reloadItemsWithTimes:times];
});
timeout--;
}
});
// 激活_timer
dispatch_resume(_timer);
参考文章:
http://blog.csdn.net/u013009873/article/details/50979415 http://jingyan.baidu.com/article/d8072ac47d3c00ec94cefd5b.html
相关文章推荐
- phpcms 调用全站最新发布数据
- oracle12c创建用户
- Nginx-虚拟主机
- xhost命令介绍
- git基本
- 利用 Composer 完善自己的 PHP 框架(一)——视图装载
- Html - a标签如何包裹Div
- HDU 2476 String painter(区间DP)
- 到底什么是消息队列?Java中如何实现消息队列?
- HQL: Hibernate查询语言
- 83. Remove Duplicates from Sorted List
- python的冒泡排序法和快速排序法
- Google Fonts导致WordPress 速度问题
- Swift中函数返回多值问题
- SBC编码
- Unicode下CString和char *之间的互相转换
- Git常用命令
- opengl之数据类型
- 对“仅通过崩溃地址找出源代码的出错行”一文的补充与改进
- Android最方便的推送框架