zoukankan      html  css  js  c++  java
  • iOS 程序退出之后

    转自:http://blog.csdn.net/iitvip/article/details/17356299

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
    __block UIBackgroundTaskIdentifier background_task;
    //注册一个后台任务,告诉系统我们需要向系统借一些事件
    background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
    
    //不管有没有完成,结束background_task任务
    [application endBackgroundTask: background_task];
    background_task = UIBackgroundTaskInvalid;
    }];
    
    /*
    尽量用异步,如果像这样 你会发现主线程会被阻塞
    while(TRUE)
    {
    NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
    [NSThread sleepForTimeInterval:1]; //wait for 1 sec
    }
    */
    //异步
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
    //### background task starts
    NSLog(@"Running in the background\n");
    
    static int i = 0;
    while(i < 5)
    {
    NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
    [NSThread sleepForTimeInterval:1]; //wait for 1 sec
    i++;
    }
    /*
    while(TRUE)
    {
    NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
    [NSThread sleepForTimeInterval:1]; //wait for 1 sec
    i++;
    }
    */
    
    //我们自己完成任务后,结束background_task任务
    //如果我们用while(TRUE)的话,下面这两行代码应该不会执行
    [application endBackgroundTask: background_task];
    background_task = UIBackgroundTaskInvalid;
    });
    }
  • 相关阅读:
    基于mybatis的crud demo
    事务的隔离级别
    spring中ioc的实现
    spring中xml配置文件
    spring中AOP的实现
    mybatis框架
    基于Mapreduce的并行Dijkstra算法执行过程分析
    算法技巧:位运算 逻辑运算
    day04_09 while循环03
    day04_08 while循环02
  • 原文地址:https://www.cnblogs.com/bluen/p/3593769.html
Copyright © 2011-2022 走看看