zoukankan      html  css  js  c++  java
  • 理解允许定位,音频,网络电话..

    大家都知道我们的程序在后台运行的时间是10分钟,10分钟后便会停止。但是像实时定位,播放音频,以及网络电话这些功能我们需要在后台持续运行。那么我们就要进行相应的设置。

    - (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.  
        */
        
        if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])  
        { //Check if our iOS version supports multitasking I.E iOS 4  
            if ([[UIDevice currentDevice] isMultitaskingSupported])  
            { //Check if device supports mulitasking  
                UIApplication *application = [UIApplication sharedApplication]; 
                //Get the shared application instance  
                  
                __block UIBackgroundTaskIdentifier background_task; 
                //Create a task object  
                  
                background_task = [application beginBackgroundTaskWithExpirationHandler: ^{  
                    /*  
                     当应用程序后台停留的时间为0时,会执行下面的操作
                     (应用程序后台停留的时间为600s,可以通过backgroundTimeRemaining查看)  
                     */  
                    [application endBackgroundTask: background_task];
                     //Tell the system that we are done with the tasks  
                    background_task = UIBackgroundTaskInvalid; 
                    //Set the task to be invalid  
                      
                    //System will be shutting down the app at any point in time now  
                }];  
                  
                // Background tasks require you to use asyncrous tasks  
                  
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
                    //Perform your tasks that your application requires  
                    NSLog(@"time remain:%f", application.backgroundTimeRemaining);                  
                    [application endBackgroundTask: background_task]; 
                    //End the task so the system knows that you are done with what you need to perform  
                    background_task = UIBackgroundTaskInvalid; //Invalidate the background_task  
                });  
            }  
        }    
    }

    修改应用的Info.plist 文件,你需要在Info.plist文件中添加UIBackgroundModes字段,该字段的值是应用支持的所有后台模式,是一个数值类型。目前此数 组可以包含“audio”、“location”和“voip”这三个字符串常量.

  • 相关阅读:
    python-学习笔记之-Day5 双层装饰器 字符串格式化 python模块 递归 生成器 迭代器 序列化
    python学习笔记-day4笔记 常用内置函数与装饰器
    Python学习笔记-Day3-python内置函数
    Python学习笔记-Day3-文件操作
    Python学习笔记-Day3-python函数
    Python学习笔记-Day3-python关键字
    Python学习笔记-Day3-set集合操作
    Python学习笔记-Day2-Python基础之列表操作
    Python学习笔记-Day2-Python基础之元组操作
    Python学习笔记-Day2-Python基础之字典操作
  • 原文地址:https://www.cnblogs.com/LzwBlog/p/5743847.html
Copyright © 2011-2022 走看看