转载请注明出处:http://blog.csdn.net/dawanganban/article/details/9823623

Running Your App

PREVIOUSNEXT

THIS LESSONTEACHES YOU TO

1. Run on a Real Device

2. Run on the Emulator

YOU SHOULD ALSOREAD

·Using Hardware Devices

·Managing Virtual Devices

·Managing Projects

If you followed theprevious lessonto create an Android project, it includesa default set of "Hello World" source files that allow you toimmediately run the app.

How you run your app depends on twothings: whether you have a real Android-powered device and whether you're usingEclipse. This lesson shows you how to install and run your app on a real deviceand on the Android emulator, and in both cases with either Eclipse or thecommand line tools.

Before you run your app, you should beaware of a few directories and files in the Android project:

AndroidManifest.xml

Themanifest filedescribes the fundamentalcharacteristics of the app and defines each of its components. You'll learnabout various declarations in this file as you read more training classes.

One of the most important elements yourmanifest should include is the<uses-sdk>element. This declares your app'scompatibility with different Android versions using theandroid:minSdkVersionandandroid:targetSdkVersionattributes. For your first app, itshould look like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
<uses-sdk android:minSdkVersion="8"android:targetSdkVersion="17"/>
...
</manifest>

You should always set theandroid:targetSdkVersionas high as possible and test your app onthe corresponding platform version. For more information, readSupporting Different PlatformVersions.

src/

Directory for your app's main sourcefiles. By default, it includes anActivityclass that runs when your app islaunched using the app icon.

res/

Contains several sub-directories forapp resources. Hereare just a few:

drawable-hdpi/

Directory for drawable objects (such asbitmaps) that are designed for high-density (hdpi) screens. Other drawabledirectories contain assets designed for other screen densities.

layout/

Directory for files that define yourapp's user interface.

values/

Directory for other various XML filesthat contain a collection of resources, such as string and color definitions.

When you build and run the defaultAndroid app, the defaultActivityclass starts and loads a layout filethat says "Hello World." The result is nothing exciting, but it'simportant that you understand how to run your app before you start developing.

Run on a RealDevice

If you have a real Android-powereddevice, here's how you can install and run your app:

1. Plug in your device to your developmentmachine with a USB cable. If you're developing on Windows, you might need toinstall the appropriate USB driver for your device. For help installingdrivers, see theOEM USB Driversdocument.

2. EnableUSBdebuggingon your device.

oOn most devices running Android 3.2 or older, you canfind the option underSettings > Applications > Development.

oOn Android 4.0 and newer, it's inSettings> Developer options.

Note:On Android 4.2 and newer,Developeroptionsis hidden by default. To make it available, go toSettings >About phoneand tapBuild numberseven times. Return to the previousscreen to findDeveloper options.

To run the app from Eclipse:

1. Open one of your project's files andclickRunfrom the toolbar.

2. In theRun aswindow that appears, selectAndroidApplicationand clickOK.

Eclipse installs the app on yourconnected device and starts it.

Or to run your app from a command line:

1. Change directories to the root of yourAndroid project and execute:

ant debug

2. Make sure the Android SDKplatform-tools/directory is included in yourPATHenvironment variable, then execute:

adb install bin/MyFirstApp-debug.apk

3. On your device, locateMyFirstActivityand open it.

That's how you build and run yourAndroid app on a device! To start developing, continue to thenext lesson.

Run on theEmulator

Whether you're using Eclipse or thecommand line, to run your app on the emulator you need to first create anAndroid Virtual Device(AVD). An AVD is a device configurationfor the Android emulator that allows you to model different devices.


Figure 1.The AVD Manager showing a few virtualdevices.

To create an AVD:

1. Launch the Android Virtual DeviceManager:

a. In Eclipse, click Android Virtual DeviceManagerfrom the toolbar.

b. From the command line, changedirectories to<sdk>/tools/and execute:

android avd

2. In theAndroidVirtual Device Managerpanel, clickNew.

3. Fill in the details for the AVD. Give ita name, a platform target, an SD card size, and a skin (HVGA is default).

4. ClickCreateAVD.

5. Select the new AVD from theAndroidVirtual Device Managerand clickStart.

6. After the emulator boots up, unlock theemulator screen.

To run the app from Eclipse:

1. Open one of your project's files andclickRunfrom the toolbar.

2. In theRun aswindow that appears, selectAndroidApplicationand clickOK.

Eclipse installs the app on your AVD andstarts it.

Or to run your app from the commandline:

1. Change directories to the root of yourAndroid project and execute:

ant debug

2. Make sure the Android SDKplatform-tools/directory is included in yourPATHenvironment variable, then execute:

adb install bin/MyFirstApp-debug.apk

3. On the emulator, locateMyFirstActivityand open it.

That's how you build and run yourAndroid app on the emulator! To start developing, continue to thenext lesson.

运行你的应用程序

这节课教你

1. 在真实设备上运行

2. 在模拟器上运行

你也应该阅读

·使用硬件设备

·管理虚拟设备

·管理项目

如果你是按照上一课创建一个Android项目,该项目包括设一个默认的“Hello World” 的源文件,我们可以立即运行。

你如何运行你的应用程序依赖于两件事情:你是否有一个真正的Andr​​oid设备和Eclipse。这一课将告诉您使用Eclipse或命令行工具Android项目在真实的设备和Android模拟器中如何安装和运行。

