Android Four components (detailed summary)

Source: Internet
Author: User

Android Four components (detailed summary)-ican87 's Column-Blog channel-csdn.net

Android Four components are activity,service,content provider,broadcast
Receiver.

First,Android Four components of the detailed

1.activity

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

(2) communication betweenActivity through Intent .

(3) every Activity in anAndroid application must be declared in the androidmanifest.xml configuration file, or the system will not recognize or perform the Activity.

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 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 StartedService (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 servicein 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) theAndroid 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 by Contentresolver objects to ContentProvider the operation.

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

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 of4 major components

activity , service , content Provider all need to be androidmanifest files are configured. androidmanifest file is not declared in the activity , services, and content providers will not be visible to the system and are therefore not available. and broadcast
Receiver Broadcast recipient's registered sub-static registration (in androidmanifest file) and dynamically created through code to invoke is registered to the system. Note that 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 of4 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 of4 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 activityby calling its finish () method. Service shutdown: For services started by the StartService () method, call the Context.stopservice ( ) method to close the service, using bindservice () method to invoke the Contex.unbindservice () method to close the service.

(4) tasks inAndroid (activity Stack)

(activity , which consists of one or more activity , which together completes a complete user experience. The bottom of the stack is the activity , the top of the stack is the activity , when a activity starts another time, the new activity is pressed into the stack and becomes the currently running activity . The previous activity remains in the stack. When the user presses back key, the current activity out of the stack, while the previous one reverts to the currently running activity . The stack is actually the object, the stack of activity never reflow, 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 (detailed summary)

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.