Android Development Four components overview

Source: Internet
Author: User

This article is mainly about Android development of the four components, this paper is mainly divided into
I. Specific explanation of activity
Second, service specific explanation
Third, broadcast receiver specific explanation
Iv. Content Provider Specific explanations

Plus a specific explanation of an important component, intent. (mainly from the network)

1. Activity

An activity is usually presented as a visual user interface. For example, an activity might show a list of menu items that a user can choose, or a caption that shows some pictures and images. A messaging application might include an activity that displays a list of contacts, an activity that writes information, and other activity that looks at information and changes application settings. Although these activities work together to form an application, each activity is relatively independent. Each activity is a subclass of activity (android.app.Activity).

An application may include only one activity, or as many activity as the message service program mentioned above. An application consists of several activity and the functions of each activity complete depending on the application and its design. Typically, each application includes an activity that is first presented to the user after the application is launched. Initiating a new activity in the activity currently being presented to the user enables the transition from one activity to another activity.

Each activity will have a form for drawing the user interface. Usually such a window fills the entire screen, and of course the form can be smaller than the screen and floats on top of other forms. Activity can also use some extra forms, such as a pop-up dialog box that requires a user's response, or a form that presents some important information to the user after the user selects an entry on the screen.

The visual content area of an activity form is a view that has a hierarchical relationship (much like a tree in a data structure), whereas a view is represented by a subclass of Class View. Each view controls a rectangular area in the window. The parent view includes some child views and manages the layout of the child views. The view at the leaf node controls and responds to the user's actions directly. So the view is the interface that the activity interacts with the user. For example, a view that displays a picture may start an action when the user clicks it. Android has many views that developers can use directly, including buttons, text fields, scroll bars, menus, checkboxes, and so on.

Set the view of the form that represents the activity by calling the Activity.setcontentview () method. The content view is the root node view in the view hierarchy.

2. Service

Service is one of Android's four components, that is, the Android system services (not a thread, is part of the main program), unlike the activity, it can not interact with the user, can not start their own, Need to call Context.startservice () to start, execute the background, assuming we exit the app, the service process does not end, it is still in the background row.

The service does not have a real user interface, but it is always executed in the background. For example, the service may play background music when the user is dealing with something else, or fetch data from the network, or perform some operations and provide the operation structure to the activity to show the user. Each service extends from a class serivce.

Multimedia player playing music is a good example of applying a service. The multimedia player program may contain one or more activity where the user selects and plays music. However, music playback does not require an activity to process, because the user may want the music to continue to play, even if exiting the player to perform other programs. To keep the music playing, the multimedia player activity may start a service to play music in the background. The android system causes the music playback service to execute, even after the activity that started the service exits.

The application is able to connect to a service that is executing. When connected to a service, the service can be used to communicate with the service to the exposed interface. For the above mentioned service of playing music, this interface may consent to the user pausing, stopping or playing the music again.

As with the activity and other components, the service is executed in the main thread of the application process. So they cannot clog other components or user interfaces, and typically need to derive a thread for those service to perform time-consuming tasks.

3. Broadcase Receiver

1. Broadcastreceiver is used to receive broadcast intent asynchronously. There are two main categories that are used to receive broadcasts:


Regular broadcast normal broadcasts (sent with Context.sendbroadcast ()) is completely asynchronous. They are executed in an undefined order, typically at the same time. This is more efficient, but means that receiver cannot include the results you want to use or the API that is aborted.


Ordered broadcasts Ordered broadcasts (sent with Context.sendorderedbroadcast ()) are sent to a receiver each time. Order is that each receiver can be transmitted to the next receiver after execution, and can completely stop the transmission--not to other receiver. The order of receiver execution can be controlled by the android:priority inside the matched Intent-filter, and receiver is executed in random order when priority is the same.

2, broadcast receiver is a focus on receiving broadcast notification information, and make the corresponding processing components. Very many broadcasts originate from system code-for example, notification time zone changes, low battery level, taking a photo, or user changing language options. Applications can also broadcast-for example, notifying other applications that some data has been downloaded and in a usable state.
3. The application can have a random number of broadcast receivers to respond to all of the notification messages that are of interest to them. All receivers inherit from the Broadcastreceiver base class.
4, the broadcast receiver does not have a real user interface. However, they can initiate an activity 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-flashing back lights, shaking, playing sounds, and more. In general, a persistent icon is placed on the status bar where the user can open it and get the message.

4. Content Provider

ContentProvider (content provider) is one of the four components of Android. It is mainly used to share data externally, that is, by contentprovider the data in the application to other applications, other applications can manipulate the data in the specified application through ContentProvider. ContentProvider is divided into the system and its own definition, the system is such as contact people, pictures and other data.
The following is a general overview of ContentProvider in Google Docs.
Content providers use some specific application data to feed other applications. Data can be stored in a file system, SQLite database, or other means. The content provider inherits from the ContentProvider base class and implements a standard set of methods for other applications to fetch and store the data it manages. However, the application does not call these methods directly, but instead uses a Contentresolver object that invokes its method as an alternative. Contentresolver is able to engage with casual content providers and work with them to manage all relevant interactive communications.

5, intent

When a request is received from the Contentresolver, the content provider is activated. The other three components ──activity, service, and broadcast receivers are activated by an asynchronous message called intent. Intent is a intent object that holds the content of the message. For activity and service, it indicates the requested action name, as well as the URI and other information that is the data for the action object. For example, it can host a request for an activity, let it display a picture for the user, or let the user edit some text. For broadcast receivers, the intent object indicates the declared behavior. For example, it can declare a camera button to be pressed on all objects of interest.
For each component, the method of activation is different:
1, by passing a intent object to context.startactivity () or Activity.startactivityforresult () to load (or assign a new job to) an activity. The corresponding activity is able to view the intent that activates it by calling the Getintent () method. Android invokes the activity's Onnewintent () method to pass it to its secondary intent.
An activity often starts the next one. Assuming that it expects the activity it initiates to return a result, it will call Startactivityforresult () instead of startactivity (). For example, suppose it launches another activity to allow the user to pick a photo, and 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.
2, by passing a intent object to Context.startservice () will start a service (or give the service being executed with a new instruction). Android invokes the OnStart () method of the service and passes the intent object to it.
Similarly, a intent can be passed to Context.bindservice () by the calling component to obtain a connection to the target service being executed. This service gets the intent object through a call to the Onbind () method (assuming that the service has not started, Bindservice () starts it first). For example, an activity can connect to the aforementioned music playback service and provide the user with an actionable (user interface) to control playback. The activity can call Bindservice () to establish a connection and then invoke the object defined in the service to affect playback.
3. The application can pass the intent object to Context.sendbroadcast (), Context.sendorderedbroadcast (), and Context.sendstickybroadcast () and other similar methods to generate a broadcast. Android invokes the OnReceive () method of all broadcast receivers that are interested in this broadcast to pass intent to them.


Android Development Four components overview

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.