Running Your App

If you followed the previous lesson to create an Android project, it includes a default set of “Hello World” source files that allow you to immediately run the app.

如果你跟着前面的课程创建了一个Android项目,这个项目包括一系列默认的“Hello World”资源文件,你就可以立即开始运行你的app了。

How you run your app depends on two things: whether you have a real Android-powered device and whether you’re using Eclipse. This lesson shows you how to install and run your app on a real device and on the Android emulator, and in both cases with either Eclipse or the command line tools.

要想运行你的app必须具备以下两个条件:你是否有一个使用Android操作系统的真机设备,你是否使用Eclipse。这节课将教给你如何在一个真机上或者一个Android模拟器上安装并且运行你的app,以下两种情况我们都会说到:在Eclipse上和在命令行工具里。

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

在你开始运行你的app以前,你应该了解在你的Android项目中有以下一些目录和文件:

AndroidManifest.xml

  • The manifest file describes the fundamental characteristics of the app and defines each of its components. You’ll learn about various declarations in this file as you read more training classes.

    清单文件记述了对于app的基本特性和定义了它的每个组分。如果你继续读后面的更多练习课程,你将在这个文件中学会各种各样的声明。

  • One of the most important elements your manifest should include is the element. This declares your app’s compatibility with different Android versions using the android:minSdkVersion and android:targetSdkVersion attributes. For your first app, it should look like this:

    你的清单文件中应该包含的最重要的元素之一就是元素。这个使用 android:minSdkVersion 和 android:targetSdkVersion属性来声明你的app对于不同Android版本的兼容性。对于你的第一个app,它应该看起来像以下这样:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />
    ...
    </manifest>
  • You should always set the android:targetSdkVersion as high as possible and test your app on the corresponding platform version. For more information, read Supporting Different Platform Versions.

    你应该总是设置android:targetSdkVersion这个属性尽可能的高,并且在相应的平台版本上测试你的app。想获得更多的信息,请阅读“Supporting Different Platform Versions”。

src/

  • Directory for your app’s main source files. By default, it includes an Activity class that runs when your app is launched using the app icon.

    这是你app的主要的资源文件。默认情况下,它包含一个Activity类,当你的app通过应用图标来运行时会用到它。

res/

  • Contains several sub-directories for app resources. Here are just a few:

    包含了你的app资源的几个子目录。以下列取了几个:

    • drawable-hdpi/

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

    drawable对象(就如位图对象一样)的目录,它是为高密度(hdpi)的屏幕设置的。其它的drawable目录包含的资源是位其它密度的屏幕设置的。

  • layout/

    Directory for files that define your app’s user interface.

    定义你的app的用户界面的文件目录。

  • values/

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

    其它的一些各种各样的XML文件目录,它包含许多资源,比如string(字符)和color(颜色)的定义。

When you build and run the default Android app, the default Activity class starts and loads a layout file that says “Hello World.” The result is nothing exciting, but it’s important that you understand how to run your app before you start developing.

当你创建好并且运行了默认的Android app后,默认的Activity类会开始运行并且加载一个显示“Hello World”的layout文件。这个结果可能没有什么令人兴奋的,但是在你开始开发之前,它对于你了解如何运行你的app是非常重要的。

Run on a Real Device

在一个真机上运行

If you have a real Android-powered device, here’s how you can install and run your app:

如果你有一个搭载Android系统的真机,这里将告诉你如何开始安装和运行你的app:

  1. Plug in your device to your development machine with a USB cable. If you’re developing on Windows, you might need to install the appropriate USB driver for your device. For help installing drivers, see the OEM USB Drivers document.

    用一个USB数据线把你的Android设备连接到你开发所用的机器上。如果你在Windows上进行开发,你可能需要安装适合你的Android设备的USB驱动。如果你对安装设备不太了解,请看OEM USB Drivers文档。

  2. Enable USB debugging on your device.

    在你的Android设备上打开USB调试模式

    • On most devices running Android 3.2 or older, you can find the option under Settings > Applications > Development.

    在大多数搭载Android 3.2或者更低版本的Android设备上,在 设置 > 应用程序 > 开发 中你可以找到此选项

  3. On Android 4.0 and newer, it’s in Settings > Developer options.

    在搭载Android 4.0或者更高版本的机器上,你可以在 设置 > 开发者选项中找到此选项。
  4. Note: On Android 4.2 and newer, Developer options is hidden by default. To make it available, go to Settings > About phone and tap Build number seven times. Return to the previous screen to find Developer options.

    注意:在Android 4.2或者更高的版本上,开发者选项默认是隐藏的。想让其显示,你可以按 设置 > 关于手机,在版本号选项上连续点击7次。返回前一页,你将可以找到开发者选项。

