1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

6  Set Up an IDE on Windows to Develop for Android

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.04 MB, 688 trang )


Figure 1-15.



Figure 1-16.



Problem

Some owners of Android mobile phones, tablets and other devices may want to try and

develop their own Android applications. They may use a Windows PC, a concise guide

to setting up an IDE for that platform is useful.



Solution

The use of the Eclipse IDE is recommended when developing Android Apps. Configuring Eclipse on Windows is not a single shot install, several stages need to be completed. A single concise guide to setting up the IDE on Windows is helpful, particularly

for those with limited software development experience.



24 | Chapter 1: Getting Started



www.it-ebooks.info



Figure 1-17.



Discussion

To develop applications for Android it is recommended that Eclipse is used. Eclipse

provides an Integrated Development Environment (IDE) for Java. An Android Development Tools plug-in is available to enhance Eclipse. The ADT plug-in uses the Android Software Development Kit which provides essential tools to develop Android

software. To set-up a development system you will need to download and install:

1.

2.

3.

4.



Java Standard Edition Development Kit

Eclipse for Java Development

Android Software Development Kit

Android Development Tools Plug-in (from within Eclipse)



These stages in more detail for a PC running Windows (tested on 32 bit XP and Vista):



1. Install JDK (Java Development Kit)

Go to the Java download page at:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

Select the 'Java' icon to access the JDK downloads:

The list of JDK downloads will be shown. Click the 'Accept License Agreement' radio

button, otherwise you cannot download the JDK.

Download and run the file jdk-7-windows-i586.exe (or jdk-7-windows-x64.exe for 64

bit Windows). You may need to select the location of the download site. Accept any

security warnings that appear but only if you are downloading from the official Java

download web page.

When the download has completed and is run you will need to go through the install

screens clicking Next until the JDK installer has finished. You should not need to change

any options presented. When the JDK installer has completed click the Finish button.

A product registration web page may load, this can be closed or you can choose to

register your installation.

1.6 Set Up an IDE on Windows to Develop for Android | 25



www.it-ebooks.info



Figure 1-18.



2. Install Eclipse for Java Development

Got to the Eclipse Downloads web page at:

http://www.eclipse.org/downloads/

Windows needs to be selected in the Packages dropdown, select the relevant Eclipse

IDE for Java Developers download link.

Download and open the zip file. In the file there will be an eclipse directory containing

several files and sub-directories. Copy the eclipse directory and all its contents as it

comes. The usual place to copy the files to is either the root of the C drive or under C:

\Program Files, you may need to select continue when Windows asks permission for

the copy.



26 | Chapter 1: Getting Started



www.it-ebooks.info



Figure 1-19.



Figure 1-20.



Make a shortcut to eclipse.exe.

Run Eclipse so that it sets up a workspace and to check that both Java and Eclipse

installed correctly. When running Eclipse a security warning may be diplayed, select

Run to continue. Accept the default workspace location or use a different directory.



3. Install Android SDK (Software Development Kit)

Go to the Android Software Development Kit download page at:

http://developer.android.com/sdk/index.html

Choose the Windows EXE package (installer_r12-windows.exe) and select Run. Accept

the security warning only if you are downloading from the official Android SDK web

site. The Android SDK Tools installer will show some screens, select the Next button

1.6 Set Up an IDE on Windows to Develop for Android | 27



www.it-ebooks.info



Figure 1-21.



on each screen, you should not need to change any options. You may see a Java SE

Development Kit (JDK) not found screen. Although the JDK has been installed this is a



28 | Chapter 1: Getting Started



www.it-ebooks.info



Figure 1-22.



bug in the Android SDK installer. The work around is to select the Back button and

then the Next button again.

When the Install button is pressed a progress screen will briefly display while the Android files are copied, usually to C:\Program Files\Android\android-sdk unless the install

location was changed. Some users have experienced minor issues with the SDK installed

under C:\Program Files because of the space in the directory name. There is a work

around described below, alternatively install to C:\Android\android-sdk. Click the final

Next button and the Finish button at the end of the installation. If you left the Start

SDK Manager checkbox ticked then the SDK Manager will run. Otherwise select SDK

Manager from the Android SDK Tools program group (Start->All Programs->Android

SDK Tools->SDK Manager).

When the SDK Manager runs a progress dialog will be shown while the Android packages available to download are checked. Then a list of all available packages are shown

with many pre-selected for download. You should not need to change the initial selection. Click Install and the selected packages will download and be configured for use.

This may take a few minutes.

You may see a message box titled ADB Restart, if this is still the first run of SDK Manager

you can select No. Select Close on the completed progress screen. Close SDK Manager

by clicking the X button in the top corner of the window.



4. Android Development Tools (ADT) Plug-in

Installing the ADT Plug-in is done via Eclipse. To install the ADT Plug-in Eclipse must

be run from the Administrator accout. Use the shortcut created earlier or eclipse.exe

