Android开发入门第一个程序

2011年05月22日 Android 暂无评论

开始学习Android开发。之前已经做过开发环境的搭建。
Android开发环境搭建(Linux篇)
Android开发环境搭建(Windows篇)

有了开发环境就可以开始写代码了。本文是对Android开发做一个感性认识,了解一下Android的项目结构是怎样的。如果有JAVA基础的人,我想学起android会比较轻松。开发工具用的是eclipse,对java开发的人再为熟悉不过了。

新建第一个android程序。由于目前Android2.1的使用率是最高的,我这里就以2.1作为模拟器。Android模拟器是向后兼容的。
Android开发入门第一个程序

eclipse会自动生成一些代码,什么都不用写就可以运行了。简单的看看生成的代码:

lsActivity.java

package com.linuxsight;

import android.app.Activity;
import android.os.Bundle;

public class lsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


Strings.xml   我对它做点修改

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Linux视野</string>
    <string name="app_name">Android入门程序</string>
</resources>

mail.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>

Android开发入门第一个程序

运行程序。

[2011-05-22 14:51:41 - MyAndroid] ------------------------------
[2011-05-22 14:51:41 - MyAndroid] Android Launch!
[2011-05-22 14:51:41 - MyAndroid] adb is running normally.
[2011-05-22 14:51:41 - MyAndroid] Performing com.linuxsight.lsActivity activity launch
[2011-05-22 14:51:41 - MyAndroid] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD '2.1'
[2011-05-22 14:51:51 - MyAndroid] Application already deployed. No need to reinstall.
[2011-05-22 14:51:51 - MyAndroid] Starting activity com.linuxsight.lsActivity on device emulator-5554
[2011-05-22 14:51:53 - MyAndroid] ActivityManager: DDM dispatch reg wait timeout
[2011-05-22 14:51:53 - MyAndroid] ActivityManager: Can't dispatch DDM chunk 52454151: no handler defined
[2011-05-22 14:51:54 - MyAndroid] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.linuxsight/.lsActivity }
[2011-05-22 14:51:54 - MyAndroid] ActivityManager: Warning: Activity not started, its current task has been brought to the front

可以看到程序已经出来了。

Android开发入门第一个程序

再看看菜单,其实此过程是当应用程序安装在模拟器上。类似于用模拟器安装软件:http://www.linuxsight.com/blog/1912

Android开发入门第一个程序

好了,第一个程序已经成功运行了。现在已经大概了解了android的项目结构已经运行过程是怎样的。至于各个文件的作用及它们之间的关系还需要继续深入的学习。

给我留言