To run the app from Eclipse:

从Eclipse中运行你的app

  1. Open one of your project’s files and click Run from the toolbar.

    打开一个项目文件,在工具栏上点击 Run。

  2. In the Run as window that appears, select Android Application and click OK.

    在运行后出现的窗口中,选择你用来编译的Android设备,然后点击OK。

Eclipse installs the app on your connected device and starts it.

Eclipse将在你已经连接好的设备上安装app并且运行。

Or to run your app from a command line:

或者,你也可以从命令行来运行你的app:

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

    进入你的Android项目的根目录,执行:

    ant debug

  2. Make sure the Android SDK platform-tools/ directory is included in your PATH environment variable, then execute:

    确保你的PATH环境变量中包含 Android SDK platform-tools/ 目录,然后执行:

    adb install bin/MyFirstApp-debug.apk

  3. On your device, locate MyFirstActivity and open it.

    在你的设备上,找到 MyFirstActivity ,然后打开它。

That’s how you build and run your Android app on a device! To start developing, continue to the next lesson.

这就是如何创建并且在Android设备上运行你的Android app。要开始开发,继续下一节课程吧。

Run on the Emulator

在模拟器上运行

Whether you’re using Eclipse or the command line, to run your app on the emulator you need to first create an Android Virtual Device (AVD). An AVD is a device configuration for the Android emulator that allows you to model different devices.

无论你是使用Eclipse还是使用命令行,如果你想在模拟器上运行你的app你首先都需要创建一个Android模拟设备(AVD)。AVD是一个Android模拟器的设备配置,它可以让你去模拟不同的设备。

To create an AVD:

创建一个AVD

  1. Launch the Android Virtual Device Manager:

    运行Android 虚拟设备管理器

    • In Eclipse, click Android Virtual Device Manager from the toolbar.

      在Eclipse中,在工具栏里点击 Android Virtual Device Manager。

      *. From the command line, change directories to /tools/ and execute:

      在命令行里,改变目录到 /tools/,然后运行:

      android avd

  2. In the Android Virtual Device Manager panel, click New.

    在Android Virtual Device Manager面板中,点击New。

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

    填写AVD的详情。给它一个名字,一个目标平台,一个SD卡的大小和一个皮肤(默认是HVGA)。

  4. Click Create AVD.

    点击创建AVD。
  5. Select the new AVD from the Android Virtual Device Manager and click Start.

    从Android Virtual Manager中选择一个新的AVD设备,然后点击Start。
  6. After the emulator boots up, unlock the emulator screen.

    模拟器启动以后,把模拟器的屏幕解锁。

To run the app from Eclipse:

从Eclipse中运行你的app

  1. Open one of your project’s files and click Run from the toolbar.

    打开一个项目文件,在工具栏上点击 Run。

  2. In the Run as window that appears, select Android Application and click OK.

    在运行后出现的窗口中,选择你用来编译的Android设备,然后点击OK。

Eclipse installs the app on your connected device and starts it.

Eclipse将在你已经连接好的设备上安装app并且运行。

Or to run your app from a command line:

或者,你也可以从命令行来运行你的app:

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

    进入你的Android项目的根目录,执行:

    ant debug

  2. Make sure the Android SDK platform-tools/ directory is included in your PATH environment variable, then execute:

    确保你的PATH环境变量中包含 Android SDK platform-tools/ 目录,然后执行:

    adb install bin/MyFirstApp-debug.apk

  3. On your device, locate MyFirstActivity and open it.

    在你的设备上,找到 MyFirstActivity ,然后打开它。

That’s how you build and run your Android app on a device! To start developing, continue to the next lesson.

