Android官方教程翻译(2)——运行第一个程序
转载请注明出处:http://blog.csdn.net/dawanganban/article/details/9823623
Running Your App
THIS LESSONTEACHES YOU TO
YOU SHOULD ALSOREAD
If you followed the previous
lesson to 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
The manifest
file describes 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 the android:minSdkVersion andandroid:targetSdkVersion attributes.
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 the android:targetSdkVersion as
high as possible and test your app onthe corresponding platform version. For more information, read Supporting
Different PlatformVersions.
src/
Directory for your app's main sourcefiles. By default, it includes an Activity class
that runs when your app islaunched using the app icon.
res/
Contains several sub-directories for app
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 default Activity class
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 the OEM USB Drivers document.
2. Enable USBdebugging on your device.
o On most devices running Android 3.2 or older, you canfind the option under Settings > Applications > Development.
o On Android 4.0 and newer, it's in Settings> Developer options.
Note: On Android 4.2 and newer, Developeroptions is
hidden by default. To make it available, go toSettings >About phone and tap Build number seven
times. Return to the previousscreen to findDeveloper options.
To run the app from Eclipse:
1. Open one of your project's files andclick Run from
the toolbar.
2. In the Run as window that appears, select AndroidApplication and
click OK.
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 SDK platform-tools/ directory
is included in your PATH environment variable, then execute:
adb install bin/MyFirstApp-debug.apk
3. On your device, locate MyFirstActivity and open it.
That's how you build and run yourAndroid app on a device! To start developing, continue to the next
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 DeviceManager from the toolbar.
b. From the command line, changedirectories to<sdk>/tools/ and execute:
android avd
2. In the AndroidVirtual Device Manager panel, click New.
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. Click CreateAVD.
5. Select the new AVD from the AndroidVirtual Device Manager and click Start.
6. After the emulator boots up, unlock theemulator screen.
To run the app from Eclipse:
1. Open one of your project's files andclick Run from
the toolbar.
2. In the Run as window that appears, select AndroidApplication and
click OK.
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 SDK platform-tools/ directory
is included in your PATH environment variable, then execute:
adb install bin/MyFirstApp-debug.apk
3. On the emulator, locate MyFirstActivity and open it.
That's how you build and run yourAndroid app on the emulator! To start developing, continue to the next
lesson.
运行你的应用程序
这节课教你
1. 在真实设备上运行
2. 在模拟器上运行
你也应该阅读
· 使用硬件设备
· 管理虚拟设备
· 管理项目
如果你是按照上一课创建一个Android项目,该项目包括设一个默认的“Hello
World” 的源文件,我们可以立即运行。
你如何运行你的应用程序依赖于两件事情:你是否有一个真正的Android设备和Eclipse。这一课将告诉您使用Eclipse或命令行工具Android项目在真实的设备和Android模拟器中如何安装和运行。
在你运行你的应用程序之前,注意在Android项目的几个目录和文件:
AndroidManifest.xml
清单文件描述了应用程序的基本特征,并定义每个组件。在后续的课程中会说明这个文件中的各种声明。
清单文件中最重要的元素之一是<uses-sdk>元素。 使用android:minSdkVersion和android: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”。 其结果并不吃惊,但你要了解如何运行你的应用程序,然后开始开发。
在真正的设备上运行
如果你有一个真正的Android设备,这里将说明如何在该设备中安装和运行您的应用程序:
1. 使用USB线将设备插入到你的开发机器。 如果在Windows系统上开发,你可能需要为您的设备安装相应的USB驱动程序。 安装驱动程序的帮助,请参阅OEM的USB驱动程序文件。
2. 您的设备上启用USB调试 。
o 在大多数设备上运行Android 3.2或以上,你可以找到下的选项设置>应用程序>开发 。
o 在Android 4.0和更高版本,它是在“设置”>“开发人员选项 。
注意:在Android 4.2和更高版本中, 开发人员选项默认是隐藏的。 为了使其可用,进入设置>关于手机和点击 Build
number 七次。 返回到前一个画面,找到开发人员选项 。
从Eclipse中运行的应用程序:
1. 打开一个项目的文件,单击“ 运行 ” 从工具栏。
2. 在出现的窗口中运行 ,选择Android的应用程序,并单击“确定”。
Eclipse的安装连接的设备上的应用程序,并启动它。
或运行您的应用程序的命令行:
1. 更改你的Android项目的根目录,然后执行:
ant debug
2. 请确保Android SDK的platform-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. 单击 Create AVD。
5. 从Android虚拟设备管理器“,单击” 开始 “选择新的AVD。
6. 模拟器启动后将模拟器的屏幕解锁。
从Eclipse中运行的应用程序:
1. 打开一个项目的文件,单击“ 运行 ” 从工具栏。
2. 在出现的窗口中运行 ,选择Android的应用程序,并单击“确定”。
Eclipse的AVD上安装的应用程序,并启动它。
或者运行你的应用程序的命令行:
1. 更改你的Android项目的根目录,然后执行:
ant debug
2. 请确保Android SDK的platform-tools/目录已经包含在你的PATH环境变量,然后执行:
adb install bin/MyFirstApp-debug.apk
3. 解锁模拟器,找到MyFirstActivity“并打开它。
这是在模拟器如何建立和运行Android应用程序!继续下一课 。
Android官方教程翻译(2)——运行第一个程序的更多相关文章
- Android官方教程翻译(4)——启动另一个Activity
Starting Another Activity 启动另一个Activity PREVIOUSNEXT THIS LESSON TEACHES YOU TO 这节课教你 1. Respond t ...
- Android官方教程翻译(3)——创建一个简单的用户界面
转载请注明出处:http://blog.csdn.net/dawanganban/article/details/9839523 Building a Simple User Interface 创建 ...
- Android官方教程翻译(1)——创建第一个Android应用
转载请注明出处:http://blog.csdn.net/dawanganban/article/details/9822431 Building Your First App GETSTARTED ...
- Android官方教程翻译(6)——添加ActionBar
The action bar allows you to add buttons for the most important action items relating to the app's c ...
- Android官方教程翻译(5)——设置ActionBar
Setting Up the Action Bar 设置Action Bar PREVIOUSNEXT THIS LESSONTEACHES YOU TO 这节课教你 1. Support An ...
- Java入门——编写并运行第一个程序
Java入门——编写并运行第一个程序 摘要:本文主要介绍如何使用Java语言编写并通过DOS运行简单的程序. 编写简单的程序 在D盘新建一个文本文档,输入如下代码: class Hello { pub ...
- 运行第一个程序!hello world!
第一个程序:1 //打印 hello world 2 3 #include <stdio.h> 4 int main() 5 { 6 printf("hello world!\n ...
- Android初级教程理论知识(第一章快速入门)
一.综合介绍. Android项目的目录结构 Activity:应用被打开时显示的界面 src:项目代码 R.java:项目中所有资源文件的资源id Android.jar:Android的jar包, ...
- c# MongoDB Driver 官方教程翻译
先贴官方文档地址:http://mongodb.github.io/mongo-csharp-driver/2.5/getting_started/quick_tour/ 安装部分很简单,nuget搜 ...
随机推荐
- Eclipse "Could not create java virtual machine"的问题解决
今天到了新的环境,需要重新搭建Android的开发环境,下载eclipse并安装了JDK1.6后,启动eclipse,发现出现了错误“Could not create Javavirtual mach ...
- 移动端 h5 开发相关内容总结——JavaScript 篇
1.改变页面标题的内容 有时候我们开发 h5页面的时候须要动态的去更新title 的名字,这个时候使用 document.title='改动后的名字'; 就行解决我们的问题. 或者使用 //当前fir ...
- 浅浅的分析下es6箭头函数
原文链接:http://damobing.com/?p=589 前言 箭头函数作为es6重点的语法内容之一,很多开发者对其爱不释手,当也要注意其可能存在的问题,其正确的使用场景,否则会引起不必要的bu ...
- 【record】9.24..10.1
因为参加比赛所以做得比较少了
- php课程 5-19 php数据结构函数和常用函数有哪些
php课程 5-19 php数据结构函数和常用函数有哪些 一.总结 一句话总结: 1.php数据结构函数有哪些(四个)? • array_pop();从最后弹出一个值,返回弹出值• array_pus ...
- centos7安装nginx的两种方法
第一种方式:通过yum安装 直接通过 yum install nginx 肯定是不行的,因为yum没有nginx,所以首先把 nginx 的源加入 yum 中 运行下面的命令: 1.将nginx放到y ...
- [Compose] 14. Build curried functions
We see what it means to curry a function, then walk through several examples of curried functions an ...
- Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡...)
众所周知,想要让ImageView旋转的话,可以用setRotation()让其围绕中心点旋转,但这个旋转是不带动画的,也就是旋转屏幕时图片噌的一下就转过去了,看不到旋转的过程,此UI体验不大好,为此 ...
- 微信开发学习日记(五):weiphp开源框架的bug,公众号权限编辑问题
最近在研究weiphp,总体感觉还行,bug据说还挺多. 这不,我就遇到一个比较严重影响使用的.感觉不太应该出现这么严重的bug啊. weiphp的微信公众号等级,权限增加和编辑bug,看不到权限列表 ...
- IT忍者神龟之Hibernat持久化对象-数据表映射配置回想
1.持久化对象POJO编写规则: 1) 有空參public构造器: 2) 提供标识属性.映射数据表主键: 3) 属性提供setter和getter方法. 4) 属性使用基本数据类型的包装类型.基本类型 ...