Android Four components parsing

Source: Internet
Author: User

1. Features of four components
* Activity
An activity is usually a separate screen (window), and the activity communicates through the intent. , each 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.
* Service (Start-up and tie-train)
Service is used to complete user-specified actions in the background. The service is divided into two types: (a) started (boot): When the application component (such as activity) calls the StartService () method to start the service, it is in the started state. The service's life cycle is up to its own discretion. 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) bound (binding): The service is in bound state when the application component calls the Bindservice () method to bind to the service. Using the Bindservice () method to enable the service, the caller binds to the service, and the caller exits and the service terminates. The service's life cycle is the same as the current component's life cycle.

* Content Provider
The Content provider makes the specified dataset for one application available to other applications. Other apps can get or deposit data from the content provider through the Contentresolver class, and content providers are needed when data is shared across 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 unified data access, and contentprovider for 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, and developers do not directly use objects of the ContentProvider class. Most are implemented on ContentProvider by Contentresolver objects; ContentProvider uses URIs to uniquely identify its dataset, where the URI is prefixed with content://. Indicates that the data is managed by ContentProvider.
* Broadcast receiver (dynamic or static)
Broadcast receiver can filter external events to receive and respond only to external events of interest, such as when the phone is being called, or when the data network is available. 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, put a persistent icon on the status bar, the user can open it and get the message, there are two methods of registering the broadcast receiver, namely, the dynamic registration of the program and the static registration in the Androidmanifest file, the dynamic registration of the broadcast receiver is when the activity used to register is turned off. , the broadcast will fail. 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.

2, four components of the realization: (four components of the life cycle and the process is independent of each other)
* Activity component: Inherits the activity class and implements the calling function in the activity's life cycle
* Service component: Inherits the service class and implements the call function in the service's life cycle
* Broadcastreceive Component: Inherits the Broadcastreceive class, implements the OnReceive () method, provides a freer way to use intent and notification, are intent consumers and inspire other components based on intent. Broadcastreceive life cycle of only about 10 seconds, if within onreceive () do more than 10 seconds of things, will report the ANR (Application no Response) program unresponsive error message, if you need to complete a more time-consuming work, It should be done by sending Intent to the service and by the service.
* ContentProvider component: Inherit ContentProvider class, implement Suid method.
3, four components of the registration
All four components need to be registered to be used, and each activity, Service, ContentProvider content provider needs to be configured in the Androidmanifest file. The registration format is as follows:
* The name attribute of the <activity> element specifies the subclass of activity that implements the activity. The icon and label properties point to the resource file that contains the icons and labels for this activity that are presented to the user.
* <service> elements for declaring services
* The <receiver> element is used to declare a broadcast sink. The registration of the Broadcastreceive broadcast recipient is statically registered (configured in the Androidmanifest file) and is dynamically created via code and registered to the system in the form of a call to Context.registerreceiver (). The dynamic registration of the broadcast receiver also has the feature that when the activity used to register is turned off, the broadcast fails. 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.
* <provider> elements for declaring content providers
4. Activation of four components
* activity is activated by passing an intent object to context.startactivity () or Activity.startactivityforresult () to load (or designate a new job to) an activity. The corresponding activity can view the intent that activates it by calling the Getintent () method. If it expects the activity it initiates to return a result, it will replace startactivity () with the call to Startactivityforresult (). For example, if it launches another activity to allow the user to pick a photo, it may want to know which photo is selected. The result is encapsulated in a intent object and passed to the Onactivityresult () method of the activity that made the call.
* Service activation can be done by passing a intent object to Context.startservice () or Context.bindservice (). The former Android invokes the service's OnStart () method and passes the intent object to it, which Android invokes the service's Onbind () method to pass the intent object to it.
* Broadcastreceive activation can be done by passing a intent object to Context.sendbroadcast (), Context.sendorderedbroadcast () or Context.sendstickybroadcast (). Android invokes the OnReceive () method of all broadcast receivers that are interested in this broadcast, passing intent to them.
* ContentProvider 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
5. Closing of four components
* Activity off: You can close an activity by 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.
* Broadcastreceive is only activated when responding to broadcast messages, there is no need to explicitly close this component.
* ContentProvider is only activated when responding to Contentresolver requests, there is no need to explicitly close this component.
6. Framework (Application framework) identifies and creates 4 large component objects (loosely coupled implementations)
* A very central point in the Android application architecture: The intent message is passed to the framework by carrying 4 large component name strings that need to be activated. The framework layer finds its corresponding component and its property configuration in Androidmanifest.xml based on the string, constructs the Component object, and then starts it up.

7. Tasks in Android (activity stack)
(a) The task is actually the stack of activity, which consists of one or more activity that collectively completes a complete user experience. The bottom is the activity that initiates the whole task, the top of the stack is the activity that the current user can interact with, and when one activity launches the other, the new activity is pushed into the stack and becomes the active activity of the current operation. The previous activity remains in the stack. When the user presses the back key, the current activity is out of the stack, and the previous one reverts to the currently running activity. The stack is actually the object, the activity in the stack 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).

This article from "Tech record" blog, declined reprint!

Android Four components parsing

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.