后台运行之[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]
// 正常程序退出后,会在几秒内停止工作;
// 要想申请更长的时间,需要用到
// beginBackgroundTaskWithExpirationHandler
// endBackgroundTask
// 一定要成对出现 - (void)applicationDidEnterBackground:(UIApplication *)application { [self beginTask];
//在非主线程开启一个操作在更长时间内执行; 执行的动作
aa =0;
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(go:) userInfo:nil repeats:YES];
} -(void)go:(NSTimer *)tim
{
NSLog(@"%@==%ld ",[NSDate date],aa);
aa++;
if (aa==9) {
[_timer invalidate];
[self endBack]; // 任务执行完毕,主动调用该方法结束任务
}
} -(void)beginTask
{
NSLog(@"begin=============");
_backIden = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"begin bgend=============");
[self endBack]; // 如果在系统规定时间内任务还没有完成,在时间到之前会调用到这个方法,一般是10分钟
}];
} -(void)endBack
{
NSLog(@"end=============");
[[UIApplication sharedApplication] endBackgroundTask:_backIden];
_backIden = UIBackgroundTaskInvalid;
}```
后台运行之[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]的更多相关文章
- IOS开发使用GCD后台运行
什么是GCD Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法.该方法在Mac OS X 10.6雪豹中首次推出,并随后被引入到了iOS4.0中.GCD ...
- IOS 后台运行
默认情况下,当app被按home键退出后,app仅有最多5秒钟的时候做一些保存或清理资源的工作.但是应用可以调用UIApplication的beginBackgroundTaskWithExpirat ...
- ios GCD简单介绍 后台运行~
本从实践出发简单说明: 首先,gcd是Grand Central Dispatch的缩写,意为多线程优化技术,是苹果为多核处理优化的技术.使用简单.清晰. 多线程就分同步.异步方法如下: //异步线程 ...
- iOS后台运行播放无声音频 测试可行
如果打回来了,就自认倒霉吧 制作无声音频. @interface AppDelegate () { NSInteger count; } @property(strong, nonatomic)NST ...
- IOS App 后台运行
使用block的另一个用处是可以让程序在后台较长久的运行.在以前,当app被按home键退出后,app仅有最多5秒钟的时候做一些保存或清理资源的工作.但是应用可以调用UIApplication的beg ...
- app额外后台运行操作
//在视图中运行操作中进行周期操作 - (void)applicationDidEnterBackground:(UIApplication *)application { [self beingBa ...
- 计算视图相对坐标时convertPoint:toView: ,UIApplication sharedApplication - keyWindow is nil?
UIWindow *window = [UIApplication sharedApplication].keyWindow; window 为nil的原因:在指定rootViewController ...
- phonegap ios插件开发及无限后台运行解决
1.首先开发插件:因为我的项目前需要所以要做(根据情况) 在项目的plugins文件中新建obj c文件.如 Demo,此时会产生出Demo.h和Demo.m两个文件. .h文件主要就是定义一些方法, ...
- 应用程序添加角标和tabBar添加角标,以及后台运行时显示
1.设置角标的代码: // 从后台取出来的数据可能是int型的不能直接给badgeValue(string类型的),需要通过description转化 NSString *count = [re ...
随机推荐
- MySQL参数max_connect_errors分析释疑
最近一MySQL服务器,由于一些特殊因素遇到"ERROR 1129 (00000): Host 'xxx' is blocked because of many connection e ...
- linux下ffmpeg安装
1.ffmpeg下载地址: http://www.ffmpeg.org/download.html 2.解压 1 $ tar zvfj ffmpeg.tar.bz2 这里作者假设已经重命名为ffmpe ...
- 通过grub-install命令把grub安装到u盘-总结
通过grub-install命令把grub安装到u盘 ①准备一个u盘,容量不限,能有1MB都足够了. ②把u盘格式化(我把u盘格式化成FAT.fat32格式了,最后证明也是成功的).③开启linux系 ...
- SMBus
SMBus (System Management Bus,系统管理总线) 是1995年由Intel提出的,应用于移动PC和桌面PC系统中的低速率通讯.希望通过一条廉价并且功能强大的总线(由两条线组成) ...
- Hibernate中的条件查询完成类
Hibernate中的条件查询有以下三个类完成: 1.Criteria:代表一次查询 2.Criterion:代表一个查询条件 3.Restrictions:产生查询条件的工具类
- JavaScript控制输入框只能输入非负正整数
1.问题背景 问题:一个输入框,输入的是月份,保证输入的内容只能是非负正整数 2.JavaScript代码 function checkMonth() { $("month").k ...
- python version 2. required,which was not found in the registry 解决方案
不能在注册表中识别python2.7 新建一个register.py 文件 import sys from _winreg import * # tweak as necessary version ...
- Python中ValueError: invalid literal for int() with base 10 的实用解决办法
爬虫代理IP由芝麻HTTP服务供应商提供今天在写爬虫程序的时候由于要翻页,做除法分页的时候出现了 totalCount = ' totalPage = int(totalCount)/20 Value ...
- ORA-01940: cannot drop a user that is currently connected解决方法
我们在删除数据库用户时候会碰到如下错误 SQL> DROP USER sys_xj cascade; DROP USER sys_xj cascade*ERROR at line 1:ORA-0 ...
- 一个仿3D的平面游戏页面
package com.totoo.TouhouMassLight;import android.os.Bundle;import android.view.MotionEvent;import an ...