Android Four components

Source: Internet
Author: User

Android Four components are activity,Service,content provider,broadcast receiver, respectively.

First,Android four components of the detailed

1.Activity

(1) An activity is usually a separate screen (window).

(2)communication between activity through intent.

(3)every activity in an Android app must be declared in the androidmanifest.xml configuration file, or the activity will not be recognized or executed by the system .

2.Service

(1)service is used to complete user-specified actions in the background. service is divided into two types:

(a)started (start): When an application component (such as activity) calls the StartService () method to start a service, the service is in a started state.

(B)bound (binding): the service is in bound state when the application component calls the Bindservice () method to bind to the service .

(2) difference between StartService () and Bindservice ():

(a) The Started service (startup service) is initiated by other components calling the StartService () method, which causes the service's Onstartcommand () method to be called. When a service is a started state, its life cycle is independent of the component that started it and can run indefinitely in the background, even if the component that started the service has been destroyed. Therefore, the service needs to be stopped by calling the Stopself () method after completing the task, or by another component calling the StopService () method.

(b) using the Bindservice () method to enable the service, the caller and the service are bound together, and once the caller exits, the service terminates, with the feature of "do not demand simultaneous birth and must die simultaneously".

(3) The developer needs to declare the entire service in the application configuration file , using the <service></service> tag.

(4) The service is typically run in the background, and it generally does not need to interact with the user, so the service component does not have a graphical user interface. service components need to inherit service base classes. service components are typically used to provide background services to other components or to monitor the running state of other components.

3.Content provider

(1)The Android platform provides Content provider to make the specified data set for an application available to other applications. Other apps can get or deposit data from the content provider through the Contentresolver class.

(2) content providers are required only if you need to share data between multiple applications. For example, Address book data is used by multiple applications and must be stored in a content provider. It has the advantage of unifying the way data is accessed.

(3)ContentProvider realize data sharing. ContentProvider is used to save and retrieve data and make it visible to all applications. This is the only way to share data across applications because Android does not provide common storage for all applications.

(4) developers do not directly use objects of the ContentProvider class, most of which are implemented on ContentProvider by Contentresolver objects .

(5)ContentProvider uses a URI to uniquely identify its dataset, where the URI is prefixed with content://, indicating that the data is managed by ContentProvider.

4.Broadcast receiver

(1) Your app can use it to filter external events to receive and respond only to external events of interest, such as when a phone is being called, or when a data network is available . The broadcast receiver does not have a user interface. However, they can start an activity or Serice to respond to the information they receive, or use Notificationmanager to notify the user. Notifications can be used in a number of ways to attract users ' attention, such as flashing back lights, shaking, playing sounds, and so on. In general, a persistent icon is placed on the status bar, and the user can open it and get the message.

(2) There are two ways to register a broadcast receiver, namely, the dynamic registration of the program and the static registration in the Androidmanifest file.

(3) The dynamic registration of the broadcast receiver is characterized by when the activity used to register is turned off, the broadcast becomes invalid. Static registration does not need to worry about whether the broadcast receiver is turned off, as long as the device is turned on and the broadcast receiver is open. This means that even if the app itself does not start, the app subscription's broadcast will also work on it when triggered.

Second, Android four components summary:

(1)Registration of 4 major components

4 basic components need to be registered to use, each activity, service, content provider are required in androidmanifest broadcast receiver broadcast receiver registers a static registration (in androidmanifest file) and dynamically created through code to invoke context.registerreceiver () is registered to the system. It is important to note that the broadcast receivers configured in the androidmanifest file will remain active as the system is started. Whenever you receive a broadcast that you are interested in, it is triggered (even if the program is not running).

(2)activation of 4 large components

Content provider activation: When a request is received from Contentresolver , the content provider is activated. The other three components activity, service, and broadcast receivers are activated by an asynchronous message called intent.

(3)closure of 4 large components

The content provider is only activated when responding to contentresolver requests. A broadcast receiver is activated only when it responds to broadcast information. Therefore, it is not necessary to explicitly close these components. activity Close: You can close an activity by calling its finish () method . Service shutdown: For services started by the StartService () method, to invoke the Context.stopservice () method to close the service, the service started with the Bindservice () method is called The Contex.unbindservice () method closes the service.

(4)tasks in Android (activity stack)

(a) task is actually activity stack, which consists of one or more activity, together to complete a complete user experience. At the bottom of the stack is the activity, which initiates the entire task, and the top of the stack is the activity, when a activity start another time, activity is pushed into the stack and becomes the currently running activity. The former activity remains in the stack. Current >back activity. The stack is actually the object, the stack of activity will never reflow, will only press in or eject.

(B) All activity in the mission is moved as a whole. The entire task (that is , the activity stack) can be moved to the foreground or back to the background.

(c) The Android system is a multitasking (multi-task) operating system that allows you to listen to music on your phone while performing several other programs. With each application, it consumes more system memory, and when too many programs are executed at the same time, or the closed program does not release the memory correctly, the system will feel slower and even more unstable. To solve this problem, Android introduced a new mechanism, the lifecycle (life cycle).

Android Four components

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.