Recall that earlier you created a few AVDs using the AVD Manager. So which one
will be launched by Eclipse when you run an Android application? Eclipse checks the target that you specified (when you created a new project), comparing it against
the list of AVDs that you have created. The first one that matches will be launched to run your application.
If you have more than one suitable AVD running prior to debugging the application, Eclipse will display the Android Device Chooser dialog, which enables you to select the desired emulator/device to debug theapplication (see Figure 1-34).
FIGURE 1-34
How It Works
To create an Android project using Eclipse, you need to supply the information shown in Table 1-2.
TABLE 1-2: Project Files Created by Default
PROPERTIES DESCRIPTION
Project name The name of the project
Application name A user-friendly name for your application
Package name The name of the package. You should use a reverse domain name for this.
Create Activity The name of the first activity in your application
Min SDK Version The minimum version of the SDK that your project is targeting
In Android, an activity is a window that contains the user interface of your applications. An application can have zero or more activities; in this example, the application contains one activity: HelloWorldActivity. ThisHelloWorldActivity is the entry point of the application, which is displayed when the application is started. Chapter 2 discusses activities in more detail.
In this simple example, you modified the main.xml file to display the string “This is my first Android Application!” and a button. The main.xml file contains the user interface of the activity, which is displayed when HelloWorldActivityis loaded.
When you debug the application on the Android emulator, the application is automatically installed on the emulator. And that’s it — you have developed your first Android application!
The next section unravels how all the various files in your Android project work together to make your application come alive.
ANATOMY OF AN ANDROID APPLICATION
Now that you have created your first Hello World Android application, it is time to dissect the innards of the Android project and examine all the parts that make everything work.
First, note the various files that make up an Android project in the Package Explorer in Eclipse (see Figure 1-35).
The various folders and their files are as follows:
➤ src — Contains the .java source files for your project.
In this example, there is one file, HelloWorldActivity
.java. The HelloWorldActivity.java file is the source file for your activity. You write the code for your application in this file. The Java file is listed under the package name for your project, which in this case is net
.learn2develop.HelloWorld.
➤ gen — Contains the R.java file, a compiler-generated
file that references all the resources found in your project. You should not modify this file. All the resources in your project are automatically compiled into this class so that you can refer to them using the class.
➤ Android 4.0 library — This item contains one file, android.jar, which contains all the class libraries needed for an Android application.
➤ assets — This folder contains all the assets used by your application, such as HTML, text files, databases, etc.
➤ bin — This folder contains the files built by the ADT during the build process. In particular, it generates the .apk file (Android Package). An .apk file is the application binary of an Android application. It contains everythingneeded to run an Android application.
FIGURE 1-35
➤ res — This folder contains all the resources used in your application. It also contains a few other subfolders: drawable-<resolution>, layout, and values. Chapter 3 talks more about how you can support devices with different screenresolutions and densities.
➤ AndroidManifest.xml — This is the manifest file for your Android application. Here you
specify the permissions needed by your application, as well as other features (such as intent-filters, receivers, etc.). Chapter 2 discusses the use of the AndroidManifest.xml file in more detail.
The main.xml file defines the user interface for your activity. Observe the following in bold:
<TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”@string/hello” />
The @string in this case refers to the strings.xml file located in the res/values folder. Hence,
@string/hello refers to the hello string defined in the strings.xml file, which is “Hello World, HelloWorldActivity!”:
<?xml version=”1.0” encoding=”utf-8”?>
<resources>
<string name=”hello”>Hello World, HelloWorldActivity!</string>
<string name=”app_name”>HelloWorld</string>
</resources>
It is recommended that you store all the string constants in your application in this strings.xml file and reference these strings using the @string identifier. That way, if you ever need to localize your application to another language, all you need to do is make a copy of the entire values folder and modify the values of strings.xml to contain the string in the language that you want to display. Figure 1-36 shows that I have another folder named values-fr with thestrings.xml file containing the same hello string in French.
FIGURE 1-36
If the user loads the same application on a phone configured to display French as the default language, your application will automatically display the hello string in French.
The next important file in an Android project is the manifest file. Note the content of the
AndroidManifest.xml file:
<?xml version=”1.0” encoding=”utf-8”?>
package=”net.learn2develop.HelloWorld” android:versionCode=”1” android:versionName=”1.0” >
<uses-sdk android:minSdkVersion=”14” />
<application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” >
<activity android:label=”@string/app_name” android:name=”.HelloWorldActivity” >
<intent-filter >
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>
</manifest>
The AndroidManifest.xml file contains detailed information about the application:
➤ It defines the package name of the application as net.learn2develop.HelloWorld.
➤ The version code of the application is 1 (set via the android:versionCode attribute).
This value is used to identify the version number of your application. It can be used to programmatically determine whether an application needs to be upgraded.
➤ The version name of the application is 1.0 (set via the android:versionName attribute).
This string value is mainly used for display to the user. You should use the format
<major>.<minor>.<point> for this value.
➤ The android:minSdkVersion attribute of the <uses-sdk> element specifies the minimum version of the OS on which the application will run.
➤ The application uses the image named ic_launcher.png located in the drawable folders.
➤ The name of this application is the string named app_name defined in the strings.xml file.
➤ There is one activity in the application represented by the HelloWorldActivity.java file.
The label displayed for this activity is the same as the application name.
➤ Within the definition for this activity, there is an element named <intent-filter>:
➤ The action for the intent filter is named android.intent.action.MAIN to indicate that this activity serves as the entry point for the application.
➤ The category for the intent-filter is named android.intent.category.LAUNCHER to indicate that the application can be launched from the device’s launcher icon. Chapter 2 discusses intents in more detail.
As you add more files and folders to your project, Eclipse will automatically generate the content of
R. java, which currently contains the following:
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package net.learn2develop.HelloWorld;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=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;
}
}
You are not supposed to modify the content of the R.java file; Eclipse automatically generates the content for you when you modify your project.
Finally, the code that connects the activity to the UI (main.xml) is the setContentView() method, which is in the HelloWorldActivity.java file:
package net.learn2develop.HelloWorld;
import android.app.Activity;
import android.os.Bundle;
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
}
}
Here, R.layout.main refers to the main.xml file located in the res/layout folder. As you add additional XML files to the res/layout folder, the filenames will automatically be generated in
the R.java file. The onCreate() method is one of many methods that are fired when an activity is loaded. Chapter 2 discusses the life cycle of an activity in more detail.
Không có nhận xét nào:
Đăng nhận xét