在你运行你的应用程序之前,注意在Android项目的几个目录和文件:

AndroidManifest.xml

清单文件描述了应用程序的基本特征,并定义每个组件。在后续的课程中会说明这个文件中的各种声明。

清单文件中最重要的元素之一是<uses-sdk>元素。使用android:minSdkVersionandroid:targetSdkVersion属性声明您的应用程序对不同的Android​​本的兼容性。对于你的第一个应用程序,声明文件如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >  <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />  ...</manifest>

你应该设置您的应用程序在相应的平台版本尽可能高的android:targetSdkVersion欲了解更多信息,请阅读支持不同的平台版本

src/

您的应用程序的主源文件目录。默认情况下,它包含了一个Activity类通过应用程序图标启动你的程序运行。

res/

包含几个应用程序资源的子目录列举其中的几个子目录:

drawable-hdpi/

绘制资源(如位图),是专为高分辨率(hdpi)屏幕。其他的绘制资源目录存放其他屏幕分辨率资源。

layout/

定义你的应用程序的用户界面的目录文件。

values/

其他各种XML文件,其中包含资源文件,如字符串和颜色定义文件。

当你建立并运行Android默认应用程序时,默认的Activity类启动并加载布局文件中的“Hello World”其结果并不吃惊,但你要了解如何运行你的应用程序,然后开始开发。

在真正的设备上运行

如果你有一个真正的Andr​​oid设备,这里将说明如何在该设备中安装和运行您的应用程序:

1. 使用USB线将设备插入到你的开发机器。如果在Windows系统上开发,你可能需要为您的设备安装相应的USB驱动程序。安装驱动程序的帮助,请参阅OEMUSB驱动程序文件。

2. 您的设备上启用USB调试

o在大多数设备上运行Android 3.2或以上,你可以找到下的选项设置>应用程序>开发

oAndroid 4.0和更高版本,它是在设置”>“开发人员选项

注意:Android 4.2和更高版本中,开发人员选项默认是隐藏的。为了使其可用,进入设置>关于手机和点击Build number七次。返回到前一个画面,找到开发人员选项

Eclipse中运行的应用程序:

1. 打开一个项目的文件,单击运行从工具栏。

2. 在出现的窗口中运行,选择Android的应用程序,并单击确定

Eclipse的安装连接的设备上的应用程序,并启动它。

或运行您的应用程序的命令行:

1. 更改你的Andr​​oid项目的根目录,然后执行:

ant debug

2. 请确保Android SDKplatform-tools/目录已经包含在你的PATH环境变量,然后执行:

 adb install bin/MyFirstApp-debug.apk

3. 在设备上,找到MyFirstActivity并打开它。

这就是你如何在设备上构建并运行Android应用程序!继续下一课

在模拟器上运行

无论是在Eclipse或命令行中,在模拟器上运行你的应用,你需要首先创建一个Android虚拟设备AVD)。一个AVD是一个为Android模拟器,它可以让你模拟不同设备配置的设备。


1示出了几个虚拟设备AVD管理。

要创建一个AVD

1. 运行Android虚拟设备管理:

a. Eclipse中,从工具栏单击“Android虚拟设备管理

b. 在命令行,更改目录到<sdk>/tools/执行:

 android avd 

2. Android虚拟设备管理器面板中,单击新建

3. 填写AVD的细节。给它一个名字,选择一个版本平台,设置SD卡大小和皮肤(HVGA是默认值)。

4. 单击CreateAVD

5. Android虚拟设备管理器单击开始选择新的AVD

6. 模拟器启动后将模拟器的屏幕解锁。

Eclipse中运行的应用程序:

1. 打开一个项目的文件,单击运行从工具栏。

2. 在出现的窗口中运行,选择Android的应用程序,并单击确定

EclipseAVD上安装的应用程序,并启动它。

或者运行你的应用程序的命令行:

1. 更改你的Andr​​oid项目的根目录,然后执行:

ant debug

2. 请确保Android SDKplatform-tools/目录已经包含在你的PATH环境变量,然后执行:

adb install bin/MyFirstApp-debug.apk 

3. 解锁模拟器,找到MyFirstActivity“并打开它。

这是在模拟器如何建立和运行Andr​​oid应用程序!继续下一课

更多相关文章

  1. Android中获取应用程序(包)的信息-----PackageManager的使用(一)
  2. Socket编程java[简单易懂],附带Android(安卓)Studio 项目聊天实
  3. android 应用选择器的使用
  4. Android(安卓)P正式发布,你需要尽快做适配了
  5. android 应用选择器的使用
  6. 获取Android应用程序的签名
  7. Android(安卓)获取设备宽高分辨率
  8. Android在外部修改了工程名文件夹名称,报错
  9. Android(安卓)运行时权限

随机推荐

  1. 学习
  2. linux切换php版本--centos7配置
  3. 入职以来,我和老大之间的故事。
  4. 请不要拒绝成熟!
  5. C语言函数以及函数的使用
  6. android 界面中加入密码框
  7. android Theme启动APP闪屏处理
  8. Android给TextView添加点击事件的实现方
  9. android跑马灯有时候不跑
  10. android 的常标签和方法 android 初学者