Use eclipse to develop and debug Android applications (1)

Source: Internet
Author: User

 

This section describes how to build an android Development Environment Based on Android SDK (2.3) and eclipse (Helios) in windows and create the first application Hello Android world. The specific reference of the small blog: http://blog.sina.com.cn/deaboway
Or http://blog.csdn.net/deaboway
.

Now, we can use eclipse to create and develop Android applications. This article will continue to use the hello Android world project to deeply parse the structure and debugging of the android project in eclipse.

At the time of writing the previous article, the android SDK platform honeycomb preview and revision 1(android-3.0_pre_r01-linux.zip) were added to the Development SDK platform 2.3.1, that is, Android 9 AVD for demonstration. Now, the android SDK platform honeycomb preview has been placed in Alibaba SDK platform honeycomb preview for this tutorial.

Let's take a look at the latest honeycomb Preview (because it is a preview version, it is not flattering to start. According to rumors, the reason why this version cannot be downloaded and installed a few days ago is that Google found that this preview version is too bad, I couldn't help my face, so I removed it again, haha ):

Compared with the highest version of Android 2.3 currently used on mobile phones, Android 3.0 honeycomb is more suitable for tablet computers and is a system version specially optimized for Android tablets. With the release of the SDK, developers and manufacturers are more conducive to the development of Android 3.0 honeycomb tablets, including Android tablet applications and matching.

I,
Android
Application Overview

1.
Android
Component

Android has four first-class citizens (or component), including activity, contentprovider, broadcastreceiver, and service ). They must all be declared in the androidmanifest. xml file.

Activity
Activity

Activity is the most common Android Application Form. The activity provides the UI for the application with the help of a class called view (described later. The View class implements various UI elements, such as text boxes, labels, buttons, and other common UI elements on the computing platform.

An application can contain one or more activities. These activities usually have a one-to-one relationship with the screen in the application.

The application transfers an activity from one activity to another by calling the startactivity () or startsubactivity () method. If the application only needs to "Switch" to a new activity, the previous method should be used. If asynchronous call/response mode is required, use the last method. In both cases, an intent needs to be passed through the method parameters.

The operating system determines which activity is most suitable for the specified intent (which will be described later ).

The key to an activity is its life cycle (which will be introduced later), followed by State Preservation and Restoration (onsaveinstancestate onrestoreinstancestate ), and activity redirection and data transmission (intent ).

An activity performs almost all the operations on the application. The activity should have a window, which can be changed by theme. You should pay attention to the impact of its lifecycle (lifecycle), device status (configuration) changes, and storage of running status and data, this is critical to the reliability and humanization of an application. Permissions should also be stated in the activity to use some hardware and software features of Android, which can be provided by code or manifest. xml. Finally, each activity must be stated in manifest.

Service
Service

Like other multi-task computing environments, some applications run in the background to execute various tasks. Android calls such an application a "service ".

A service is a program without interfaces. It is a so-called service or a background program. Pay special attention to the relationship between the service start (startservice) and bindservice) methods and the service lifecycle, the life cycle effects of the two account service methods are different. In addition, the permission and service are affirmed in code or manifest.

Broadcastreceiver
Broadcast Receiver

A broadcast receiver is an application component that receives requests and processes intent. Like a service, a receiver generally does not have a UI element. The broadcast receiver is usually registered in the androidmanifest. xml file. The class attribute of the broadcast receiver is the Java class that implements the receiver.

Broadcast receiving is not generally referred to as radio broadcasting, but an intent sent by sendbroadcast (). That is, the intent is broadcast here, And broadcastreceiver is registered (registe) then, you can automatically monitor the intent that meets the given conditions. If so, the owner of this broadcastreceiver will be notified.

Contentprovider
Content Providers
-- Data Management

The content provider is the data storage abstraction mechanism of Android. Take a common data type on mobile devices as an example: Address Book or contact database. The address book contains all contacts and their phone numbers. Users may need to use the data when using their mobile phones. The content provider abstracts the access methods for data storage. Content Providers play the role of database servers in many aspects. Read/write operations on data in data storage should be passed through appropriate content providers, rather than directly accessing files or databases. There may be "clients" and "implementations" of the content provider ".

Contentprovider is used to save application data and establish and maintain the database, so that the program can return to the previous state or save information when restarted. Pay attention to the application permission and SQL language usage. Android uses a lightweight Database System SQLite.

2.
Android
Lifecycle

The lifecycle of Android programs is controlled by the system rather than directly controlled by the program itself, this is different from the thinking that a desktop application executes a specific action (such as return from the main function) After receiving the close request, which leads to the Process Termination.

In Android, when an activity calls startactivity (myintent), the system searches for the activity with the most matching intentfilter and myintent among all installed programs to start the process, and send the intent notification to the activity. This is the "production" of a program ". In Android, all applications are "born equal", so not only Android core programs or even third-party programs can issue an intent to start an activity in another program. This design of Android is very conducive to the reuse of "program components.