from the eclipse folder. In either case bring up the context menu (usually right-click)

and select Run as administrator, accept any security warnings. When Eclipse has loaded

open the Help menu item and select Install New Software....

1.6 Set Up an IDE on Windows to Develop for Android | 29



www.it-ebooks.info



Figure 1-23.



On the Install screen enter the following address into the Work with box:

https://dl-ssl.google.com/android/eclipse/

Click the Add button. An Add Repository screen appears, in the Name box type something meaningful, such as ADT plug-in (the above address will be displayed in the

Location box below).

Click the OK button. The screen will update after briefly showing Pending in the

Name column of the table.

Check the box next to Developer Tools. Then select the Next button at the bottom of

the screen.



30 | Chapter 1: Getting Started



www.it-ebooks.info



Figure 1-24.



A list of the items to be installed will be displayed. Select Next again.

A screen displays the licenses, ensure that each license has been accepted (select I accept

the terms of the license agreements radio button). Then click the Finish button. A security

warning will need to be accepted to complete the installation, select OK to this warning

(the address entered above is a secure address).

Eclipse will ask you for a restart. Select the Restart Now button and Eclipse will close

and reload.



1.6 Set Up an IDE on Windows to Develop for Android | 31



www.it-ebooks.info



Use the Window menu option in Eclipse and select Preferences. On the Preferences

dialog select Android. A Google Android SDK usage monitoring question may appear.

If you are happy with usage monitoring select the Proceed button, otherwise remove

the tick on the check box and then select Proceed.

In the SDK Location box enter the location selected in step three (or use the Browse

button to choose). If that location is C:\Program Files\Android\android-sdk use the

Windows 8.3 format for the Program Files directory, i.e. set it to C:\PROGRA~1\Android\android-sdk. Select the Apply button and the list below the SDK Location will

update. Select OK to close the screen.

Eclipse is now configured to build and debug Android Apps. Use the recipe Recipe 1.5 to configure an Andriod Emulator; then try the Recipe 1.4 recipe as a sanity

check.



See Also

Recipe 1.5

Recipe 1.4



1.7 Android Lifecycle

Ian Darwin



Problem

Android apps do not have a "main" method; you need to learn how they get started

and how they stop or get stopped.



Solution

The class android.Activity provides a number of well-defined life-cycle methods that

are called when an application is started, suspended, restarted, etc., as well as a method

you can call to mark an Activity as finished.



Discussion

Your Android application runs in its own Unix process, so in general it cannot directly

affect any other running application. The Dalvik VM interfaces with the operating system to call you when your application starts, when the user switches to another application, and so on. There is a well-defined lifecycle for Android applications.

An Android application has three states it can be in:

1. Active - the app is visible to the user and is running;

2. Paused - the app is partly obscured and has lost the input focus.

3. Stopped - the app is completely hidden from view

32 | Chapter 1: Getting Started



www.it-ebooks.info



Your app will be transitioned among these states by Android calling the following

methods on the current Activity at the appropriate time:

Example 1-4.

void

void

void

void

void

void

void



onCreate(Bundle savedInstanceState)

onStart()

onResume()

onRestart()

onPause()

onStop()

onDestroy()



For an application's first Activity, onCreate() is how you know that the application has

been started. This is where you normally do constructor-like work such as setting up

the "main window" with setContentView(), add listeners to buttons to do work (including starting additional Activities), and so on. This is the one method that even the

simplest Android app needs.

You can see the effects of the various lifecycle methods by creating a dummy project

in Eclipse and overriding all the methods with log "debug" statements.



1.8 Opening a Web Page, Phone Number or anything else with

an Intent

Ian Darwin



Problem

The Intent mechanism is fundamental to Android; it allows one application to have

some entity processed by another application without knowing or caring what that

application is.



Solution

Invoke the Intent constructor; invoke startActivity on the constructed Intent.



Discussion

The Intent constructor takes two arguments, the action to take and the entity to act

on. Think of the first as the verb and the second as the object of the verb. The most

common action is Intent.ACTION_VIEW, for which the String representation is

android.intent.action.VIEW. The second will typically be a URL or as Android likes it

less precisely (more generally) a URI. URIs can be created using the static parse() method in the URI class. Assuming that the String variable data contains the location we

want to view, the code to create an Intent for it might be something like the following:



1.8 Opening a Web Page, Phone Number or anything else with an Intent | 33



www.it-ebooks.info



Figure 1-25.

Example 1-5.

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));



That's all! The beauty of Android is shown here - we don't know or care if data contains

a web page URL with http:, a phone number with tel:, or even something we've never

seen. As long as there is an application registered to process this type of intent, Android

will find it for us, after we invoke it. How do we invoke the Intent? Remember that

34 | Chapter 1: Getting Started



www.it-ebooks.info



Xem Thêm
Tải bản đầy đủ (.pdf) (688 trang)

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×