[Android] 15.1 background and foreground tasks, android15.1

Source: Internet
Author: User

[Android] 15.1 background and foreground tasks, android15.1

Category: C #, Android, VS2015;

Created on: 1. Introduction

Android Activities can be in multiple States, depending on users' behaviors and operating system requirements. Although the lifecycle of an Activity changes with the status, such as creating an Activity, transferring it to the background, transferring it to the foreground, and destroying the Activity, in fact, Acivity cannot perform any substantive UI interaction processing in the background. For this reason, the process that Android runs in the background is called a Service. Its essential meaning is that a Service is an instance of a class that runs independently and has no direct relationship with the lifecycle of the Activity. In addition, well-designed applications should not exclusively occupy resources or cause application crashes during the lifecycle of an activity.

1. Backgrounding)

What Android handles in the background (Backgrounding) is actually a compromise solution between the foreground application and the background Service (Service), in other words, through Backgrounding, device resources are not consumed too quickly and frontend requests can be responded as much as possible.

In terms of functionality, background tasks in Android applications are very similar to the multitasking of desktop applications, but they are not a concept of multitasking with desktop applications. Desktop computers generally have a variety of resources available for applications, such as large screens, power supplies, memory, and various external storages. Therefore, these applications can run on the screen one by one, and can maintain high performance. However, the resources of mobile applications are extremely limited, and it is difficult to display multiple high-speed applications on such a small screen at the same time, because to do so, cell phone battery will soon run out.

An Activity in the background state cannot perform any operations. Therefore, it provides an Android Service ). To enable background processing in the Android App, you can call application components to implement the Android Service.

Android services are still part of the created applications, but they run independently in their own lifecycles and are irrelevant to the Activity's lifecycles. This means that even if the user transfers the application to the backend, the service can continue to be executed, even if the operating system stops or all application activities have been destroyed.

For Activities running in the background, Android and iOS have different processing methods. In the Android system, Activities are reflected by the status of transferring in and out of the background. These statuses are part of the Activity lifecycle. The iOS solution is to directly transfer a non-running activity to the background. In addition, to save system resources, the activity will be stopped shortly after it is transferred to the background, that is, these activities that are transferred to the background are no longer allowed to run tasks.

The following overview diagram lists the background technologies that should be used in Android apps. Please carefully study this diagram to help you quickly understand related concepts and technologies you need.

2. Foreground task (Forgrounding)

A foreground task (Forgrounding) is a background Service that is frequently noticed by users on the Android console. The features of these services are that when the memory is too low, it will not automatically become the object to be killed. The foreground task must provide you with a notification that the notification will disappear only after the background service is terminated or removed from the foreground task.

For example, a player that uses the background service to play music should process it as a foreground task, because this allows the user to clearly know the running status of the Background Service through the frontend Notification. In front-end tasks, the notification may indicate the songs being played and allow the user to start an activity to interact with the player.

You can call the StartForeground () method to process your background service as a foreground task (this task is to notify the user of the current status of the Background Service Running on the frontend. This method has at least two parameters: one is the Notification ID (integer), and the other is the Notification object that provides the Notification. You can also add more parameters in this method. For example, you can tell the user once every second or once every minute.

To remove a foreground task, call the StopForeground () method. This method accepts a Boolean parameter, indicating whether to remove the notification at the same time. This method does not terminate the background service. However, if you explicitly terminate the background service, the notification will be removed at the same time.

Note: The StartForeground () and StopForeground () methods are introduced from Android 2.0 (API Level 5. To enable background services to be processed as foreground tasks on earlier platforms, you must use the previously provided SetForeground () method instead of the StartForeground () method.

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.