Analysis of Four Android Components

Source: Internet
Author: User

Analysis of Four Android Components
1. Features of the four major components * an activity is usually a separate screen (window). The Activity communicates with each other through Intent. In android applications, each Activity must be in AndroidManifest. declared in the xml configuration file, otherwise the system will not recognize or execute the Activity. * The startup and binding services are used to complete user-specified operations in the background. There are two types of services: (a) started (start): When an application component (such as activity) calls the startService () method to start the service, the service is in the started state. The life cycle of the service is determined by yourself. Started service (start service) is started when other components call the startService () method, which causes the onStartCommand () method of the service to be called. When a service is in the started state, its life cycle is irrelevant to the component that starts it and can be run indefinitely in the background, even if the component that starts the service has been destroyed. Therefore, the Service must call the stopSelf () method after the task is completed to stop, or other components call the stopService () method to stop. (B) bound (binding): When the application component calls the bindService () method to bind to the service, the service is in the bound state. The bindService () method is used to enable the Service. When the caller and the service are bound together, the service is terminated once the caller exits. The lifecycle of the service is the same as that of the current component. * Content provider Content Provider provides the specified dataset of an application to other applications. Other applications can obtain or store data from the content provider through the ContentResolver class. when data is shared among multiple applications, the content provider is required. For example, the address book data is used by multiple applications and must be stored in a content provider. The advantage of ContentProvider is the unified data access mode. ContentProvider is used to save and retrieve data and make it visible to all applications. This is the only way for different applications to share data, because android does not provide a public storage area for all applications to access. Developers do not directly use the ContentProvider class objects, most of the operations on ContentProvider are implemented through the ContentResolver object. ContentProvider uses URI to uniquely identify its dataset. The URI here uses content: // as the prefix, indicating that the data is managed by ContentProvider. * Broadcast receiver (dynamic or static) broadcast receiver can filter external events and only filter external events of interest (for example, when calls are received or data networks are available) receive and respond. They can start an activity or serice to respond to the information they receive, or use icationicationmanager to notify users. Notifications can be used in many ways to attract users' attention, such as flashing back lights, vibrations, and playing sounds. Generally, a persistent icon is placed on the status bar. You can open it and obtain the message. There are two ways to register a broadcast receiver, these are the dynamic registration of programs and static registration in the AndroidManifest file. The dynamic registration of broadcast receivers is characterized by the failure of broadcast when the Activity used for registration is disabled. Static registration does not need to worry about whether the broadcast receiver is disabled. As long as the device is enabled, the broadcast receiver is also enabled. That is to say, even if the app itself is not started, the broadcast subscribed to by the app also takes effect when triggered. 2. Implementation of four major components: (the lifecycle and process of the four major components are independent of each other) * Activity component: inherits the activity class, and implement the call function * Service component in the activity life cycle: Inherit the service class, and implement the call function * Broadcastreceive component in the service life cycle: Inherit the broadcastreceive class and implement the onReceive () method, it provides a more free way to use intent and notification. It is the consumer of intent and inspires other components based on intent. Broadcastreceive has a life cycle of about 10 seconds. If you do something in onReceive () for more than 10 seconds, an error message indicating No Response from the ANR (Application No Response) program will be reported, if you need to complete a time-consuming task, you should send Intent to the Service, which is done by the Service. * Contentprovider component: inherits the contentprovider class and implements the SUID method. 3. The registration of the four major components must be registered before they can be used. Each Activity, Service, and ContentProvider content provider must be configured in the AndroidManifest file. The registration format is as follows: * <activity> the name attribute of the element specifies the subclass of the activity that implements the Activity. The icon and label attributes point to the resource file that contains the icons and labels of the activity displayed to the user. * The <service> element declares the service * <receiver ER> element declares the broadcast receiver. The registration of BroadcastReceive broadcast receiver is divided into static registration (configured in the AndroidManifest file) and registration to the system by dynamically creating code and calling Context. registerReceiver. Another feature of Dynamic Registration of broadcast receivers is that when the Activity used for registration is disabled, the broadcast will become invalid. Static registration does not need to worry about whether the broadcast receiver is disabled. As long as the device is enabled, the broadcast receiver is also enabled. That is to say, even if the app itself is not started, the broadcast subscribed to by the app also takes effect when triggered. * <Provider> the element is used to declare the activation of four major components of content provider 4. * Activity is activated by passing an Intent object to Context. startActivity () or Activity. startActivityForResult () to load (or specify a new job) an activity. The corresponding activity can be viewed by calling the getIntent () method to activate its intent. If it expects the activity it starts to return a result, it will replace startActivity () by calling startActivityForResult (). For example, if it starts another Activity so that users can select a photo, they may want to know which photo has been selected. The result is encapsulated in an Intent object and passed to the onActivityResult () method of the called activity. * Service Activation can be performed by passing an Intent object to Context. startService () or Context. bindService (). The former Android calls the onStart () method of the service and passes the Intent object to it. The latter Android calls the onBind () method of the Service to pass the Intent object to it. * To activate Broadcastreceive, you can pass an Intent object to Context. sendBroadcast (), Context. sendOrderedBroadcast (), or Context. sendStickyBroadcast (). Android will call the onReceive () Methods of all broadcast receivers interested in this broadcast and pass intent to them. * Contentprovider activation: after receiving a request from ContentResolver, the content provider is activated. The other three components, activity, service, and broadcast receiver, are activated by an asynchronous message called intent. 5. Close the four components * Activity: You can call its finish () method to close an activity * Service: for a Service that is started using the startService () method, call Context. the stopService () method is used to close the service. To start a service using the bindService () method, you must call Contex. unbindService () method to close the service. * Broadcastreceive is activated only when it responds to broadcast information. It is not necessary to explicitly close this component. * Contentprovider is activated only in response to ContentResolver's request. It is not necessary to explicitly close this component. 6. FrameWork (Application FrameWork) identifies and creates four major component objects (loosely coupled implementation) * A very core aspect of the Android Application Architecture: intent messages are passed to the FrameWork by carrying the four major component name strings to be activated. The FrameWork layer will use this string in AndroidManifest. xml finds the corresponding component and its attribute configuration, constructs the component object, and starts it. 7. In android, the task (activity stack) (a) is actually the stack of the activity, which consists of one or more activities to complete a complete user experience. The bottom of the stack is the Activity that starts the entire task. The top of the stack is the Activity that the currently running user can interact with. When another activity starts, the new activity is pushed to the stack, and become the currently running activity. The previous activity remains in the stack. When you press the BACK key, the current activity goes out of the stack, and the previous activity is restored to the currently running one. Objects are actually stored in the stack. The Activity in the stack will never be rearranged, but will only be pushed in or popped up. (B) all activities in the task are moved as a whole. The entire task (that is, the activity stack) can be moved to the foreground or to the background. (C) The Android system is a Multi-Task operating system. It can use mobile phones to listen to music and execute multiple other programs. Every time you execute an application, it will consume more system memory. when too many programs are executed at the same time, or the closed program does not properly release the memory, the system will feel slower and slower, even unstable. To solve this problem, Android introduces a new mechanism, namely, Life Cycle ).

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.