这就是如何创建并且在Android设备上运行你的Android app。要开始开发,继续下一节课程吧。

这是我自己翻译的,如果您发现其中有重要翻译错误,敬请指出。万分感谢!

Android官方文档翻译 四 1.2Running Your App的更多相关文章

  1. android官方文档翻译(不断更新中。。。)

    最近在自学android,抽空把官方文档的guide跟training差不多看了一遍,又对比了一些书籍,感觉还是官方文档讲得比较好,所以自己计划把官方文档翻译一下,方便自己的知识巩固以及复习查找,由于 ...

  2. Android官方文档翻译 十五 3.3Supporting Different Platform Versions

    Supporting Different Platform Versions 支持不同的平台版本 This lesson teaches you to 这节课教给你 Specify Minimum a ...

  3. Android官方文档翻译 三 1.1Creating an Android Project

    Creating an Android Project 创建一个Android项目 An Android project contains all the files that comprise th ...

  4. Android官方文档翻译 八 2.1Setting Up the Action Bar

    Setting Up the Action Bar 建立Action Bar This lesson teaches you to 这节课教给你 Support Android 3.0 and Abo ...

  5. Android官方文档翻译 十四 3.2Supporting Different Screens

    Supporting Different Screens 支持不同的屏幕 This lesson teaches you to 这节课教给你 Create Different Layouts 创建不同 ...

  6. Android官方文档翻译 二 1.Building Your First App

    Building Your First App 创建你的第一个App项目 Dependencies and prerequisites 依赖关系和先决条件 * Android SDK * ADT Pl ...

  7. Android官方文档翻译 九 2.2Adding Action Buttons

    Adding Action Buttons 增加动作按钮 This lesson teaches you to 这节课教给你 Specify the Actions in XML 在XML中指定动作 ...

  8. Android官方文档翻译 十七 4.1Starting an Activity

    Starting an Activity 开启一个Activity This lesson teaches you to 这节课教给你 Understand the Lifecycle Callbac ...

  9. Android官方文档翻译 十六 4.Managing the Activity Lifecycle

    Managing the Activity Lifecycle 管理activity的生命周期 Dependencies and prerequisites 依赖关系和先决条件 How to crea ...

随机推荐

  1. windows下python3.7安装gmpy2、Crypto 库及rsa

    基于python3.7在windows下安装gmpy2 先检查一下是否安装了wheel文件包,在cmd中输入wheel,查看一下,如果没有安装,则输入安装:pip install wheel 如果遇到 ...

  2. [BUUCTF]PWN15——[BJDCTF 2nd]one_gadget

    [BUUCTF]PWN15--[BJDCTF 2nd]one_gadget 附件 步骤: 例行检查,64位,保护全开 nc试运行一下程序,看看情况,它一开始给了我们一个地址,然后让我们输入one ga ...

  3. 在【自定义列】中编辑简单运算公式(Power Query 之 M 语言)

    数据源: "品名"."数量"."单价"三列 目标: 计算销售单价(单价*1.2) 解决方案: 在[自定义列]中使用乘法四则运算 步骤: 打开 ...

  4. C#面对抽象编程第一讲

    闲话不多说,面向对象编程是高级语言的一个特点,但是把它概括成面向抽象更容易直击灵魂,经过了菜鸟大家都要面对的是不要写这么菜的代码了. 上例子,这应该是大家都很熟悉耳熟能详的代码, so easy. 1 ...

  5. generating project in interactive mode

    解决方案:加个参数 -DarchetypeCatalog=internal 让它不要从远程服务器上取catalog

  6. JAVA获取文件byte数组并输出进行展示和文件下载

    /** * 文件下载 */ @GetMapping(value = "/download") public void download(HttpServletResponse re ...

  7. SpringBoot整合mybatis-plus并实现代码自动生成

    1.引入maven <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...

  8. c++使用map保存成员函数地址

    note 本基于c++11介绍一种使用map保存成员函数地址 可避免使用 if 和 switch 配置灵活 方便, 代码维护效率高 结果: 范例开始 头文件包含 #include <iostre ...

  9. 【九度OJ】题目1069:查找学生信息 解题报告

    [九度OJ]题目1069:查找学生信息 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1069 题目描述: 输入 ...

  10. 【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...