01 The Android Application consists of four components: Activity, intent receiver er, service, and content provider.

Source: Internet
Author: User

For an android application, it is made up of four construction blocks. The four construction blocks are as follows:

However, not every android application requires these four construction blocks. This is not necessary. In some cases, we only need to combine these four types into our applications.

1. Activity

Activity is the most basic type of Android block construction. In an application, an activity is usuallySeparate Screen. Every activity is implemented as an independent class and inherits from the base class of activity. This activity class will displayViews ControlUsersInterfaceAnd respond to the event. Most applications contain multiple screens. For example,A Short Message applicationThere will be a screenShow contact list,The second screen is used to write short messages.And is also usedBrowse old short messages and screen for system settings. Every screen like this isActivity. SlaveOne ScreenNavigationToAnother screenIt is very simple. In some applications, a screen may evenReturn ValueTo the previous screen.

When a new screen is opened, the previous screen is paused and saved in the history stack. You can return to the previous screen in the history stack. When the screen is no longer used, it can also be deleted from the history stack.DefaultNext, Android willRetainedFrom the main screenEvery applicationOperationLineScreen.

Android usedIntentThisSpecialClass, implemented on the screenScreenAnd ScreenScreenBetweenMobile.. The intent class is used to describe what an application will do. In the description structure of intent, there are two most important parts: the data corresponding to the action and action. TypicalAction typeIncluding main (activity portal), view, pick, and edit. WhileData corresponding to the actionIt is represented in Uri format. For example, to view the contact information of a person, you need to create an action typeViewOfIntentAnd a URI that represents the person.

A class associated with it is calledIntentfilter. Compared with intentIs a valid request for doing somethingAn intent filter is used to describe which intent an activity (or intent receiver) can operate on. To display the contact information of a person, an intentfilter must be declared for an activity. The intentfilter must know how to handle the view action and represent a person's uri. Intentfilter is required.In androidmanifest. xmlMediumDefinition.

By parsing various intents, it is easy to navigate from one screen to another.When navigating forwardActivity will callStartactivity(Intent myintent) method. Then, the system searches for the intentfilter defined in all installed applications and finds the most matchedMyintentIntent. The new activity starts to run after it receives a notification from myintent. When the startactivity method is called, myintent Parsing is triggered. This mechanism provides two key advantages:

A. Activities can reuse a request generated in intent form from other components;

B. activities can be replaced by a new activity with the same intentfilter at any time.

2. Intent Receiver

Intent-receiver can be viewed
The agent of the activity, or the input event-type service, is handled when the event comes, and can be processed quickly,

When you want your application to respond to an external event (such as when a call is made, a data network is available, or at night), you can use an intent receiver. Although intent receiver uses icationicationmanager to notify users of events of interest, it cannot generate a UI. Intent consumer er is registered in androidmanifest. XML, but you can also use context. registerreceiver () in the Code for registration. When an intent receiver is triggered, your application does not have to call the intent
Extends er. The system starts your application as needed. Various Applications can also broadcast their own intent Referer to other applications by using context. broadcastintent.


So what exactly is intent?

As we mentioned above, activities are independent of each other. Generally, they are independent processes. However, the service-oriented requirement mentioned earlier requires that each activity has a high frequency of interaction. How can this problem be solved. This solution uses intent.

In fact, we can regard intent as the standard of activity communication. For example, the content in intent tells the system what services are required to stimulate intent activity, and the service provider activity should meet the conditions. Then it is about the Android system. It is responsible for identifying qualified activity service providers and giving intent to the activity service providers. Then, this acitivity performs corresponding operations based on the remaining information in the intent.

As shown above, intent contains two parts:

1. Information of the activity service provider, which is used by Android to find the right activity.

2. What operations does the activity service provider perform? This is for the activity service provider.

Let's talk about this process with the android built-in notepad instance program. Refer to the URL
Http://li53262182.blog.163.com/blog/static/1283933872009822105031577/


3. ServiceHowever, services only have a long life cycle.
It usually takes a long time to run multiple activity services. Because of this, their resource recycling priorities are also different. Since activity, intent-
Intent is used for reciever and service.

A service has a long life cycle and has no user interface. A better example is a media player that is playing a song from the playlist. In a media player application, there should be multiple activities that allow users to select and play songs. However, the music replay function does not have a corresponding activity, because the user will certainly think that the music should still be played when navigation to other screens. In this example, the activity of the Media Player uses context. startservice () to start a service, so that the music can be played in the background. At the same time, the system will keep the service running until the service stops running. In addition, we can connect to a service by using the context. bindservice () method (if the service is not running, it will be started ). After connecting to a service, we can also communicate with the interface provided by the Service. In the media player example, we can also pause, replay, and other operations.

4. Content Provider

Applications can save their data to files, SQL databases, or even any valid device. Content Provider is useful when you want to share your application data with other applications. A content provider class implements a set of standard methods, so that other applications can save or read various data types processed by this content provider.

For more details about the content provider, see accessing content providers in the attached document.

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.