Research on Automatic Program startup and continuous background running (VOIP and GPS)

Source: Internet
Author: User

1. VoIP
1) First, modify the plist configuration of the application and add the following settings:
Application does not run in Background: No
Required background modes: VoIP
Note: after these configurations are added, the application will automatically run after the program is restarted and unlocked:
Didfinishlaunchingwitexceptions method.
2) Main test code:
-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions
{
_ Block uibackgroundtaskidentifier background_task;
Background_task = [Application beginbackgroundtaskwithexpirationhandler: ^ {
// Open the background to run the task
}];

Static dispatch_queue_t _ queue;
_ Queue = dispatch_queue_create ("sddfgfgwerty456567fre4ghghkjdsfbnjfrtyrt", dispatch_queue_serial );
Dispatch_async (_ queue, ^ {
While (true)
{
// Print the process ID
Nslog (@ "processid = % d ++ ++ currentthread =% @", [ssprocessinfo processid], [[nsthread currentthread] description]);
[Nsthread sleepfortimeinterval: 5]; // wait for 15 min
}
});

Sleep (1 );
Return yes;
}

Note: The program must be tested on a real machine. Use the organizer tool on xcode to observe the log information.
After analyzing the log information, it is found that processid will change after a period of time, indicating that the program will be killed after a period of time, and then the program will automatically restart. Although it achieves the purpose of continuous operation, it does not meet the requirements of continuous operation of the program.

2. GPS
1) First, modify the plist configuration of the application and add the following settings:
Application does not run in Background: No
Required background modes: app registers for location updates
After adding these two items, you can ensure that the program runs in the background.
2) Main test code:
-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( 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 (@ "Unlimited run .................................. .............. ");
// After this branch enters the background, the running time is the same as that of the foreground, Which is unlimited.
[_ 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];
}
}

Note: The program must be tested on a real machine. Use the organizer tool on xcode to observe the log information.
After analyzing the log information, it is found that processid does not change, indicating that the program can continue to run.

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.