Four main components 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:

Activity



Intent receiver er

Service

Content Provider

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.

When we have clarified which construction blocks are required for our application, we need to register the list of these construction blocks in androidmanifest. xml. This is an xml configuration file that defines the components, functions, and necessary conditions of our application. This configuration file is required by every android application. For the schema of androidmainfest. XML, refer to the documentation attached to the SDK package. The following describes the four construction blocks:

I. Activity

Activity is the most basic type of Android block construction. In an application, an activity is usually a separate screen. Every activity is implemented as an independent class and inherits from the base class of activity. This activity class will display the user interfaces composed of several views controls and respond to the events. Most applications contain multiple screens. For example, a short message application will have a screen for displaying the contact list, a second screen for writing short messages, and a screen for browsing old short messages and system settings. Every screen like this is an activity. It's easy to navigate from one screen to another. In some applications, a screen may even return a value to 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. By default, Android retains the running screen from the main screen to each application.

Android uses the intent special class to move between the screen and the screen. 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. Typical action types include main (activity portal), view, pick, and edit. The data corresponding to the action is represented in Uri format. For example, to view the contact information of a person, you need to create an intent with the action type of view and a URI that represents the person.

An associated class is intentfilter. Compared with intent, it is a valid request for doing something. An intent filter is used to describe which intent an activity (or intent Referer) 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 must be defined in androidmanifest. xml.

By parsing various intents, it is easy to navigate from one screen to another. When you navigate forward, activity calls the startactivity (intent myintent) method. Then, the system finds the activity that best matches the intent of myintent in the intentfilter defined in all installed applications. 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.

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

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

5. Intent class

Intent describes the actions, actions involving data, and additional data of an application. Android identifies the corresponding components based on the description of the intent, pass intent to the called component and complete the call of the component. Therefore, intent serves as a media intermediary, providing information about component calls to each other to decouple callers from callers.

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.