Android Components.

Source: Internet
Author: User
Tags call back

Four main components of Android development are:

Activity: Used to present functions.

Service: the service runs in the background and does not provide interface rendering.

Broadcastreceiver: used to receive broadcasts.

Content Provider: supports data storage and reading in multiple applications, which is equivalent to a database.

Activity

In Android, activity is the foundation of all programs, and the processes of all programs run in activity. activity is the most frequently encountered by developers and one of the most basic modules in Android. In Android programs, activity generally represents a screen on the mobile phone screen. If you compare a mobile phone to a browser, activity is equivalent to a webpage. You can add buttons, check boxes, and other controls to the activity. The concept of activity is similar to that of web pages.

Generally, an Android Application consists of multiple activities. You can jump between multiple activities. For example, after you press a button, you may jump to another activity. A little different from a webpage jump is that the jump between activities may return values. For example, if activity a jumps to Activity B, when Activity B stops running, activity A may return a value. This is often quite convenient.

When a new screen is opened, the previous screen is paused and pushed into the history stack. You can return to a previously opened screen through the rollback operation. We can selectively remove unnecessary screens, because android will save the start of each application to the current screen in the stack.

Service

A service is a component in the Android system. It is similar to the activity level, but it cannot run on its own. It can only run on the background and interact with other components. Service is a code without a long life cycle interface. A service is a program that can run for a long time, but it does not have a user interface. This is a bit boring. Let's look at an example. Open a music player program. If you want to access the Internet at this time, we open the android browser. Although we have already entered the browser program, the playing of the song has not stopped, instead, you can play the next one in the background. In fact, this playback is controlled by the service that plays the music. Of course, the Service for playing music can also be stopped. For example, when the songs in the playlist are all over, or the user presses the shortcut key for stopping music. Service
It can be used in multiple applications. For example, when the user starts other activities during multimedia playback, the program will continue playing in the background, for example, detecting file changes on the SD card, or record the changes in your geographic information location in the background. In short, the service is always hidden in the back.

There are two ways to enable service:

(1) context. startservice (): the service will go through oncreate-> onstart (if the service is not running, Android first calls oncreate () and then calls onstart (); if the service is already running, only onstart () is called, so the onstart method of a service may be called multiple times). When stopservice is called, ondestroy is called directly. If the caller directly exits without calling stopservice, the service is always running in the background. After the service caller starts up again, the service can be closed through stopservice.
Note that context is called multiple times. startservice () is not nested (even if the corresponding onstart () method is called), no matter how many times the same service is started, Once context is called. stopservice () or stopself. Note: intent objects passed to startservice () are passed to the onstart () method. The call sequence is oncreate -- & gt; onstart (which can be called multiple times) -- & gt; ondestroy.

(2) context. bindservice (): the service will go through oncreate () -- & gt; onbind (), onbind will return to the client an ibind interface instance, ibind allows the client to call back the service method, for example, you can obtain the service running status or other operations. At this time, the caller (such as activity) will be bound with the service, and the context will exit, and srevice will call onunbind -- & gt; ondestroyed to exit accordingly, the so-called binding together will survive.

Broadcastreceiver

In Android, broadcast is a widely used mechanism for transmitting information between applications. Broadcastreceiver is a type of component that filters and accepts and responds to broadcast. Broadcastreceiver can be used to allow applications to respond to an external event. This is very interesting. For example, you can use broadcastreceiver to handle this external event when the incoming call arrives. For example, you can still use broadcastreceiver to process a program successfully downloaded. Broadcastreceiver cannot generate the UI. That is to say, it is not transparent to users and invisible to users. Broadcastreceiver uses icationicationmanager
To notify users that these events have happened. Broadcastreceiver can be registered either in androidmanifest. xml or in the runtime code using context. registerreceiver. As long as it is registered, when the event comes, even if the program is not started, the system starts the program as needed. Various Applications can also broadcast their own intent broadcasts to other applications by using context. sendbroadcast.

You can register broadcastreceiver in either of the following ways:

(1) Register in androidmanifest. xml. This method has a feature that even if your application is closed, the broadcastreceiver still accepts the broadcast objects, that is to say, the broadcast events can be accepted whether the application is enabled or disabled in the active state;

(2) register the broadcast in the code.

The first is static registration, and the second is dynamic registration. The difference between the two is as follows:

Dynamic Registration is more flexible than static registration. Experiment shows that when a broadcast receiver is registered statically, no matter whether the application is started or not. Can accept the corresponding broadcast.

During dynamic registration, if you do not execute the unregister register Er (); Method to cancel registration, it is the same as static. However, if you execute this method, you will not be able to accept the broadcast after it is executed.

Content Provider

Content Provider is a third-party Application Data Access solution provided by Android.

In Android, data protection is very strict. Apart from the data stored on the SD card, the databases and files held by an application are not allowed to be directly accessed. Andorid certainly does not make every application an isolated island. It provides a window for all applications. This is the content provider. The data to be provided by an application can be encapsulated into a content provider by deriving the content provider class. Each content provider uses a URI as an independent identifier, such as content: // COM. XXXXX. Everything looks like rest, but in fact, it is better than rest
More flexible. Similar to rest, Uri can also be of two types, one with ID and the other with list, but the implementers do not need to follow this pattern, you can also return the list type data for the URI of your ID. As long as the caller understands it, there is no need to demand the so-called rest.

In addition, content providers are not only available in Uris like rest, and can also accept projection, selection, orderby, and other parameters. In this way, you can perform projection, selection, and sorting like databases. The query results are returned in the form of cursor (see reference/Android/database/cursor.html). The caller can move the cursor to access the data of each column.

Content Provider shields the storage details of internal data and provides the preceding unified interface model. This abstraction layer greatly simplifies the writing of upper-layer applications, it also provides a more convenient way for data integration. Content Provider is usually implemented in databases. Android provides powerful SQLite support, but in many cases, you can also encapsulate files or other mixed data.

In Android, content resolver is used to initiate content provider locating and access. However, it only provides the content provider interface for Synchronous access. However, generally, content providers may need to access large data sources such as databases, which is less efficient than enough, resulting in congestion of calling threads. Therefore, Android provides an asyncqueryhandler (see reference/Android/content/asyncqueryhandler.html) to help users access content providers asynchronously.

Among the major components, both service and content provider require continuous access. If a service is time-consuming, asynchronous access interfaces are often provided. content providers provide agreed synchronous access interfaces regardless of the efficiency.

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.