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.