Android 开发指南
作者: 刘鹏
日期: 2009-07-14
本文总结了开发 Android 程序涉及的主要知识,包括 Android SDK 的安装、Eclispe 的使用、虚拟机的使用、在 G1 手机上安装软件的方法等等。

G1 系统配置

  • 屏幕 320x480
  • 本文使用的 SDK 版本为 Android 1.1 SDK r1

http://bbs.pockoo.com/redirect.php?tid=12455&goto=lastpost

android g1刷成工程机的方法

http://blog.chinaunix.net/u/26691/showart_1825789.html

安装 SDK 及其开发环境

具体的安装过程可以参考下面的文档:

http://developer.android.com/sdk/1.1_r1/installing.html

注意一点,在 Eclispe 上安装 ADT Plugin 时需要使用地址:

http://dl-ssl.google.com/android/eclipse/

而不是

https://dl-ssl.google.com/android/eclipse/

Eclips 的使用

介绍如何使用 Eclips 的文章有很多,大家可以去网上查,在这里提几个重点实 用的技巧。

Ctrl+Shift+O,可以根据源码自动在 eclispe 中自动导入倚赖的包。

往文件夹中拷入了新文件后,在 eclisps 中按 F5 更新即可将新加入的文件加 到工程中来,并实时更新 R.java 文件,图片、layout 文件、anim 文件等都可 以这样加。

Android 下的 printf

在 C/C++ 语言中,常常使用 printf 来输出信息,有时与 debuger 配合来调试 程序。android 往 log 里输出信息。在代码中使用 Log 类来输出信息,如

Log.d("Test", "Hello");

上述代码将会在 Log 中输出 Hello 字符串。Log.d 表示在调试版本中输出,若 是 release 版本则不输出。

与 Log.d 想对应,Log 类还有很多其它方法,如 Log.v 等,具体细节请参阅相关文档。

使用 LogCat 查看 Log 信息,Eclipse 的下方有个页签是写着 "LogCat",这个 就是 LogCat 窗口。LogCat 也可单独使用,运行 adb logcat 就可以启动 LogCat 程序。另外,dmms 可以查看 log 信息。

http://www.cnblogs.com/jacktu/archive/2008/11/22/1339062.html http://code.google.com/android/reference/android/util/Config.html

虚拟机的使用

启动虚拟机只需到 android-sdk-linux_x86-1.0_r2/tools 目录下运行 emulator 就可以了。虚拟机跟 Eclispe 关联的很好,只要你在 Eclipse 中运 行 Android 程序,Eclipse 会自动启动虚拟机并运行程序。

这里重点介绍些如何在虚拟机中使用虚拟 SD Card:

  • make a simulated SD card.

mksdcard <size> <file> , size from 1 byte to 128G and file is your simulated SD card name.

Use "mksdcard 128M mysdcard.img" can create a 128M simulated SD card named mysdcard.img.

  • run the emulator with -sdcard <filepath>

run "emulator -sdcard /home/birdy/mysdcard.img" , then your emulator has a 128M SD card.

  • copy file to your SD card

"adb push /home/birdy/pictures/test.png /sdcard/test.png"

This command will copy a file from your host to emulator's SD card.

  • You must run MediaScanner in DevTools, it will help your emulator to find the file in SD card and the AP will know there are some files in SD card.

ps. if your emulator version is 1.5, ignore step 4 .

http://www.mail-archive.com/android-framework@googlegroups.com/msg00585.html http://www.anddev.org/emulating_a_sd-card-t263.html

升级 SDK 后更新 app

升级 SDK 后需要,原来的工程需要相应更新一下,以 SDK 从 1.0 升级到 1.5 为例,更新工程需要如下步骤:

  1. 在 project properties 中,设定 project build target 为 Android 1.5.
  2. 选择 project/clean,clean 下工程代码
  3. 取消 project/Build Automatic
  4. 选择 project/Build All

Android Intent 机制

Android中提供了Intent机制来协助应用间的交互与通讯,或者采用更准确的说 法是,Intent不仅可用于应用程序之间,也可用于应用程序内部的 Activity/Service之间的交互。

理解Intent的关键之一是理解清楚Intent的两种基本用法:一种是显式的Intent, 即在构造Intent对象时就指定接收者,这种方式与普通的函数调用类似,只是复 用的粒度有所差别;另一种是隐式的Intent,即Intent的发送者在构造Intent对 象时,并不知道也不关心接收者是谁,这种方式与函数调用差别比较大,有利于 降低发送者和接收者之间的耦合。另外Intent除了发送外,还可用于广播。

下面将以同一个应用程序中的Activity切换为例,介绍显式的Intent(Explicit Intent)。

前面的章节已经讨论过Activity的概念,通常一个应用程序中需要多个UI屏幕, 也就需要多个Activity类,并且在这些Activity之间进行切换,这种切换就是通 过Intent机制来实现的。

在同一个应用程序中切换Activity时,我们通常都知道要启动的Activity具体是 哪一个,因此常用显式的Intent来实现。下面的例子用来实现一个非常简单的应 用程序SimpleIntentTest,它包括两个UI屏幕也就是两个 Activity——SimpleIntentTest类和TestActivity类,SimpleIntentTest类有一个 按钮用来启动TestActivity。

程序的代码非常简单,SimpleIntentTest类的源代码如下:


package com.tope.samples.intent.simple;


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;



public class SimpleIntentTest extends Activity implements View.OnClickListener{

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Button startBtn = (Button)findViewById(R.id.start_activity);

        startBtn.setOnClickListener(this);

    }



    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.start_activity:

            Intent intent = new Intent(this, TestActivity.class);

            startActivity(intent);

            break;

        default:

            break;

        }

    }

}

上面的代码中,主要是为“Start activity”按钮添加了OnClickListener,使 得按钮被点击时执行onClick()方法,onClick()方法中则利用了Intent机制,来 启动TestActivity,关键的代码是下面这两行:


Intent intent = new Intent(this, TestActivity.class);

startActivity(intent);

这里定义Intent对象时所用到的是Intent的构造函数之一:

Intent(Context packageContext, Class<?> cls)

两个参数分别指定Context和Class,由于将Class设置为TestActivity.class,这样便显式的指定了TestActivity类作为该Intent的接收者,通过后面的startActivity()方法便可启动TestActivity。

TestActivity的代码更为简单,如下所示:


package com.tope.samples.intent.simple;


import android.app.Activity;

import android.os.Bundle;


public class TestActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.test_activity);

    }

}

注意几个点:

  1. AndroidManifest.xml 里需要列出所有的 Activity;
  2. 保证两个 Activity 的 layout 都存在;
  3. Activity 的 OnCreate 实现中,setContentView (R.layout.xxx) 要指明 layout 。

参考:

  1. http://student.csdn.net/space.php?uid=47527&do=thread&id=2719
  2. An Intent based Activity switching example

其它资源