Four major components of Android, four major components of Android

Source: Internet
Author: User

Four major components of Android, four major components of Android

Service is one of the four major components in Android. A service is a component with no interface. It runs in the background and in the current application process. If there is a time-consuming operation, you do not want to have the interface, and you do not want the program to exit and stop the running logic, put it in the service. It should be noted that the service is also running in the main thread. If time-consuming operations are performed, they should be placed in the sub-thread. if the service is killed by the system, it will be restarted by default. In addition, components can interact with a Service through binding, or even complete inter-process communication. For example, the Service can process network transmission, play music, read and write I/O streams, or interact with content providers in the background. StartService () and bindService () are two ways to enable the Service. startService () cannot call methods in the service or communicate with the service. Once the service is enabled, it will run in the background for a long time. It does not matter if it is enabled. If it is enabled and exited, the service is still running and the methods in the service cannot be called. bindService () the methods in the service can be indirectly called and can communicate with the service process. If the service is enabled, it is bound to the service life cycle. if the service is enabled, it is disabled, the initiator can indirectly call the methods in the service. If the service is enabled and bound at the same time, the service cannot be stopped. You need to unbind the service to stop the service. If a service needs to run in the background for a long time, you also need to call the methods in the service and enable the service in a hybrid manner. That is, the two methods are used, but the calling methods must be strictly ordered. First, enable the service using the start method to ensure that the service can run for a long time in the background. Second, bind the service using the bind method and bind the Service to facilitate the call of methods in the service; before unbinding, unBind and stop the service.

The above is the basic description of the service, and then the specific operations of the Service are implemented. First, you need to clarify the specific operation steps of the service.

The general process for starting a service using startService () is as follows:
1. Write a class to inherit the Service;
2. Override the onCreate () method;
3. Declare service under the configuration file, and write the service package name and class name in the name tag;

BindService:
1. Create a Service class and inherit from the Service;
2. Define an interface to expose the externally provided methods;

public interface IService{     public void callServiceMethed(); }

3. define a proxy object in the service class and define a method that can call the service method indirectly. This can prevent others from calling methods that do not want to be exposed, write the method to be called in the interface. Other methods are not called by other classes;

Private class MyBinder extends Binder implements IService {public void callServiceMethed () {method to call the service }...}

 

4. The proxy object is returned in the onBinder method. If no proxy object is returned, the object obtained by the caller is null.

public IBinder onBind(Intent intent) { return new MyBinder(); }

5. Create a class to implement ServiceConnection and implement the two methods

Private class MyConn implements ServiceConnection {@ Override public void onServiceConnected (ComponentName name, IBinder service) {// call when the service connection is successful} @ Override public void onServiceDisconnected (ComponentName) {// called when the service is disconnected }}

 

6. The activity starts the service by binding, and the bindService () method binds the service;
7. Call the proxy object method and indirectly call the methods in the service

 

Next, let's take a look at the service lifecycle.
Like an Activity, a Service also has a life cycle. What's different is the life cycle of a Service. There can be two different paths from when it is created to when it is destroyed: standard enable mode and binding mode. The following arrows can be used to represent the two lifecycles:

Lifecycle of the standard activation mode:
StartService ()-> onCreate ()-> onstartcommand ()-> onDestroy ()

Lifecycle of the binding mode:
BindService ()-> onBind ()-> onunBind ()-> onDestroy ()

The enabled service is created by calling startService () through other components. This kind of service can run infinitely. You must call the stopSelf () method or other components to stop it by calling the stopService () method. When the service is stopped, the system will destroy it. Use the start method to enable the service. The service is created only once and the onCreate method is executed once. Once the service is created, the onstart and onstartcommand methods will be executed when start is called to enable the Service, when the stop method is called, the service only calls the onDestroy method once.

The Bound service is created by calling bindService () when other components simulate a customer. The customer can communicate with the service through an IBinder interface. You can use the unbindService () method to close the connection. One service can be bound to multiple customers at the same time. When multiple customers are unbound, the system will destroy the service. You can bind a service that has been enabled by calling startService. For example, a background music service may be enabled by calling the startService () method. Later, you may want to control the player or obtain information about the current song. You can use bindService () bind an activity to a service. In this case, stopService () or stopSelf () cannot actually stop this service unless all customers are unbound.

The purpose of understanding the service lifecycle is to better understand the operating principles of the service throughout the project. Therefore, it is necessary to understand the working principles of processes in Android.

In Android, the process priority ranges from high to low and is divided into: Foreground process, Visible process, and Service process ), background process and Empty process ). The following describes the concepts and application scenarios of each process.
Foreground process: the application process that the user is operating on is called the Foreground process. Generally, the system has only a small number of foreground processes at any time. These processes will only be killed as a final means, that is, when the memory is insufficient to continue running them. At this time point, the device has reached the memory paging status. When the system reaches the memory paging status, it can only access the memory through virtual addresses, it can be understood that when the system reaches this state, it will no longer be able to allocate new memory space, so it will kill some foreground processes, it is necessary to release the memory space to ensure that the application can continue to respond to user interaction.

Visualized process: You can no longer operate the application, but you can still see the application interface. A visible process is considered extremely important and generally won't be killed by the system unless it is necessary to ensure that all foreground processes are running.

Service process: The application Service runs in the background. A running Service is started by the startService () method, and the process is not classified as the first two (pre-process and visible process) type, the process is a service process. Although service processes are not directly bound to visible controls, the work of these processes is still of interest to users (such as playing music in the background or downloading data from the network ), therefore, the system keeps these processes running unless the system does not have enough memory to run foreground and visual processes.
Background process: the application interface is minimized by users. An Activity that is invisible to users has been executed by the onStop () method process called the background process. The background process has no direct impact on the user experience, and the system will kill the background process when any foreground process, visible process, or service process requests memory. A large number of background processes are usually run in the system. These background processes are stored in an LRU (least recently used) list, the LRU rule is used to ensure that the Activity process recently used by the user is killed, that is, who was recently used and who was finally killed. If an Activity correctly implements its lifecycle method and saves its state, the state is usually automatically saved by the system, when the system kills the process, there is no visible impact on the user experience, because when the user navigating to the previous Activity, this Activity automatically restores the previously saved view status. View the Activity document to obtain

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.