What are the four components of Android programming, respectively? _android

Source: Internet
Author: User

The four components of Android development are: activities (activity): for performance functions. Service: background Run service, does not provide interface rendering. Broadcast Receiver (broadcastreceiver): used to receive broadcasts. content Provider (ContentProvider): supports storing and reading data in multiple applications, equivalent to databases.

1. Activity
in Android, activity is the root of all programs, and the process of all programs runs in activity, which is the most frequently encountered by developers and one of the most basic modules of Android. In Android apps, activity typically represents a screen on a mobile phone screen. If you compare a cell phone to a browser, activity is the equivalent of a Web page. Some controls, such as button, Check box, can be added to the activity. You can see that the concept of activity is quite similar to the concept of Web pages.
A typical Android application is made up of multiple activity. These multiple activity can jump to each other, for example, by pressing a button, you may jump to another activity. and web jump slightly different is that the activity between the jump is likely to return a value, for example, from activity A to activity B, then when the activity B runs out, it is possible to give activity a a return value. It is quite convenient to do so in many cases.
When a new screen is opened, the previous screen is put in a paused state and pressed into the history stack. The user can return to a previously opened screen by a fallback operation. We can selectively remove some screens that don't need to be preserved, for Android to save each application's start to the current screen on the stack.

2. Service
A Service is a component of the Android system that is about the same level as the activity, but he can't run it on his own, it can only run in the background and interact with other components. A Service is a long lifecycle code that has no interface. A Service is a program that can run for a long time, but it has no user interface. That's a bit boring, let's look at an example. Open a music player program, this time if you want to surf the internet, then we open the Android browser, this time, although we have entered the browser this program, but, the song does not stop, but in the background to continue one song after another play. In fact, this play is controlled by the service that plays music. Of course, the music service can also stop, for example, when the playlist inside the end of the song, or the user pressed the Stop music play shortcut keys. Service can be used in and multiple applications, for example, when playing multimedia, users start other activity this time the program to continue to play in the background, such as the detection of SD card file changes, or in the background to record the location of your geographical information changes and so on, in short service, always hidden behind.
There are two ways to open a service:
(1) context.startservice ():Service will experience OnCreate-> OnStart (if the service is not already running, then Android calls OnCreate () and then calls OnStart (), and if the service is running, only OnStart () is invoked. So the OnStart method of a service may be repeated multiple times), StopService directly OnDestroy, and if the caller exits without calling StopService, the service will run in the background. When the caller of the service is restarted, the service can be turned off by StopService. Note that multiple calls to Context.startservice () will not be nested (even if a corresponding OnStart () method is invoked), so no matter how many times the same service is started, once the Context.stopservice () or stopself is invoked ( ), he will be stopped. Supplemental Note: The intent object passed to StartService () is passed to the OnStart () method. The call order is: OnCreate--> onStart (callable multiple times)--> OnDestroy.
(2) Context.bindservice ():Service will experience OnCreate ()-> onbind (), Onbind will return to the client a Ibind interface instance, Ibind the method that allows the client to callback the service, such as getting service running status or other actions. This time the caller (context, such as activity) and service will be bound together, the context exit, Srevice will call the Onunbind-> ondestroyed corresponding exit, the so-called binding together will survive.

3, Broadcastreceiver
In Android, broadcast is a widely used mechanism for transmitting information between applications. And Broadcastreceiver is a kind of component that filters the sent broadcast to accept and respond. You can use Broadcastreceiver to allow applications to respond to an external event. This is very interesting, for example, when the phone calls into this external event, you can use broadcastreceiver for processing. For example, when downloading a program completes successfully, you can still use broadcastreceiver for processing. Broadcastreceiver cannot generate the UI, which means it is not transparent to the user and is not visible to the user. Broadcastreceiver through Notificationmanager to inform the user that these things have happened. Broadcastreceiver can be registered either in Androidmanifest.xml or in Run-time code using Context.registerreceiver (). As long as it is registered, when the event comes, even if the program does not start, the system will also start the program when needed. Various applications can also broadcast their own intent broadcasts to other applications by using Context.sendbroadcast ().
There are two ways of registering Broadcastreceiver:
(1) registration in Androidmanifest.xml. One feature of this approach is that even if your application has been shut down, the Broadcastreceiver will still accept the broadcast object, which means that the event will be acceptable to the broadcast regardless of whether your application is open or closed to the active state.
(2) register the broadcast in the code.
The first is commonly known as static registration, the second is commonly known as dynamic registration, the two kinds of registration Broadcastreceiver difference:
Dynamic registration is more flexible than static registration. Experiments show that when a broadcastreceiver is registered statically, whether the application is started or not. can accept the corresponding broadcast.
Dynamic registration, if you do not perform unregisterreceiver (), method cancellation registration, and static is the same. However, if you execute this method, you cannot accept the broadcast after it has been executed.

4. Content Provider
Content Provider is an Access program for Third-party application data provided by Android.
In Android, the protection of data is very tight, in addition to the data placed in the SD card, an application of the database, files, etc., are not allowed to other direct access. Andorid Of course does not really make every application an island, it has a window for all applications, this is the content Provider. Applications want to provide data, can be derived from the ContentProvider class, encapsulated into a content Provider, each content Provider with a URI as a separate identity, in the form of: Content://com.xxxxx. Everything looks like rest, but in reality it is more flexible than rest. Like rest, URIs can also have two types, one with ID, the other is list, but the implementation does not need to follow this pattern, give you the URI of the ID you can return the list type of data, as long as the caller understand, there is no harm, do not need to demanding so-called rest.
In addition, the Content provider is not like rest only the URI is available, and can accept parameters such as Projection,selection,orderby, so that it can be projected, selected, and sorted like a database. The results of the query are returned in the form of Cursor (see: reference/android/database/cursor.html), and the caller can move the Cursor to access the data in each column.
Content provider Masks the storage details of internal data and provides the above unified interface model, which simplifies the writing of upper application and provides more convenient way for data integration. Content provider Internal, common database to implement, Android provides strong sqlite support, but often, you can also encapsulate files or other mixed data.
In Android, Contentresolver is used to initiate the positioning and access of content provider. However, it provides only the interface of the content provider that is accessed synchronously. But in general, Content provider need to access a large data source such as a database, not efficient enough to cause the call thread congestion. So Android provides a asyncqueryhandler (see: reference/android/content/asyncqueryhandler.html) to help with the content Provider asynchronous access.
In the major components, service and content provider are the ones that require continuous access. Service if it is a time-consuming scenario, it often provides an asynchronous access interface, and content provider, regardless of efficiency, provides the agreed synchronous access interface.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.