码迷,mamicode.com
首页 > 其他好文 > 详细

程序自启动及后台持续运行的研究(voip和GPS方式)

时间:2014-07-28 19:16:14      阅读:5261      评论:0      收藏:0      [点我收藏+]

标签:des   io   for   art   cti   代码   ar   时间   

1,voip方式
    1)首先要修改应用的plist配置,添加下面的设置:
        Application does not run in background: NO
        Required background modes: VOIP
    注:添加这些配置之后,程序重启解锁就会自动运行application:              
                    didFinishLaunchingWithOptions方法。
    2)主要测试代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
     __block UIBackgroundTaskIdentifier background_task;
    background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
        //打开后台运行任务
    }];
   
    static dispatch_queue_t _queue ;
    _queue = dispatch_queue_create("sddfgfgwerty456567fre4ghghkjdsfbnjfrtyrt", DISPATCH_QUEUE_SERIAL);
    dispatch_async(_queue, ^{
        while(TRUE)
        {
            //打印进程id
            NSLog(@"processID = %d++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++CurrentThread = %@",[SSProcessInfo processID],[[NSThread currentThread] description]);
            [NSThread sleepForTimeInterval: 5]; //wait for 15 min
        }
    });
    
    sleep(1);
    return YES;
}

注:程序测试必须真机下,观察log信息用Xcode上自带的Organizer工具。
经过分析log信息,发现processID过一段时间就会变,说明程序运行一段时间就会被kill掉,然后程序会自动重启。虽然达到了持续运行的目的,但是不符合程序持续运行要求。

2,GPS方式
    1)首先要修改应用的plist配置,添加下面的设置:
    Application does not run in background: NO
    Required background modes: App registers for location updates
    添加这两项之后可以保证程序在后台运行
    2)主要测试代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([CLLocationManager significantLocationChangeMonitoringAvailable])
     {
        [self.locationManager startUpdatingLocation];
      }
   else
     {
         NSLog(@"Significant location change monitoring is not available.");
     }
     return YES;
}

- (CLLocationManager *)locationManager
{
    
    if (_locationManager == nil) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        
        if([CLLocationManager locationServicesEnabled])
        {
               NSLog(@"无限运行................................................");
            //这个分支进入后台后,运行时间和前台一样,是无限的
            [_locationManager startMonitoringSignificantLocationChanges];
            _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
            _locationManager.distanceFilter= 1;
            _locationManager.pausesLocationUpdatesAutomatically = NO;
            _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
            [_locationManager startUpdatingLocation];
       }
    }
  return _locationManager;
    
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    while (true) {
        [NSThread sleepForTimeInterval:1];
            NSLog(@"processID = %d++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++CurrentThread = %@",[SSProcessInfo processID],[[NSThread currentThread] description]);
        [self.locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:10];
    }
}

注:程序测试必须真机下,观察log信息用Xcode上自带的Organizer工具。
经过分析log信息,发现processID不会变,说明程序能够持续运行。

程序自启动及后台持续运行的研究(voip和GPS方式),布布扣,bubuko.com

程序自启动及后台持续运行的研究(voip和GPS方式)

标签:des   io   for   art   cti   代码   ar   时间   

原文地址:http://www.cnblogs.com/huntaiji/p/3873100.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!