Android Application Development basics: Application Components

Source: Internet
Author: User

Introduction to Android Application Development

Android applications use Java as the development language. The aapttool packages the compiled Java code together with the data and resource files of its own application program to an androidfile. This file uses .apk as the extension. It is the medium for publishing applications and installing them on mobile devices. Users only need to download and install this file on their devices. All the code in a single .apk file is considered as an application.

In many aspects, every Android application exists in its own world:

◆ By default, each application runs in its own Linux Process. When an application starts execution, Android starts a process. When the application no longer needs this process and other applications need system resources, it is disabled.

◆ Each process runs on its own VM. Therefore, application code is actually isolated from other application code.

◆ By default, each application is assigned a unique Linux User ID and configured with permissions, so that the application files are only visible to this user and the application. Of course, there are other ways to make these files accessible to other applications.

Two applications can have the same user ID. In this case, they can see each other's files. To protect system resources, applications with the same ID will run in the same Linux Process and in the same virtual machine.

Core of Android Application Development: Application Components

One of the core functions of Android is that an application can use the elements of other applications if that application permits ). For example, if your application requires an image scroll list, and another application has developed an identical list suitable for use by others, you can call the scroll list directly to implement the function, instead of developing another one. The application does not include or link the code of other applications. It only enables the functions of other applications when needed.

To achieve this goal, the system must start this application as part of an application needs it and instantiate that part of the Java object. Unlike applications on other systems, Android applications do not provide separate program portals for applications, such as the main () method, but provide some core components, the system can instantiate these components as needed. There are four types of components:

Activity)

Activity is a visual user interface displayed in response to user operations. For example, an Activity can display a menu item list for users to choose from, or display pictures containing instructions. A Short Message application can include an Activity used to display the list of contacts for sending objects, an Activity for sending text messages to selected contacts, and reading previous text messages and changing settings. Although they write together to complete a complete user interface, each Activity remains independent from other activities. All are implemented by subclasses of the Activity class.

An application can have only one Activity, or, as mentioned in the text message application, it contains multiple. The role and number of each Activity naturally depend on the application and its design. Generally, one of the activities is marked to start the Activity. When the application starts, the user first sees the content of the Activity. Start a new Activity with the current Activity to switch to another Activity.

Each Activity is drawn by a default window. Normally, this window is full, but it can also be a small floating window located above other windows. You can also use an additional window for an Activity. For example, a dialog box is displayed during the Activity running process, or an important information window is displayed when you select a specific project on the screen.

The visible content displayed in a window is composed of a series of views that inherit from the View base class. Each View Controls a specific rectangular space in the window. The parent view contains and organizes its child view layout. The leaf node view is located at the bottom of the view hierarchy. It is drawn in the rectangle under their control and responds to the operations implemented by the user. Therefore, a view is the interface on which the Activity interacts with the user. For example, a view can display a small image and generate an action when the user instructs it. Android has many established views for users to use directly, including buttons, text fields, scroll bars, menu items, check boxes, and so on.

The view and its hierarchy are put into the Activity window by the Activity. setContentView () method. Content View) is a View object located at the root position of the View hierarchy. For more information about the view and its hierarchy, see the user interface section .)

Service)

The Service does not have a Visualized User Interface, but runs in the background within a period of time. For example, a service can play background music, obtain some data from the network, or compute some things in the background when the user is doing other things, and provide it to the Activity that requires this calculation result. Each Service is inherited from the Service base class.

A good example is the playlist of a media player. The player application may have one or more Activity to select a song and play it. However, the task of playing music itself should not be processed by any Activity, because the user expects the music to continue playing when they leave the player application and start to do other things. For this purpose, the Media Player Activity should enable a service running on the background. The system will continue to run the music play service after the Activity is no longer displayed on the screen. We can connect to and bind) to a running service. If the service is not running, it is started ). After the connection, you can communicate with the service through the interface exposed by the Service. For music services, this interface allows users to pause, roll back, stop, and start playing again.

Like other components, the service runs in the main thread of the application process. Therefore, it does not interfere with other components or user interfaces. They generally derive a new thread to perform time-consuming tasks such as music playback ). See the following processes and threads.

Broadcast receiver)

A broadcast receiver is a component that focuses on receiving and processing broadcast notifications. Many broadcasts are derived from system code, such as notifying time zone changes, low battery, taking a picture, or changing language options. Applications can also be broadcast-for example, to notify other applications that some data has been downloaded and is available.

An application can have any number of broadcast receivers to respond to all notifications of interest to it. All receivers inherit from the BroadcastReceiver base class.

The broadcast receiver itself does not display the user interface. However, it can start an Activity to respond to received information, or use icationicationmanager to notify users. Notifications can be used in many ways to attract users' attention-flashing back lights, vibrations, and playing sounds. In general, a Persistent icon is placed on the status bar. You can open it and get the message.

ContentProvider)

The content provider provides specific application data to other applications. Data can be stored in a file system, SQLite database, or other methods. The content provider inherits from the ContentProvider base class and implements a set of standard methods for other applications to read and store the data it manages. However, the application does not directly call these methods, but uses a ContentResolver object to call its methods. ContentResolver can communicate with any content Provider to manage all related interactive communication.

Every time a request needs to be processed by a specific component, Android will make sure that the application process of that component is running or start it whenever necessary. Make sure that the instance of the corresponding component exists and that instance will be created if necessary.

The above section describes the basics of Android application development. This article is from the blog "Application basics-1" of Android SDK 1.5.

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.