01 composition of Android applications

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 screen navigationToAnother 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

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 consumer er is triggered, your application does not need to call the intent consumer ER for the request. The system starts your application as needed. Various Applications can also broadcast their own intent Referer to other applications by using context. broadcastintent.

 

3. 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.

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.