IOS在后台每隔一段时间执行一下 资料2

来源:互联网 发布:sass for mac中文版 编辑:程序博客网 时间:2024/05/16 01:53

步骤:

1.在info.plist里加入UIBackgroundModes键,其值为数组,数组之一为voip字符串:

<key>UIBackgroundModes</key><array><string>voip</string></array>

2.在程序启动的时候调用- (void)setupBackgroundHandler函数,函数体如下:

#pragma mark - VoIP
 
- (void)setupBackgroundHandler
{   
    if( UIUDeviceIsBackgroundSupported() )
    {
        if(
           [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler: ^
            {
                [self requestServerHowManyUnreadMessages];
            }
            ]
           )
        {
            UDLog(@"Set Background handler successed!");
        }
        else
        {//failed
            UDLog(@"Set Background handler failed!");
        }
    }
    else
    {
        UDLog(@"This Deviece is not Background supported.");
    }
}
 
- (void)requestServerHowManyUnreadMessages
{
    UIApplication* app = [UIApplication sharedApplication];
     
    if([app applicationState] == UIApplicationStateBackground)
    {
        NSArray * oldNotifications = [app scheduledLocalNotifications];
        if ([oldNotifications count] > 0)
            [app cancelAllLocalNotifications];
        UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];
        if (alarm)
        {
            alarm.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
            alarm.timeZone = [NSTimeZone defaultTimeZone];
            alarm.repeatInterval = 0;
            alarm.soundName = UILocalNotificationDefaultSoundName;
            alarm.alertBody = @"Time to request MOA2 Server!";
            [app scheduleLocalNotification:alarm];
        }
    }
    else if([app applicationState] == UIApplicationStateActive)
    {
        UIAlertView *alertView =  [[[UIAlertView alloc] init] autorelease];
        [alertView setTitle:@"alert"];
        [alertView setMessage:@"Time to request MOA2 Server!"];
        [alertView addButtonWithTitle:NSLocalizedString(@"cancel",nil)];
        [alertView setDelegate:nil];
        [alertView show];
    }
}

解说:


- (BOOL)setKeepAliveTimeout:(NSTimeInterval)timeout handler:(void (^)(void))keepAliveHandler

函数功能:app每隔timeout唤醒一次。

0.要成功调用该函数,就必须在Info.plist里设UIBackgroundModes键的array值之一voip字符串.

1.timeout必须>=600

2.唤醒app的时间间隔是不精准的。

3.唤醒后只有10秒执行时间。即handler里的代码要在10秒类执行完。10秒后app再次被阻塞。

(可以用-backgroundTimeRemaining属性来返回剩余时间

4.该函数成功调用后,在程序生命周期内有效。

该函数的效果在回到前台的状况下,依然有效。(因此可以把它当timer使.) 

5.clearKeepAliveTimeout函数用来清除handler。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小米手机跳电怎么办 手机屏幕翘屏了怎么办 iphone触屏不灵怎么办 平板进水屏幕失灵怎么办 小米平板屏幕失灵怎么办 手机内屏变脏怎么办? 苹果手机屏幕触摸不灵敏怎么办 苹果6触控坏了怎么办 手机触摸屏不好使怎么办 手机触摸屏不好用怎么办 平板触屏不准怎么办 小米手机锁屏键失灵怎么办 三星手机锁屏键失灵怎么办 魅族2屏幕失灵怎么办 屏幕触屏不行怎么办 手机触屏失控怎么办 手机触屏不灵怎么办 电脑触屏不灵敏怎么办 iphone屏坏了怎么办 荣耀手机充电慢怎么办 苹果电池胶拉断怎么办 iphone8耳机丢了怎么办 华为p10进水了怎么办 苹果手机屏幕老是闪怎么办 苹果充电没反应怎么办 6sp开胶了怎么办 夜场ic微信怎么办 手机壳影响信号怎么办 三星开机键失灵怎么办 手机待机键失灵怎么办 手机关机键失灵怎么办 华为开关键失灵怎么办 iphone6plus屏幕碎了怎么办 苹果手机屏幕点不动怎么办 手机屏幕失灵划不动怎么办 苹果手机像素不好怎么办 苹果8指纹不灵敏怎么办 手机摔侧面裂缝怎么办 电脑触摸板进水怎么办 iphone部分屏幕失灵怎么办 苹果屏幕按键不灵怎么办