Use eclipse to develop and debug Android applications (2)

Source: Internet
Author: User

 

II,
Create
Android eclipse
Engineering

Please refer to Xiaosheng's Android SDK 2.3 and the latest version of eclipse development environment to build (5), blog address: http://blog.sina.com.cn/s/blog_573860a90100ownd.html or http://blog.csdn.net/deaboway/archive/2011/01/30/6170449.aspx

III,
Detailed analysis of Engineering Structure

1. Engineering Structure

In the Java perspective of Eclipse, you can see the package explorer of the project, for example:

Including:

SRC
Folder
-Contains the package of the sample application, org. deaboway. Test.

R. Java
-Android developer tools automatically creates this file, which provides the constants required to access various resources of Android applications. The relationship between R classes and resources will be discussed in detail later.

Helloandroidworld. Java
-Implementation of the main activity class of the application.

Referenced Libraries
-Contains Android. jar, which is the JAR file of the android runtime class in the android SDK.

Res
Folder
-Contains application resources, including:

· Drawable-icon: this folder contains graphic files, comparison labels and bitmaps.

· Layout-layout file: this folder contains XML files indicating the application layout and view.

· Values-string: this folder contains the strings. xml file. This is the main method to implement string localization for applications.

Andriodmanifest. xml
-Deployment descriptor of the sample application.

Default. Properties
And proguard. cfg
-Since Android 2.3, the android SDK itself contains proguard, which can be obfuscated by normal compilation, we can see that a proguard folder is added under Android-SDK-Windows/tools. Prevents the decompilation tool from decompiling to ensure code security.

2. Primary Analysis

This example application is composed of an activity, helloandroidworld. As mentioned above, the helloandroidworld class is implemented in the helloandroidworld. Java file.

Package
Org. deaboway. test;

Import
Android. App. activity;

Import
Android. OS. Bundle;

Import
Android. widget. textview
;

Public
Class
HelloandroidworldExtends
Activity {

/** Called when the activity is first created .*/

@ Override

Public
Void
Oncreate (bundle savedinstancestate ){

Super
. Oncreate (savedinstancestate );

// Textview = new textview (this );

// Textview. settext ("Hello android
World! ");

// Setcontentview (textview );

Setcontentview (R. layout.Main
);

}

}

Note the following points in the source code snippet:

  • Helloandroidworld is a common Java class that contains packages and import statements.
  • Helloandroidworld extends the android base activity in the Android. app package.
  • The oncreate () method is the entry point of this activity. It accepts a bundle type parameter. The bundle class is essentially a package of map or hashmap. In this parameter, pass the elements required to construct the activity. This parameter is not discussed here.
  • Setcontentview () is responsible for using the R. layout. Main Parameter to create the main UI. R. layout. Main is the identifier of the main layout in the Application resource.

3. R. Java File

The R. Java file is automatically created during build, so do not modify it manually, because all modifications will be lost. In this example, the content of the R. Java file is as follows:

/* Auto-generated file. Do not modify.

*

* This class was automatically generated by

* Aapt
Tool from the resource data it found. It

* Shoshould not be modified by hand.

*/

Package
Org. deaboway. test;

Public
Final
Class
R {

Public
Static
Final
Class
ATTR {

}

Public
Static
Final
Class
Drawable {

Public
Static
Final
Int
Icon
= 0x7f020000;

}

Public
Static
Final
Class
Layout {

Public
Static
Final
Int
Main
= 0x7f030000;

}

Public
Static
Final
Class
String {

Public
Static
Final
Int
App_name
= 0x7f040001;

Public
Static
Final
Int
Hello
= 0x7f040000;

}

}

The r class contains some anonymous sub-classes. Each sub-class contains the identifiers of various resources described above. Note that these classes are static.

Note the elements represented by R. layout. Main. This identifier represents the layout defined by main. xml. You can use this value in the oncreate method of the activity: setcontentview (R. layout. Main ). This is how to associate a specific activity (helloandroidworld here) with a specific layout (main layout) at runtime.

4. Main. xml file

This example contains an activity and a view. The application contains a file named main. XML, which represents the visual aspect of the active main UI.

Note that the layout is not specified in Main. xml. This means that you can use it in multiple activities if needed.

& Quot; 1.0 & quot; encoding ="UTF-8"
?>

Http://schemas.android.com/apk/res/android"

Android: Orientation ="Vertical"

Android: layout_width ="Fill_parent"

Android: layout_height ="Fill_parent"

>

 

Android: layout_width ="Fill_parent"

Android: layout_height ="Wrap_content"

Android: text ="@ String/hello"

/>

 

This is the simplest layout, with only one vertical linear layout, which means that all elements are arranged in a column. Here is a textview element, which is similar to labels in other development environments. Textview indicates the static text that cannot be edited.

Note that each view element (such as linearlayout and textview in this example) has an attribute that belongs to the android namespace. Some attributes are available to all view elements, such as Android: layout_width and Android: layout_height. These attributes can use the following values:

Fill_parent-fill the available space with view elements. You can also think that this is "stretching ".

Wrap_content-this value allows android to arrange elements one by one without stretching.

5. androidmanifest. xml file

The androidmanifest. xml file is the deployment descriptor of the android application. This file lists all activities, contentprovider, broadcastreceiver, service, and intentfilter supported by the application. The following is the complete androidmanifest. xml file of the example application.

& Quot; 1.0 & quot; encoding ="UTF-8"
?>