Android moves the least important process when the memory is insufficient based on its importance. IMPORTANCE from high to low:

1. Foreground process. Such a process has an activity that is displayed on the screen and interacts with the user or its intentreciver is running. Such a program is of the highest importance. It is terminated only when the system memory is very low.

2. Visible process. Programs that are displayed on the screen but not on the foreground. For example, a foreground process is displayed in front of the process in a dialog box. Such processes are also important. They are terminated only when the system does not have enough memory to run all foreground processes.

3. service process. Such processes run continuously in the background, such as playing background music and uploading and downloading Background Data. Such a process is generally useful to users, so it will be terminated only when the system does not have enough memory to maintain all foreground and visible processes.

4. background process. Such a program has an activity that is invisible to users. When the system memory is insufficient, such a program is terminated in the LRU order.

5. Empty Process. Such a process does not contain any active program components. The system may close such processes at any time.

In a sense, the garbage collection mechanism frees programmers from the memory management nightmare, the process lifecycle management mechanism of Android frees users from the nightmare of "task management. Android uses Java as an application API, and uses its unique lifecycle management mechanism to provide developers and users with maximum convenience.

Activity
Lifecycle

Common functions in activity include setcontentview () findviewbyid () finish () startactivity (). The following are the functions involved in the lifecycle:

Void oncreate (bundle savedinstancestate)

Void onstart ()

Void onrestart ()

Void onresume ()

Void onpause ()

Void onstop ()

Void ondestroy ()

Note that to use an activity, you must add the corresponding content in the manifest file and set its attributes and intent-filter.

Service
Lifecycle

Service can use context. startservice () or context. bindservice () is created through context. stopservice (), service. stopself (), service. stopselfresult () or context. unbindservice. Its lifecycle involves the following functions:

Void oncreate ()

Void onstart (intent)

Void ondestroy ()

Oncreate () and ondestroy () can be called by all services, whether initiated by context. startservice () or context. bindservice. However, onstart () can only be called by a service initiated by startservice.

If a service is bound to another object, you need to extend the following callback method:

Ibinder onbind (intent)

Boolean onunbind (intent)

Void onrebind (intent)

Broadcastreceiver
Lifecycle

Only one method is included: void onreceive (context curcontext, intent broadcastmsg)

Broadcastreceiver containing active components will not be shut down by the system, but processes that only contain inactive components will be shut down at any time (when other components require memory ).

3.
Intent
Introduction --
Android
Innovative navigation and triggering mechanism

Previously, we introduced four first-class citizens of Android: Activity, contentprovider, broadcastreceiver, and service ). These four components are independent and can be called and coordinated to form a real Android Application.

The communication between these components is mainly completed with intent assistance. Intent is an important component in Android Application Development. Using intent, you can start another arbitrary activity from one activity, whether defined by yourself or by the system. In the activitygroup (extends activity), the flag setting of intent is crucial to the Startup Mode of the Child Activity.

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 acts as a media intermediary here, providing information about component calls to each other to decouple callers from callers.

The Android Application Framework is powerful in introducing web habits into mobile applications. This does not mean that the platform provides a powerful browser and is not limited to using JavaScript and server resources, it involves the working principle of the Android platform and how users of the platform interact with mobile devices. The power of the Internet lies in that everything can be obtained by one click. The content of these clicks is URL (uniformresource locator, unified Resource Locator) or Uri (Uniform Resource Identifier, unified resource identifier) for users ). You can use the URI to conveniently and quickly access the required daily information. "Send the link to me" means everything.

The platform that copies desktop device experience on mobile devices can only attract a small number of Loyal users. Multi-level menus and multiple clicks are generally not accepted in the mobile market. Mobile apps require higher intuitive and easy-to-use than apps in any other market. Intent and intentfilter introduce the "click" example to the core of using and developing mobile apps on the Android platform.

Intent
Structure

Intent is an abstract description of an operation. It describes the following content:

First,
A brief description of the action to be executed, such as view_action (View) and edit_action (modify). Android defines a set of standard actions for us:

Main_action

View_action

Edit_action

Pick_action

Get_content_action

Dial_action

Call_action

Sendto_action

Answer_action

Insert_action

Delete_action

Run_action

Login_action

Clear_credentials_action

Sync_action

Pick_activity_action

Web_search_action

In addition, we can also define our own actions based on the needs of the application, and define the corresponding activity to process our custom actions.

Second,
The data to be operated. Android uses a URI pointing to the data. For example, in a contact application, a URI pointing to a contact may be: content: // contacts/1. This type of Uri representation is described through the contenturi class. For details, refer to the documentation of the android.net. contenturi class.

