如何讓App保持在運作狀態

来源:互联网 发布:靠谱助手提示网络异常 编辑:程序博客网 时间:2024/05/19 00:17

转载自:http://fstoke.me/blog/?p=3135

怕自己又忘記找半天,這裡記一下… 很簡單,就一行code:

view sourceprint?
1.[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

可以把它寫在 application:didFinishLaunchingWithOptions: 裡面
這行code主要是把iPhone/iPad內部預設的idle計時關閉,不去計算目前App隔多久使用者沒有任何動作了而讓App進入睡眠(背景)模式



在老江的磁盘清理里有这么一句:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    

    if (indexPath.row ==5 && !self.isCleaning) {

        

        self.isCleaning =YES;

        [[UIApplicationsharedApplication] setIdleTimerDisabled:YES];

        

        dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

        dispatch_async(queue, ^{

            

            NSFileManager *fileManager = [NSFileManagerdefaultManager];

            [fileManager removeItemAtPath:DIR_PATHerror:nil];

            

            //创建目录

            if (![fileManager fileExistsAtPath:DIR_PATH]) {

                [fileManager createDirectoryAtPath:DIR_PATHwithIntermediateDirectories:YESattributes:nilerror:nil];

            }

            

            //第一步

            for (int i =0; i < 1000; i++) {

                

                long long freeSize = [selffreeDiskSpace];

                long long fileSize =0;

                

                if (freeSize > 2 * 1024 * MB)

                    fileSize = 2 * 1024 * MB;

                else if(freeSize >1 * 1024 * MB)

                    fileSize = 1 * 1024 * MB;

                else if(freeSize >512 * MB)

                    fileSize = 512 * MB;

                else if(freeSize >256 * MB)

                    fileSize = 256 * MB;

                else

                    break;

                

                NSString *fPath = [DIR_PATHstringByAppendingFormat:@"fill_one_%d.txt", i];

                [self createFile:fPathwith:fileSize];

            }

            

            //第二步

            for (int i =0; i < 50; i++) {

                

                long long freeSize = [selffreeDiskSpace];

                

                if (freeSize > 0) {

                    

                    NSString *fPath = [DIR_PATHstringByAppendingFormat:@"fill_two_%d.txt", i];

                    [self createFile:fPath with:freeSize];

                }else{

                    

                    sleep(1);

                }

            }

            

            sleep(5);

            

            

            //删除临时文件

            [fileManager removeItemAtPath:DIR_PATHerror:nil];

            

            //返回主线程

            dispatch_async(dispatch_get_main_queue(), ^{

                

                self.isCleaning =NO;

                

                UIAlertView *alertView = [[[UIAlertViewalloc] initWithTitle:@"友情提示" message:@"清理完成" delegate:selfcancelButtonTitle:@"关闭" otherButtonTitles:nil] autorelease];

                [alertView show];

                

                [self.tableViewreloadData];

                

                [[UIApplicationsharedApplication] setIdleTimerDisabled:NO];

                

                

                

            });

        });

    }

    

    [tableView deselectRowAtIndexPath:indexPathanimated:YES];

}


0 0
原创粉丝点击