Today i read the book<Android - A Programmer's Guide> written by J.F. DiMarzio .
1. downloading and installing Java Runtime Environment (JRE) The JRE allows you to run Java-based applications, but it does not allow you to create them. To create Java applications, you need to download and install the Java Development Kit (JDK), which contains all the tools and libraries needed to create Java applications. If you are not familiar with Java, keep these differences in mind.
Sun Developer Network (SDN) Downloads page at http:// developers.sun.com/downloads/,
2. downloading and installing Eclipse the Eclipse Downloads page at www.eclipse.org/downloads Download the Eclipse IDE for Java Developers from this site. The package is relatively small (79MB) and should download fairly quickly. Be sure not to download the Eclipse IDE for Java EE Developers, as this is a slightly different product and I will not be covering its usage.
3. download and install the Android SDK download the Android SDK from the Google Android development site, located at http://code.google.com/android
4. download and install the Android plugin for Eclipse 1. Open the Eclipse application. You will download the Android plugin for Eclipse from within the Eclipse IDE. 2. Choose Help | Software Updates | Find and Install. 3. In the Install/Update window, which allows you to begin the process of downloading and installing any of the plugins that are available to you for Eclipse, click the Search for New Features to Install radio button and then click Next. 4. The Update Sites to Visit page of the Install window, shown next, lists all the default websites used for obtaining Eclipse plugins. However, the plugin you want, Android for Eclipse, is not available from the default sites. To download the Android plugin, you must tell Eclipse where to look for it, so click the New Remote Site button. 5. In the New Update Site dialog box, shown next, you must enter two pieces of information to continue: a name for your new site, and its associated URL. The name is only for display purposes and does not affect the downloading of the plugin. In the Name field, enter Android Plugin. In the URL field, enter the URL from which Eclipse will obtain information about the plugins that are available: https://dl-ssl.google.com/android/eclipse/. Click OK. 6. A new site named Android Plugin should now be in your list of available sites: At this point Eclipse has not yet looked for the plugin; this is just a list of paths that you can tell Eclipse to check when looking for new plugins to install. 7. Check the check box next to Android Plugin and then click Finish. Eclipse searches the URL associated with the Android Plugin site for any available plugins. 8. On the Search Results page of the Updates window, select the Android Plugin and then click Finish. 9. On the Feature License page of the Install window, shown next, accept the licensing agreement for the Android Development Tools and click Next.
Keep in mind that all Eclipse plugins are installed to the %installpath%/eclipse/plugins directory. This information will help you if you need to locate the files that make up the Android plugin.
10. Eclipse downloads the Android plugin. At the time of this writing, the plugin version is 0.4.0.200802081635. On the final plugin installation page, Feature Verification, click Install All to complete the installation of the Android plugin. With the Android plugin installed, the last step you have to perform is to configure the plugin.
5. configuring the Android plugin for Eclipse The Android plugin for Eclipse is configured from the Preferences window of Eclipse. Proceed as follows: 1. From the main Eclipse window, choose Window | Preferences. 2. In the Preferences window, shown next, select Android in the menu on the left. On the right side of the window, click Browse, find the location of the Android SDK on your hard drive, and enter it in the SDK Location field. Eclipse needs this information to be able to access all the tools that are supplied with Android, such as the emulator. 3. Check the Automatically Sync Projects to Current SDK check box and then click Apply. 4. The final step in setting up the Android SDK is to put it into your PATH statement. If you are using a Microsoft Windows machine, right-click Computer (or My Computer, depending on your version of Windows) and select Properties to open the System Properties dialog box. Click the Advanced tab. 5. Click Environment Variables to display the window of the same name, shown in the following illustration. This is where you can edit your PATH statement. 6. Under System Variables, find the variable PATH and double-click it. 7. In the Edit System Variable dialog box, shown next, add the location of your Android SDK?aseparated from the existing paths by a semicolon?aand click OK to apply your changes, and click OK again in the Environment Variables window.
I suggest that anyone faced with the possibility of upgrading from one version of an SDK/plugin combo to another use this same process: simply uninstall the older version, and install the newer one, rather than upgrading.
6. What is the Android SDK? The bulk of the Android SDK, in number of files, consists of documentation, with programming APIs, tools, and samples comprising the rest.
While there are a few files in the root folder, like android.jar (a compiled Java application containing the core SDK libraries and APIs) and some release notes, the remainder of the Android SDK is divided into three main folders: Docs Contains all of the accompanying Android documentation Samples Contains six sample applications that you can compile and test from within Eclipse Tools Contains all of the development, compilation, and debugging tools that you need throughout the development process of an Android application
6.1 The Android documentation The Android documentation is located in the Docs folder within the Android SDK at ../%sdk folder%/DOCS. The documentation is in HTML format and can be accessed though the documentation.html file in the root of the SDK folder. For me, the most valuable segments of the Android SDK documentation are as follows (as they appear in the navigation bar): Reference Information ?? Class Index ?? List of Permissions ?? List of Resource Types FAQs ?? Troubleshooting
Using Eclipse, load the API Demos application as a New Android Project. To do this, select File | New | Project from the Eclipse menu bar; a New Android Project wizard opens. Do not worry about the options in this wizard for now. Simply select Create Project From Existing Source and browse to the folder with the API Demo application in it. When the project is loaded, choose Run to see it execute in the Android Emulator.
6.2 The Android Tools emulator.exe emulator.exe is going to be your only means to test applications on a ?°native?± platform. adb.exe Android Debug Bridge or adb This tool allows you to issue commands to the Emulator.exe tool. the adb tool allows you to do the following: ?? Start and stop the server ?? Install and uninstall applications ?? Move files to and from the emulator MKSDCARD.exe is a very useful tool if you are testing an application that will need to read or write files to or from an SD Memory Card inserted into the mobile device. DX.exe is the compiler of the Android SDK. When run against your Java files, DX.exe will create files with .dex extensions?aDalvik executable format. activityCreator(.bat or .pn) is a simple command-line tool that is used to set up a basic development environment.If you are in a Windows environment, this will be a .bat file; otherwise it will be a python (.pn) script.
Extensible Messaging and Presence Protocol (XMPP)developed by the Jabber open source community. Using XMPP, applications can quickly become aware of other clients?ˉ presence and availability for the purpose of messaging and communications.
6.3 standard ASP application life cycle These steps are required to be implemented by all ASP applications, and really define what an ASP application is. The steps, in order, are 1. Application_Start 2. Event 3. HTTPApplication.Init 4. Disposal 5. Application_End 6.4 The Android application life cycle The Android application life cycle is unique in that the system controls much of the life cycle of the application. All Android applications, or Activities, are run within their own process. Some of the specific methods called during the life cycle of an android activity are ?? onCreate ?? onStart ?? Process-specific events (for example: launching activities or accessing a database) ?? onStop ?? onDestroy
7. The application and Activity You will often see ?°application?± and ?°Activity?± used interchangeably. The difference between the two is that an application can be composed of multiple Activities, but one application must have at least one Activity. Each ?°window?± or screen of your application is a separate Activity. Therefore, if you create a fairly simple application with only one screen of data (like the Hello World! application in this chapter), that will be one Activity. In future chapters you will create applications with multiple Activities.
CAUTION Do not select Java Project from the New menu. While Android applications are written in Java, and you are doing all of your development in Java projects, this option will create a standard Java application. Selecting Android Project enables you to create Android-specific applications. If you do not see the option for Android Project, this indicates that the Android plugin for Eclipse was not fully or correctly installed. Review the procedure in Chapter 3 for installing the Android plugin for Eclipse to correct this.
The New Android Project wizard creates two things for you: ?? A shell application that ties into the Android SDK, using the android.jar file, and ties the project into the Android Emulator. This allows you to code using all of the Android libraries and packages, and also lets you debug your applications in the proper environment. ?? Your first shell files for the new project. These shell files contain some of the vital application blocks upon which you will be building your programs. In much the same way as creating a Microsoft .NET application in Visual Studio generates some Windows-created program code in your files, using the Android Project wizard in Eclipse generates your initial program files and some Android-created code.
If the Package Explorer is not open, you can activate it by choosing Window | Show View | Package Explorer.
7.1 AndroidManifest.xml file The AndroidManifest.xml file is where your global settings are made. The standard AndroidManifest.xml file should contain the following information: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=http://schemas.android.com/apk/res/android package="testPackage.HelloWorldText"> <application android:icon="@drawable/icon"> <activity class=".HelloWorldText" android:label="@string/app_name"> <intent-filter> <action android:value="android.intent.action.MAIN" /> <category android:value="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
7.2 Referenced Libraries A list of the Referenced Libraries is also included in the root of the project. Given that this is a new Android project, you will see one library in your project?ˉs references, android.jar, the Android SDK. The application needs to reference the SDK to gain access to all the classes contained in the SDK libraries, such as your Views, Controls, and even the Google API.
7.3 directories There are also three directories in the project root?ares, assets, and src?aeach of which has a distinct purpose.
7.3.1 res directory The res directory is where your in project resources are held and compiled into your application. When you create a new Android project, the res directory contains three subdirectories: drawable, layout, and values. You will use the drawable and layout directories in many of your projects to hold and display images and layouts respectively, whereas the values directory holds string globals that can be used throughout your application. NOTE A reference to the res directory and its contents is contained by the R.java file, located in the src directory.
7.3.1.1 drawable directory The drawable directory contains actual image files that your application can use and reference.
7.3.1.2 layout directory The layout directory holds an XML file, main.xml, that is referenced by your application when building its interface. An unaltered main.xml file contains the following code: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, HelloWorldText" /> </LinearLayout>
7.3.1.3 values directory The last directory under res, values, holds an XML file named strings. The strings.xml file is used to hold global string values that can be referenced by your application.
7.3.2 assets directory The assets directory is used to hold raw asset files. The files contained in the assets directory can include audio files for streaming and animation assets.
7.3.3 src directory The src directory contains all the source files for your project. When your project is first created, it will contain two files, R.java and <activity>.java (in this example, HelloWorldText.java), described next.
NOTE <activity>.java is always named according to your Activity name.
7.3.3.1 R.java The file R.java is an auto-generated file that is added to your application by the Android plugin. This file contains pointers into the drawable, layout, and values directories (or the items within the directories, as is the case with strings and icons). You should never have to modify this file directly. You will be referencing R.java in most of your applications.
7.4 program the UI in code rather than by using the xml file you will program the UI in code rather than by using the xml file?aand you will actually do most of the work. The first step here is to remove the TextView code that is in main.xml. The following section of code represents the TextView. Removing it essentially makes your application an empty shell. <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, HelloWorldText" /> After you have removed the TextView code, your main.xml file should look like this: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > </LinearLayout> Now that you have a clean main.xml file, and thus a clean application shell, you can begin to add the code that will display ?°Hello World!?± on the screen. Start by opening the HelloWorldText.java file and removing the following line: setContentView(R.layout.main); This line uses setContentView( ) to draw the main.xml file to the screen. Since you will not be using main.xml to define your TextView, you will not be setting it to your view. Instead, you will be building the TextView in code. Your next step is to import the package TextView from android.widget. This will give you access to the TextView and let you create your own instance of it. Place this code near the top of your current HelloWorldText.java file, where the existing import statements are import android.widget.TextView; Now, create an instance of TextView. By creating the TextView instance, you can use it to display text to the screen without directly modifying main.xml. Place the following code after the onCreate( ) statement is fired: TextView HelloWorldTextView = new TextView(this);
The preceding line creates an instance of TextView named HelloWorldTextView and then instantiates HelloWorldTextView, by setting it to a new TextView. The new TextView is passed the context of this to be fully instantiated. Now that the TextView is defined, you can add your text to it. The following line of code assigns the text ?°Hello World!?± to the TextView: HelloWorldTextView.setText("Hello World!"); You have to use the following code to set TextView to the context and display it to the screen: setContentView(HelloWorldTextView);
5. In the New Update Site dialog box, shown next, you must enter two pieces of
information to continue: a name for your new site, and its associated URL. The
name is only for display purposes and does not affect the downloading of the plugin.
In the Name field, enter Android Plugin. In the URL field, enter the URL from
which Eclipse will obtain information about the plugins that are available:
https://dl-ssl.google.com/android/eclipse/. Click OK.
It should be http://dl-ssl.google.com/android/eclipse/
用户1205135 2009-6-14 23:30
ash_riple_768180695 2007-5-29 09:25
好文!