UIApplication delegate information (multi-task)

Source: Internet
Author: User

 

When an application enters the background, it can obtain a certain amount of time to run related tasks, that is, it can run for a short period of time in the background.

Three other types can be run in the background,
1. Music 2. location 3. voip

Most applications enterBackgroundThe status will soon be suspended. In this state, the application does not execute any code and may delete it from the memory at any time. Applications provide specific services that users can requestBackgroundExecution time to provide these services.

Determine whether multithreading is supported

  1. UIDevice * device = [UIDevice currentDevice];
  2. BOOL backgroundSupported = NO;
  3. If ([device respondsToSelector: @ selector (isMultitaskingSupported)])
  4. BackgroundSupported = device. multitaskingSupported;

 

Declare the background task you need

Add the UIBackgroundModes key value to Info. plist. It contains one or more string values, including

Audio: provides sound playing functions in the background, including audio streams and sounds during video playback.

Location: the user's location information can be maintained in the background.

Voip: Use the VOIP function in the background

Each of the preceding values let the system know that your application should be awakened when appropriate. For example, an application that starts playing music and then moves to the background still needs to execute time to fill the audio output buffer. The audio key is added to tell the system framework that the audio needs to be played continuously and the application can be called back at an appropriate interval. If this option is not included in the application, any audio playback stops running after it is moved to the background.

In addition to the key value adding method, IOS also provides two ways for applications to work in the background:

Task completion-the application can apply for additional time from the system to complete the specified Task.

Local communications-applications can schedule time for local communications transfers

 

Task completion:

IOS is not a real multi-task system. After you press the Home button, all applications enter the background status and most of the applications immediately enter the paused status, all the application's working memory is in RAM, and it is not executed at all when it is paused. Therefore, switching back to such an application is very fast. However, if the system requires more memory for the currently active applications, it is possible to end the paused applications and their memory will be released.

On the one hand, when an application enters the background state, it needs to release some resources to make its pause snapshot smaller, thus reducing the risk of clearing from RAM. On the other hand, to avoid the loss of user data due to termination, you need to save the progress information when the user leaves. These tasks need to be completed within five seconds, otherwise, the system determines that an exception is forced to exit. The processing may be triggered by receiving the notification sent by the application (UIApplicationDidEnterBackgroundNotification). If you add this statement to the processing code, it will inevitably cause an exception to exit:

[NSThread sleepForTimeInterval:10];

You can use one method to request more background time to avoid this problem. Assume that the processing method triggered by receiving the notification is applicationDidEnterBackground:

-(Void) applicationDidEnterBackground {
NSLog (@ "% @", NSStringFromSelector (_ cmd ));

// Obtain the UIApplication object of the current application.
UIApplication * app = [UIApplication sharedApplication];

// A background task identifier
UIBackgroundTaskIdentifier taskID;
TaskID = [app beginBackgroundTaskWithExpirationHandler: ^ {
// If the system thinks we have been running for too long, it will execute this block and stop running the application.
[App endBackgroundTask: taskID];
}];
// UIBackgroundTaskInvalid indicates that the system does not provide us with additional information
If (taskID = UIBackgroundTaskInvalid ){
NSLog (@ "Failed to start background task! ");
Return;
}
NSLog (@ "Starting background task with % f seconds remaining", app. backgroundTimeRemaining );
[NSThread sleepForTimeInterval: 10];
NSLog (@ "Finishing background task with % f seconds remaining", app. backgroundTimeRemaining );
// Tell the system that we have finished
[App endBackgroundTask: taskID];
}

Local communications:

The UILocalNotification class provides a way to pass local communications. Unlike push notifications, remote servers must be configured. local communications are arranged in the program and executed on the current device. This capability can be used if the following conditions are met:

1. A time-based program allows the program to post an alert at a specific time in the future, such as an alarm clock.

2. A program running in the background, post a local notification to attract user attention

To arrange the transmission of local notification, you need to create a UILocalNotification instance, set it, and use the UIApplication class method to arrange it. The Local notification object contains the type (sound, alert, or badge) to be passed and the time to be presented ). The UIApplication method provides options to determine whether to transfer immediately or at the specified time.

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.