Thứ Sáu, 25 tháng 4, 2014

Which avd will be used to test your application? [Getting Started with Android Programming]

Recall that earlier you created a few AVDs using the AVD Manager. So which one
will be launche by Eclipse when you run an Androi application? Eclipse checks the target that you specified (when you created a new project) comparin it against
the list of AVDs that you have created The first one that matches will be launche 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 Androi 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 othproject

Application name                    A user-friendly name for your application

Package name                            The name othpackage. You should use a reverse domain name for this.

Create Activity                          The name othrst activity in your application

Min SDK Version                       The minimum version othSDK that your project is targeting


In Android an activity is a windo that contain the user interface of your applications. Aapplication can have zero or more activities; in this example the application contain one activityHelloWorldActivity. ThisHelloWorldActivity is the entry point of the application, which idisplayed 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 contain the user interface of the activity, which idisplayed when HelloWorldActivityis loaded.

When you debug the application on the Androi emulator, the application is automatically installed on the emulator. And thats it  you have developed your first Androi application!

The next section unravels  how all the various files in your Androi project work together  to make youapplication come alive.

ANATOMY OF AN ANDROID APPLICATION


Now that you have created your first Hello World Androiapplication, it is time to dissect the innard of the Android project and examine all the parts that make everything work.

First, note the various files that make up an Androi project ithe 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 Androi 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 Hell WorldHelloWorldActivity!:

<?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 localizyour 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 contai 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 defaullanguage your application will automatically display the hello string in French.

The next important file in an Androi project is the manifest file. Note the conten 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 contain detailed information abou 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 conten of
R. java, which currentl contain 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  {
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 suppose to modify the conten of the R.java file; Eclipse automatically generates the conten for you when you modify your project.

Finally, the code that connects the activity to the UI (main.xml) is the setContentView() methodwhich 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 generate in
the R.java file. The onCreate() metho is one of many method that are fired when an activity iloaded Chapter 2 discusses the life cycle of an activity in more detail.


SUMMARY

This chapter  has provide a brief overview of Android and highlighte some of its capabilities Iyou have followed the sections on downloading the tools and the Androi SDK, you should nohave a workin system one that is capable of developing more interestin Androi applications other than the Hello World application. In the next chapter you will learn abou the concepts of activities and intents and the very important roles theyplay in Android.

Không có nhận xét nào:

Đăng nhận xét