Getting started with Android (basic elements and structures of applications)

Source: Internet
Author: User
For an Android app, first look at an overview of the official website.

For an Android appProgramIt is made up of four types of constructor blocks. The four constructor 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:

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

 

For intent, see: http://www.cnblogs.com/windwithlife/archive/2009/07/17/1525385.html

Note:

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 validator is registered in androidmanifest. XML, but it can also be registered inCodeUse context. registerreceiver () 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.