In addition,
In addition to the two important attributes of action and data, there are also some additional attributes:

Category (category), additional information about the executed action. For example, launcher_category indicates that the receiver of intent should appear as a top-level application in launcher, while alternative_category indicates that the current intent is one of a series of optional actions that can be executed on the same piece of data.

Type (data type), explicitly specifying the intent data type (MIME ). Generally, the intent data type can be determined based on the data itself. However, by setting this attribute, You can forcibly use the explicitly specified type instead of derivation.

Component (component), specifies the Class Name of the intent's target component. Generally, Android searches for other attributes in intent, such as action, data/type, and category, and finds a matched target component. However, if this attribute of component is specified, the component specified by component will be used directly without executing the above search process. After this attribute is specified, all other intent attributes are optional.

Extras (additional information) is a collection of all other additional information. You can use extras to provide extended information for components. For example, if you want to perform the "send email" action, you can save the email title and body in extras, send to the email sending component.

In short,
Action, data/type, category, and extras form a language. This language enables the system to understand phrases such as "View Details of a contact. As applications are constantly added to the system, they can add new actions, data/type, and category to extend the language. Applications can also provide their own activities to process existing "phrases" and change the behavior of these "phrases.

Analysis
Intent

In applications, we can use intent in two forms:

· Explicit (direct) intent: Specifies the intent of the Component Attribute (call setcomponent (componentname) or setclass (context, class ). Notify the application to start the corresponding component by specifying a specific component class.

· Implicit (indirect) intent: the intent of the Comonent attribute is not specified. The intent must contain sufficient information so that the system can determine the components that meet the intent among all available components based on the information.

Android does not need to parse explicit (direct) intent because the target component is already clear. Android needs to parse implicit (indirect) intent, map intent to the activity, broadcastreceiver, or service that can process the intent.

The intent parsing mechanism mainly finds the matching intent by finding all intentfilters registered in androidmanifest. XML and the intent defined in them. In this parsing process, Android uses the intent action, type, and category attributes to determine the attributes. The judgment method is as follows:

· If intent specifies an action, the action must be included in the action list of the intentfilter of the target component; otherwise, the action cannot be matched;

· If the intent does not provide the type, the system will obtain the data type from the data. Like action, the Data Type list of the target component must contain the intent data type. Otherwise, the data type does not match.

· If the data in the intent is not a content: Type Uri and the intent does not explicitly specify its type, it will match according to the scheme (such as http: or mailto :) of the data in the intent. Similarly, the scheme of intent must appear in the scheme list of the target component.

· If intent specifies one or more category categories, all these categories must appear in the Set category list. For example, intent contains two categories: launcher_category and alternative_category. The parsed target component must contain at least these two categories.

Intent
And
Intentfilter

Intent is a required statement.

Intentfilter is a statement of ability and interest to provide assistance when necessary.

Intent consists of a series of information describing the required actions or services. This section describes the requested actions and related data.

Intentfilter can be generic or specific to providing services to some intent.

The action attribute of intent is usually a verb, such as view, pick, or edit. Many built-in intent actions are defined as members of the intent class. Application developers can also create new actions. To view information, the application can use the following intent Action: The data part of the intent is represented in the URI format and can be any part of the information, for example, a contact record, a website location, or a reference to a media clip.

Intentfilter defines the relationship between intent and applications. Intentfilter can be related to Part Of Intent data or action, or both. Intentfilter also contains a Category field. CATEGORY helps you classify actions. For example, the category_launcher category indicates that the activity that android contains this intent-filter should be visible on the main application initiator or the main interface.

After intent is distributed, the system calculates available activities, services, and registered broadcastreceiver, and delivers intent to most appropriate recipients.

Intentfilter is usually defined using tags in androidmanifest. xml of an application. In essence, the androidmanifest. xml file is an application descriptor file.

The next section describes how Android displays UI elements on the screen of a mobile device.

4.
Android
View -- display the user interface (
UI
) Element

Android activity displays UI elements in the view. The view adopts one of the following layout designs:

· Linearvertical-each subsequent element is placed under the first element to form a single column.

· Linearhorizontal-each subsequent element is placed on the right of the previous element to form a single row.

· Relative-each subsequent element has a certain offset relative to the previous element.

· Table-a series of rows and columns similar to HTML tables. Each cell can contain a view element.

After selecting a layout (or a combination of layout), you can use each view to display the UI.

View elements are composed of familiar UI elements, including:

· Button

· Imagebutton

· Edittext

· Textview (similar to label)

· Checkbox

· Radio button

· Gallery and imageswitcher (used to display multiple images)

· List

· Grid

· Datepicker

· Timepicker

· Spinner (similar to the combo box)

· AutoComplete (edittext with automatic text completion)

A view is defined in an XML file. Each element has one or more attributes that belong to the android namespace.

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.