• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

DelphiIOS后台定时器

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

3.这里有一个问题,就是客户端是通过心跳来和服务端保持连接,心跳是由定时器触发的,当我退到后台以后,定时器方法被挂起,那么通过如下设置来在后台运行定时器

beginBackgroundTaskWithExpirationHandler

-
(void)applicationDidEnterBackground:(UIApplication
*)application{

UIApplication*
app = [UIApplication sharedApplication];

__block
UIBackgroundTaskIdentifier bgTask;

bgTask
= [app beginBackgroundTaskWithExpirationHandler:^{

dispatch_async(dispatch_get_main_queue(),
^{

if

(bgTask != UIBackgroundTaskInvalid)

{

bgTask
= UIBackgroundTaskInvalid;

}

});

}];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0),
^{

dispatch_async(dispatch_get_main_queue(),
^{

if

(bgTask != UIBackgroundTaskInvalid)

{

bgTask
= UIBackgroundTaskInvalid;

}

});

});

}

 

2

- (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.
    
    testPeripheral = [[ViewController sharedInst] userGetCurrentPeri];
    
    NSLog(@"Saving all fobs... testPeripheral:%@", testPeripheral);
    
    /// _scanTimer=nil;
    
    //    NSError *error;
    //    if (![self.managedObjectContext save:&error])
    //    {
    //        NSLog(@"Saving failed: %@", error);
    //    }
    //entryBackgroundMode是定时器呼叫的函数
//    if (!_scanTimer /* &&[USER_DEFAULT boolForKey:KEY_BACKGROUND_OPEN]*/) {
//        NSLog(@"_scanTimer active ...");
//        _scanTimer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(entryBackgroundMode) userInfo:nil repeats:YES];
//        
//        [[NSRunLoop currentRunLoop] addTimer:_scanTimer forMode:NSDefaultRunLoopMode /* NSRunLoopCommonModes */];
//    }
    /// CLLocationManager * manager = [CLLocationManager new];
    
    __block UIBackgroundTaskIdentifier background_task;
    
    background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
        [application endBackgroundTask: background_task];
        background_task = UIBackgroundTaskInvalid;
    }];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        while(TRUE)
        {
            [NSThread sleepForTimeInterval:1];
            
            //编写执行任务代码
            [self entryBackgroundMode];
//entryBackgroundModeReadRSSI是定时器呼叫的函数
// _scanTimer = [NSTimer timerWithTimeInterval:0.10f target:self selector:@selector(entryBackgroundModeReadRSSI) userInfo:nil repeats:NO]; // [[NSRunLoop currentRunLoop] addTimer:_scanTimer forMode:NSDefaultRunLoopMode /* NSRunLoopCommonModes */]; } [application endBackgroundTask: background_task]; background_task = UIBackgroundTaskInvalid; }); }

 

//注入定时器  
   NSError *setCategoryErr = nil;  
   NSError *activationErr  = nil;  
   [[AVAudioSession sharedInstance]  
    setCategory: AVAudioSessionCategoryPlayback  
    error: &setCategoryErr];  
   [[AVAudioSession sharedInstance]  
    setActive: YES  
    error: &activationErr];  

    - (void)applicationDidEnterBackground:(UIApplication *)application  
    {  
        //后台继续运行定时器  
        UIApplication*   app = [UIApplication sharedApplication];  
        __block    UIBackgroundTaskIdentifier bgTask;  
        bgTask = [app beginBackgroundTaskWithExpirationHandler:^{  
            dispatch_async(dispatch_get_main_queue(), ^{  
                if (bgTask != UIBackgroundTaskInvalid)  
                {  
                    bgTask = UIBackgroundTaskInvalid;  
                }  
            });  
        }];  
          
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
            dispatch_async(dispatch_get_main_queue(), ^{  
                if (bgTask != UIBackgroundTaskInvalid)  
                {  
                    bgTask = UIBackgroundTaskInvalid;  
                }  
            });  
        });  
    }  

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
delphi编程中调用其他执行程序?(转)发布时间:2022-07-18
下一篇:
Delphi集合的用法发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap