ios实现程序切入后台,实现后台任务

- (void)applicationDidEnterBackground:(UIApplication *)application

{

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

NSAssert(self->bgTask ==
UIBackgroundTaskInvalid, nil);

self->bgTask = [application
beginBackgroundTaskWithExpirationHandler: ^{

// Synchronize the cleanup call on the main thread in case

// the task catully finished at around the same time.

dispatch_async(dispatch_get_main_queue(), ^{

if (UIBackgroundTaskInvalid !=
self->bgTask) {

[application
endBackgroundTask:self->bgTask];

self->bgTask =
UIBackgroundTaskInvalid;

}

});

}];

// Start the long-running task and return immediately.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0),

^{

// Do the work assoicated with the task.

// Synchronize the cleanup all on the main thread in case

// the task catully finished at around the same time.

dispatch_async(dispatch_get_main_queue(), ^{

NSLog(@"Background Time:%f",[[UIApplication
sharedApplication] backgroundTimeRemaining]);

if (UIBackgroundTaskInvalid !=
self->bgTask) {

[application
endBackgroundTask:self->bgTask];

self->bgTask =
UIBackgroundTaskInvalid;

}

});

});

}

ios实现程序切入后台,实现后台任务

时间: 2024-08-05 04:21:58

ios实现程序切入后台,实现后台任务的相关文章

ios实现程序切入后台,实现后台任务 (转自)

1 1,项目需求,是程序home键切入后台,3分钟后退出登陆, 2 3 首先,iOS 会再持续切入后台,给我们5秒钟的时间去处理相关数据,5秒后,程序不会再执行任何代码,处于挂起状态. 4 5 6 7 8 9 - (void)applicationDidEnterBackground:(UIApplication *)application { 10 11 //切入后台 ,申请后台任务时间, 12 13 UIApplication *app = [UIApplication sharedAppl

原创翻译:iOS 应用程序 蓝牙后台运行

默认情况下,普通的CoreBluetooth 任务中的大多数,无论是Central还是peripheral ,在后台或者挂起状况下都是无法进行的.也就是说,你可以通过宣布你的应用程序支持后台处理模式来允许你的一个应用程序把你从一个挂起状态中唤醒来执行某些蓝牙相关的事件. 即使你的应用程序支持一种或者两种CoreBluetooth后台执行模式,那么他也不会永远运行.在某些时候,系统也许由于当前的前台应用程序而终止你的应用程序来释放内存,这将会导致任何活跃的或者挂起的连接会丢失.在iOS7中,Cor

ios开发-程序压后台后,悄悄的抓取数据~~

我们使用某个app的时候,当我们将程序压到后台之后,我们希望它还能从服务器抓取一些数据,类似微博,微信,qq这些程序压后台 之后,我们依然能看到icon上显示未读数量.但是ios系统是伪多任务操作系统. 当我们将程序压后台之后,大概过1分钟,程序就会被关闭.就不能抓取数据了.很久之前的做法是:压后台之后,播放一个没有声音 的音频,保证程序存活.然后苹果很快发现了这种方法,并禁止了.我们用这种方法提交审核之后,一般都会被苹果驳回.当然,苹果 依然很贴心的给我提供了相应的解决办法.(使用swift演

IOS 后台挂起程序 当程序到后台后,继续完成Long-Running Task 任务

我们知道,到我们程序从前台退到后台(安home)键后,将执行程序的委托方法. // 当应用程序掉到后台时,执行该方法 - (void)applicationDidEnterBackground:(UIApplication *)application { } 我们已经知道: 当一个 iOS 应用被送到后台,它的主线程会被暂停.你用 NSThread 的 detachNewThreadSelector:toTar get:withObject:类方法创建的线程也被挂起了. 我们假设有这么一种情况:

iOS后台挂起程序 当程序到后台后,继续完成Long-Running Task 任务

我们知道,到我们程序从前台退到后台(安home)键后,将执行程序的委托方法. // 当应用程序掉到后台时,执行该方法 - (void)applicationDidEnterBackground:(UIApplication *)application { } 我们已经知道: 当一个 iOS 应用被送到后台,它的主线程会被暂停.你用 NSThread 的 detachNewThreadSelector:toTar get:withObject:类方法创建的线程也被挂起了. 我们假设有这么一种情况:

学习IOS开发项目篇--如何让程序在后台保持挂起状态

程序的状态分为:前台运行,后台挂起,后台休眠,为了让项目的网络请求保持活跃状态,需要对程序进行设置. 在applicationDidEnterBackground方法中调用下面的方法,可以让程序进入挂起状态,但在未知时间内,可能会被系统设置为休眠,如果在将程序设置为播放器,并且循环播放一个MP3文件,可以保持永久挂起状态. UIBackgroundTaskIdentifier task =[application beginBackgroundTaskWithExpirationHandler:

iOS程序进入后台后仍运行定时器NSTimer

由于本应用需要在应用进入后台后还要进行定时的检测功能,因此对于我来说怎样让APP在进入后台后 保持运行状态是比较大的需求.然后在iOS系统中是很难实现的,不管是 通过 音频还是 定位系统,我查找了一些资料后都是只能申请到十分钟的运行态.没有所谓的长期运行的概念..... 然而在博客中突然看到这篇文章,尝试了下竟然可以运行很久.不管怎么样我还是先试试了......至于苹果是否能审核通过估计又是个难是 具体如下: 本文所讲为其中之一:iOS程序进入后台后十分钟之内就会被系统kill掉,怎么解决呢?我

iOS 程序进入后台,包含用户上拉快捷菜单导致程序失去活跃的研究

今日在使用某App时候,突然发现上拉菜单.程序视频扔在播放,咦!引起了我的兴趣. 首先,列出两个方法, 第一个方法是AppDelegate的代理.当程序进入后台时候调用 - (void)applicationDidEnterBackground:(UIApplication *)application NS_AVAILABLE_IOS(4_0); 第二个方法是.注冊系统通知,name是UIApplicationWillResignActiveNotification,当程序释放活跃的时候调用.

iOS开发之使程序在后台运行

方法一(此方法不太可靠): 开启程序后台运行: [application beginBackgroundTaskWithExpirationHandler:^{ //后台运行过期后会调用此block内代码 }]; 上面在后台开启任务让程序在后台持续保持运行状态,但能保持运行的时间是不确定.比如内存不足的时候系统可能停止其在后台运行. 方法二: 欺骗系统,在Info.plist中声明自己的应用类型为audio.在后台播放mp3.使其能长期在后台运行.