`
从此醉
  • 浏览: 1044722 次
  • 性别: Icon_minigender_1
  • 来自: US
社区版块
存档分类
最新评论

Android官方教程翻译(2)——运行第一个程序

 
阅读更多

转载请注明出处: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应用程序!继续下一课

分享到:
评论

相关推荐

    老罗android开发视频教程全集百度网盘下载

    《老罗Android视频教程(第一季)》的视频一共分为15个章节, 涵盖了Android入门、常用UI布局介绍、HTTP编程、 XML与Json数据解析、JDBC与Web编程、常用UI控件、 数据储存与文件操作、对话框、通知、菜单、 ...

    Android程序设计基础

    第1章就将指导你完成第一个Android程序。第2章回过头来介绍Android的基本概念和原理。第3章探讨用户界面,也就是大多数Android程序中最重要的部分。  致谢  我要感谢为本书成功出版做出贡献的许多人,包括审稿人...

    新版Android开发教程.rar

    Android 是一个专门针对移动设备的软件集,它包括一个操作系统,中间件和一些重要的应用程序。 Beta 版 的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 ...

    android实战 查看短信的猫(android studio源程序工程)

    包括Android 移动开发基础案例教程课本中第八章实战演练——查看短信的猫的源代码,可用Android studio运行,适合初学者学习。

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    2.2 建立第一个Android项目(HelloAndroid!) 2.3 Android应用程序架构——从此开始 2.4 可视化的界面开发工具 2.5 部署应用程序到Android手机 第3章 用户人机界面 3.1 更改与显示文字标签——TextView标签的使用 ...

    android开发入门教程

    11.2.1 界面设计始终是第一位——实现UI 11.2.2 必备的动力源泉——数据操作和存储 11.2.3 华丽转身——实现多页面跳转 11.2.4 始终为用户做好服务——增加Service 11.2.5 细节决定成败——完善应用细节 11.3 成功就...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    2.2 建立第一个Android项目(HelloAndroid!) 2.3 Android应用程序架构——从此开始 2.4 可视化的界面开发工具 2.5 部署应用程序到Android手机 第3章 用户人机界面 3.1 更改与显示文字标签——TextView标签的使用 ...

    Android编程入门很简单.(清华出版.王勇).part1

    第3章创建第一个程序——helloworld 3.1新建第一个程序 3.1.1新建工程 3.1.2运行程序 3.2认识HelloWodd 3.2.1 首识Android工程 3.2.2认识布局文件 3.2.3认识值文件 3.2.4认识R文件 3.2.5认识注册文件 3.3调试程序 ...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    2.2 建立第一个Android项目(HelloAndroid!) 2.3 Android应用程序架构——从此开始 2.4 可视化的界面开发工具 2.5 部署应用程序到Android手机 第3章 用户人机界面 3.1 更改与显示文字标签——TextView标签的使用 ...

    android开发入门与实战(下)

    第1章 掀起你的盖头来——初识Android 1.1 认识Android 1.2 Android的背景 1.2.1 Android的历史 1.2.2 Android的发展 1.3 我的Android我做主 1.3.1 开发基于Android平台的应用 1.3.2 参加Android开发者大赛 1.3.3 ...

    Google Android SDK开发范例大全的目录

    2.2 建立第一个Android项目(HelloAndroid!) 2.3 Android应用程序架构——从此开始 2.4 可视化的界面开发工具 2.5 部署应用程序到Android手机 第3章 用户人机界面 3.1 更改与显示文字标签——TextView标签的使用 ...

    《Google Android开发入门与实战》.pdf

    第1章 掀起你的盖头来——初识android. 1 1.1 认识android 1 1.2 android的背景 2 1.2.1 android的历史 2 1.2.2 android的发展 2 1.3 我的android我做主 2 1.3.1 开发基于android平台的应用 3 ...

    android开发资料大全

    Android数据库最基础的一个例子(本人已测试,可以运行) 为launcher添加一个仿Mac的dock(附源码) 使用Gallery实现Tab 仿QQ--tab切换动画实例 Android 小项目之---猜扑克牌游戏 (附源码) fleep滑动切换tab(切换...

    Android编程入门很简单.(清华出版.王勇).part2

    第3章创建第一个程序——helloworld 3.1新建第一个程序 3.1.1新建工程 3.1.2运行程序 3.2认识HelloWodd 3.2.1 首识Android工程 3.2.2认识布局文件 3.2.3认识值文件 3.2.4认识R文件 3.2.5认识注册文件 3.3调试程序 ...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    2.2 建立第一个Android项目(HelloAndroid!) 2.3 Android应用程序架构——从此开始 2.4 可视化的界面开发工具 2.5 部署应用程序到Android手机 第3章 用户人机界面 3.1 更改与显示文字标签——TextView标签的...

    android实战 开发HelloWorld程序(android studio源程序工程)

    包括Android 移动开发基础案例教程课本中第一章实战演练——开发HelloWorld程序的源代码,可用Android studio运行,适合初学者学习。

    android开发入门与实战(上)

    第1章 掀起你的盖头来——初识Android 1.1 认识Android 1.2 Android的背景 1.2.1 Android的历史 1.2.2 Android的发展 1.3 我的Android我做主 1.3.1 开发基于Android平台的应用 1.3.2 参加Android开发者大赛 1.3.3 ...

Global site tag (gtag.js) - Google Analytics