(Original) iOS executes it at intervals in the background

Source: Internet
Author: User
Tags set background

Steps:

1. Add the uibackgroundmodes key to info. plist. The value is an array and one of the arrays is a VoIP string:

<Key>Uibackgroundmodes</Key><Array> <string>VoIP</String> </array>

2. InProgramCall at startup -(Void) Setupbackgroundhandler function. The function body is as follows:

# Pragma mark-VoIP-(void) setupbackgroundhandler {If (callback () {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) implements {uiapplication * APP = [uiapplication sharedapplication]; If ([App applicationstate] = uiapplicationstatebackground) {nsarray * oldications ications = [App scheduledlocalications ications]; if ([oldnotifications count]> 0) [App cancelalllocalnotifications]; uilocalnotification * Alarm = [[uiloc Alnotification alloc] init] autorelease]; If (Alarm) {alarm. firedate = [nsdate datewithtimeintervalsincenow: 15]; alarm. timezone = [nstimezone defaulttimezone]; alarm. repeatinterval = 0; alarm. soundname = uilocalnotificationdefasoundname; 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];}

Explanation:

-(Bool) setkeepalivetimeout :( nstimeinterval)TimeoutHandler :( void (^) (void ))Keepalivehandler

Function: the app wakes up every timeout.

0. To successfully call this function, you must set one of the array values of the uibackgroundmodes key in info. plist to a VoIP string.

1. TimeoutRequired& Gt; = 600

2.The interval between wake-up apps is inaccurate.

3. Only 10 seconds after waking up. That is,CodeThe class is executed in 10 seconds. The app is blocked again 10 seconds later.

(You can use-BackgroundtimeremainingAttribute to return the remaining time)

4. The function is valid within the life cycle of the program after it is successfully called.

This function is still effective when you return to the foreground. (So you can use it as a timer .)

5. clearkeepalivetimeoutThe function is used to clear handler.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.