Http://schemas.android.com/apk/res/android"

Package ="Org. deaboway. test"

Android: versioncode ="1"

Android: versionname ="1.0"
>

"@ Drawable/icon" Android: Label ="@ String/app_name"
>

". Helloandroidworld"

Android: Label ="@ String/app_name"
>

 

"Android. Intent. Action. Main"/>

"Android. Intent. Category. launcher"/>

 

 

 

 

  • The package name in the source file is specified here. Similar to Java source files and import statements. The actual purpose of the tag is to "import" the class in this package. In this file, all non-fully qualified classes belong to the packages specified by the package attribute.
  • A tag property references a resource of the application. Note the @ symbol before the drawable identifier. It means to find the resource named "icon" in the drawable folder of the Application resource.
  • The tag contains the following attributes and values:
    • Android: name indicates the Java class that implements this activity.
    • Android: label is the name of the application. Note that it comes from a string resource. The string. xml file contains the localized strings of the application.
    • Indicates the intentfilter available in the application. This is the most common intentfilter in Android applications. The actual meaning of this filter is that it implements the "master" Operation (that is, the entry point), and it is located in the OS initiator. This means that it can be started from the application master list on the Android device just like other applications are started.

IV,
Application debugging

To check what is happening in the running application, you need to check the running Dalvik VM. In eclipse, choose Window> open perspective> Other. Select ddms in the displayed dialog box. This will open a new perspective in eclipse, which has many interesting windows.

Ddms serves as a bridge between IDE and emultor (or gphone. The developer can view the running process/thread status on the target machine through ddms: the eclipse program can be connected to the development machine for running; the heap information, logcat information, and memory allocation of the process can be viewed; you can send text messages, send geographic location information, and call messages on the target machine. You can attach a process for debugging like GDB.

The following briefly introduces the resources provided in the ddms perspective:

1. Devices

All processes in emulator are listed in device. The buttons in the upper-right corner of the tab are: debug the selected process debugging process, update heap update process stack information, dump hprof file check hprof (Heap/CPU profiling tool) file, cause GC call garbage collection, update process update threads, start method profiling start method analysis, stop process stop a process, and screen capture the current screen of emulator. When you select a process and press the debug process button, if eclipse contains the code of this process, you can perform source code-level debugging. It's a bit like GDB attach. The image capture button can capture the current Android display desktop to your machine, which is also very useful.

The details of all the terminals connected to ddms and the app processes running on each terminal can be viewed here. the rightmost of each process corresponds to the port connected to the debugger. Because Android is developed based on the Linux kernel, it also retains the unique process ID in Linux, which is between the process name and port number.

The port used by ddms to listen to the app process on the first terminal is 8600, and the app process is allocated 8601. If there are more terminals or more app processes, and so on. Ddms receives commands from all terminals through port 8700 ("base port.

2. emulator Control

Some functions of this panel can easily enable the test terminal to simulate some interactive functions of a real mobile phone, such as answering a call and simulating various network conditions based on the options, simulate receiving SMS messages and sending virtual address coordinates to test the GPS function.

Telephony status: simulates the voice quality and signal connection mode with options.

Telephony actions: simulates the call and sends the SMS to the test terminal.

Location Control: simulates geographical coordinates or simulates Dynamic Route coordinate changes and displays preset geographical identifiers. You can use the following three methods:

· Manual: manually send two-dimensional latitude and longitude coordinates to the terminal.

· GPX: The GPX file is used to import the sequence to dynamically change the geographical coordinates, so as to simulate the GPS change value during traveling.

· Kml: Import unique geographical identifiers through the kml file and display them on the test terminal dynamically based on the changed geographical coordinates.

For example, enter "android Chinese" in emulator control/telephony actions, click send, and open messaging in the android simulator. The following text message is displayed:

3. File Manager

Displays the file system information. File explorer is very useful: it can upload files to Android, download files from Android, or delete files. The upload, download, and delete buttons are available in the upper-right corner of the tab. Generally, file explorer has the following three directories: data, sdcard, and system.

· The ram of the mobile phone corresponding to data will store temporary data such as Cache during Android OS running (/data/Dalvik-cache directory ); if you do not have root permission, the APK program is installed in/data/APP (only the APK file itself is stored);/data stores all android programs (system APK + third-party APK).

· MNT indicates mount. Mounting means that the sdcard under the MNT directory corresponds to the SD card.

· The ROM and OS of the mobile phone corresponding to system and the APK program of the system are stored here.

User applications are deployed in the/data/APP directory, while Android built-in applications are deployed in the/system/APP directory.

4. threads, heap, allocation Tracker

The threads view lists all threads of the current process.

The heap view displays the heap status, which is updated during garbage collection. When a VM is selected, the VM heap view cannot display data. You can click the "show heap updates" button on the right of the VM, then click "cause GC" to implement garbage collection and update the heap status.

In the allocation tracker view, we can track the memory allocation of each selected virtual machine. Click Start tracking and click get allocations.

For example, to view garbage collection information, click the [start tracking] button-> keep the program running for a period of time-> click the [get allocations] button, with the obtained allocations information, you can use the line number to accurately find out where the code is faulty.

5. logcat

Logcat is a log file that records activities in the VM. The application can add its own log entries through log. I (TAG, message); in this log file, the tag and message are both Java strings. The log class belongs to the Android. util. Log package.

6. Console

Android output information, loading program and other information.

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.