You are on page 1of 96

----------------------------------- Android

SDCard SDCard SDCard


SDCard
CMD
C:\Documents and Settings\
\Local
Settings\Application
Data\Android\SDK-1.1
sdcard mksdcard
256M ./sdcard.img
SDCard adb
push zyf.mp3 /sdcard/zyf.mp3
SDCard adb
pull /sdcard/mybaby.jpg C:\

SDCard

APK APK APK APK

cmd
Android SDK tools
APK Android
SDK tools
adb install Snake.pak

JDK 5 JDK 6 Android


1. Eclipse
2. Eclipse
3. ADT
4. Android SDK
5. Android SDK

1. Eclipse
Android Eclipse Android
Java Android Java
(Java Development Kit, JDK) JDK http://java.sun.com Mac OS X
JDK JDK Eclipse Eclipse
Eclipse Eclipse IDE for Java DevelopersEclipse IDE for Java EE

Developers Eclipse JDT


Eclipse IDE for Java Developers

2. Eclipse
Eclipse Java Eclipse
Eclipse Eclipse Eclipse
Eclipse Eclipse IDE
Android

3. ADT
Eclipse Android (ADT)

Eclipse 3.5
Help->Install New Softare
Available Software Add... ()
"Location"(Name) ADT (URL) http://dlssl.google.com/android/eclipse/site.xml "OK" Eclipse

https://dl-ssl.google.com/android/eclipse/site.xml Developer Tools


Install
Next() EclipseYes

Eclipse 3.4
Help->Software Updates
Available SoftwareAdd Site...()
"Location"(URL) http://dlssl.google.com/android/eclipse/site.xml "OK" Eclipse

https://dl-ssl.google.com/android/eclipse/site.xml Developer Tools


Install

Next() EclipseYes

Eclipse 3.3
Help->Software Updates->Find and Install

Search for new features to install( )Next()

New Remote Site()New Update Site(


)
(Name) ADT (URL) http://dlssl.google.com/android/eclipse/site.xml OK
Finish Eclipse
Eclipse
Eclipse OK

Android
http://developer.android.com/sdk/adt_download.html

ADT Eclipse
Help->Software UpdatesAvailable Software
Add Site... ()
Local... Android
"OK" InstallEclipse
ADT

4. Android SDK
http://developer.android.com/ Android Android
(Software Development Kit, SDK) SDK Windows

7-zip
Android SDK android_sdk

5. Android SDK
(Preference) Android ( ADT
Android ) SDK Location " Browse..."
android_sdk(Apply)
Android SDK
SDK 1.5 ADT
Android SDKHelp > Software Updates
Installed SoftwareAndroid
Uninstall..

Android SDK Android Android


SDK Android

ManageSDK
SDK

SDK
SDK
Window > Android SDK and AVD Manager
Android SDK/AVD
Installed Packages
Installed Packages

Android SDK Tools

SDK SDK

Available Packages SDK


SDKAdd-On
SDKAdd-OnInstall
Selected
SDK Accept
AllInstall Accepted
Installed Packages
SDK

SDK
Android
SDKDelete...
Available
Packages
SDK
OpenProject

Eclipse File->New->ProjectNew
Project Eclipse
AndroidAndroid ProjectNew Android Project

SDK ApiDemos New Android Project


"Browse..."
(Create project from existing source)
android_sdk/platforms/android-2.0/samples Android
(android_sdk/platforms/android-2.0/samples/ApiDemos)
New Android Project
Project Name
Finish
Eclipse

(WorkSpace)

File->ImportImportGeneral>Existing Projects into WorkSpaceNextSelect


Root DirectoryBrowse...
Finish(WorkSpace) Eclipse
SDK android_sdk/platforms/android-/samples

Eclipse Package Explorer

ADT Package ExplorerApiDemos


Android Tools(Fix Project
Properties) (Android Tools->Fix Project Properties)
/
gen
XML AndroidManifest.xml res .xml

gen
XML

SDK
Android SDK 1.5 SDK
SDK

NavigatorApiDemosproperties

Android Project Build Target


OK

PlayEmulator
Android

Android
EclipseApiDemos
Android

Android
Android

Android Android
Android
Android Android Android Virtual DeviceAVD
Android SDK 1.5
Eclipse
SDK android android-sdk/tools
android


android-sdk/tools androidsdk/tools
Windows 2000XP2003Windows 7 > > >
(S)PATH(I)
Android SDK C:\android-sdk\tools
;C:\android-sdk\tools

> cmd

android-sdk/tools
$ android list targets

Android SDK Linux Mac


$ ./android list targets

$ android list targets


Available Android targets:
id: 1
Name: Android 1.1
Type: Platform
API level: 2
Skins: HVGA (default), HVGA-L, HVGA-P, QVGA-L, QVGA-P
id: 2
Name: Android 1.5
Type: Platform
API level: 3
Skins: HVGA (default), HVGA-L, HVGA-P, QVGA-L, QVGA-P
id: 3
Name: Google APIs
Type: Add-On
Vendor: Google Inc.
Description: Android + Google APIs
Based on Android 1.5 (API level 3)

Libraries:
* com.google.android.maps (maps.jar)
API for Google Maps
Skins: HVGA (default), HVGA-L, QVGA-P, HVGA-P, QVGA-L

(id) 12 Android 1.11.5


(id) 3 Google APIsGoogle Google MapGoogle
APIs Google Map Google
3

Android SDK 2.0

$ android create avd --target 1 --name eclair

android create avd

--target 1

id 1 SDK Android 1.5--name eclair


eclair

$ android create avd --target 1 --name eclair


Android 2.0 is a basic Android platform.
Do you wish to create a custom hardware profile [no]
Created AVD 'cupcake' based on Android 2.0

Androidlist avd

$ android list avd

$ android list avd


Available Android Virtual Devices:
Name: eclair
Path: /Users/mac/.android/avd/cupcake.avd

Target: Android 2.0 (API level 5)


Skin: HVGA

android list avd Eclipse

ApiDemos ApiDemos

Run()(Run-> Debug
Configurations...)
Eclipse "Android
Application"(Android ) "New"()
New Android Application
Name
ApiDemos
ProjectBrowse...Project Selection
ApiDemosOK
Launch Action Launch Default Activity
DebugEclipse
Android
:
Run Configuration...
DebugRun
DebugRun
Debug logd

Android
Debug
Run

skin
skin
TargetHVGA 480x320QVGA
320x240
android list targets

id 1
id: 1
Name: Android 2.0
Type: Platform
API level: 5
Skins: HVGA (default), QVGA, WQVGA400, WQVGA432, WVGA800, WVGA854

Skins
QVGA

android create avd

--skin QVGA

HVGA

Windows CtrlF12 Mac OS X fn


7

?
Android SDK adb (Android Debugger) ( android-sdk/tools )
shell
root
adb shell
$ adb shell

data/app

$ cd data/app/

ls ( windows dir )
# ls
com.example.android.apis.apk

rm ApiDemos
# rm com.example.android.apis.apk
# ls


android list avd
$ android list avd
Available Android Virtual Devices:
Name: eclair
Path: /Users/mac/.android/avd/cupcake.avd
Target: Android 2.0 (API level 5)
Skin: HVGA

eclair android delete avd --name


eclaireclair
$ android delete avd --name eclair
AVD 'eclair' deleted.

android list avd


$ android list avd
Available Android Virtual Devices:

ReadSource
Android

(BMI) Android

BMI

Body Mass Index BMI


...
BMI

BMI
BMI
...: BMI
25
BMI 20

BMI Android
http://zh.wikipedia.org/wiki/

BMI
(Create new project in workspace)
""
(Use default location)

(Properties)

"New Android Project"

Project Name
Application
Name

Package
Name

(Package)JAVA (class)

Create
Activity

Activity Activity

Build Target

SDK Build Target Min


SDK Version

Min SDK
Version

SDK

Project Name

BMI

Application
Name

BMI

Package Name

com.demo.android.b
mi

Create Activity

Bmi

Min SDK Version 5


Finish
Package Name com.androidActivity
Name java Activity (class)
java Activity Name
Eclipse Package Explorer BMI

Android BMI
Android
Android

src/ (source)
src Android
package ( BMI /src/com/demo/android/bmi/)
src :
1. Bmi.java Create Activity
Android
src Android

gen/ (Generate)
gen
gen R.java

1. R.java
ADT res XML
'R.java' Android R.java
R.java
R.java R

(id)Android R

res/ (Resource)
res""
XML Android res

res/ :
3. layout/ (layout) layout XML
layout
XML HTML
XML
()
Java XML
R.java
R
4. values/ (value) values XML
() R

Android
5. AndroidManifest.xml
AndroidManifest.xml Android
(ContentProvider)
(
GPS ) (Activity)
Activity

AndroidUI

Hello World, Bmi! Android

Android
Hello World, Bmi!
Hello World, Bmi!
res/layout/main.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

10

android:text="string/hello"

11

/>

12 </LinearLayout>

Hello World, Bmi!res/layout/main.xml 10


10
android:text="Hello World, Bmi!

Hello World, Bmi!android:textHello World, Bmi!


BMI
android:text="BMI"

Android

Android Hello World, Bmi

main.xml XML
Android ViewGroup View ViewGroup View
Android

Android XML

Hello World, Bmimain.xml


main.xml
1
<?xml version="1.0" encoding="utf-8"?>

XML (Extensible Markup Language)


HTML XML Java
main.xml XML
XML
2, 6 12
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"></LinearLayout>

HTML
<LinearLayout></LinearLayout>

" "(LinearLayout)LinearLayout
/""
LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns XML (NameSpace)


URL() Android Android


<linearlayout>

</linearlayout>

( </> )Eclipse

3-5
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"

<linearlayout>

LinearLayoutAndroid layout
android:

Android
android:layout_widthandroid:layout_height
fill_parentfill_parent ""
LinearLayout
LinearLayout()
android:orientation
() vertical()

android.view.ViewGroup(layout)(View)

LinearLayout()FrameLayout()TableLayout()
AbsoluteLayout( )RelativeLayout()

LinearLayout 7

7 11
<TextView
/>

TextView ()
/>
/> XML

8-10
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, bmi"

TextView
android: layout_widthandroid:layout_height
android: layout_width fill_parent (
LinearLayout )
android:layout_height

wrap_content
(
)android:text
TextView @string/hello

android:text
XML main.xml

BmiUI

(View)

MVCModel-View-Controller
Google Android Android
-- XML
Android TextView
... BMI
BMI
BMI TextView

TextView BMI

Android
http://developer.android.com/guide/tutorials/views/index.html
EditText EditText
http://developer.android.com/reference/android/widget/EditText.html

EditText EditText
EditText TextView TextView
EditText TextView
EditText android:numeric="integer"( )
android:phoneNumber="true"()android:autoLink="all"(
) EditText XML
EditText android:numerictrue EditText

Android android-sdk
android-sdk/docs (android_sdk/docs/reference/viewgallery.html)

BMI (Height)
EditText TextView
1 <TextView
2

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=" (cm)"

/>

6 <EditText android:id="@+id/height"
7

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:numeric="integer"

10

android:text=""

11

/>

EditText TextView
android:layout_widthandroid:layout_height
android:numeric
android:text EditText
android:text
TextView
android:numeric="integer"
android:text=""

android:numericinteger EditText
android:text EditText ()
Button ()
<Button android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" BMI "
/>

Button android:layout_widthandroid:layout_height
android:text

TextView()EditText()Button()
LinearLayout ()
main.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=" (cm)"
/>
<EditText android:id="@+id/height"

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text=""
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" (kg)"
/>
<EditText android:id="@+id/weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text=""
/>
<Button android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" BMI "
/>
<TextView android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<TextView android:id="@+id/suggest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>

Layout
(cm)

(kg) BMI
()
XML EditText
XML TextView
android:text


" BMI "
BMI
... BMI
android:idandroid:id Android
UI

ADT
ADT GUI
ADT GUI Android GUI

DroidDraw - Android GUI http://code.google.com/p/droiddraw/

XmlR

XML android:
( LinearLayoutTextView)
TextView
(cm)(reference) Button
EditText XML android:id
...
android:id
<EditText android:id="@+id/height"
/>

XML ()
android:id EditText
android:idandroid:id
android:id

android:id
@+id/height

height android:idR.id.height
+idR
R R.java

XML R.java
Android XML UI
XML XML

Android XML
Android XML Android
XML
(...)
BMI gen/com/demo/android/bmiR.java

/* AUTO-GENERATED FILE. DO NOT MODIFY.


*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.demo.android.bmi;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040000;
}
}

XML gen/com/demo/android/bmi
R.java
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.demo.android.bmi;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int height=0x7f050000;
public static final int result=0x7f050003;
public static final int submit=0x7f050002;
public static final int suggest=0x7f050004;
public static final int weight=0x7f050001;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040000;
}
}

R.java attr ()drawable ()id ()layout (


)string () XML id XML
id
Google R.java
A project's R.java file is an index into all the resources defined in the file.
You use this class in your source code as a sort of short-hand way to refer to
resources you've included in your project. This is particularly powerful with the
code-completion features of IDEs like Eclipse because it lets you quickly and
interactively locate the specific reference you're looking for.

The important thing to notice for now is the inner class named "layout", and
its member field "main". The Eclipse plugin noticed that you added a new XML
layout file and then regenerated this R.java file. As you add other resources to
your projects you'll see R.java change to keep up.

R.java XML
@[]/[]

@+id/height EditText

XML
res XML res
R.java
XML
res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="app_name">BMI</string>
</resources>

app_name AndroidManifes
t.xml
<string name=""></string>

strings.xml
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">BMI</string>
<string name="bmi_height"> (cm)</string>
<string name="bmi_weight"> (kg)</string>

<string name="bmi_btn"> BMI </string>


<string name="bmi_result"> BMI </string>
</resources>

strings.xml app_name
R.java string
public static final class string {
public static final int app_name=0x7f040000;
public static final int bmi_btn=0x7f040003;
public static final int bmi_result=0x7f040004;
public static final int bmi_height=0x7f040001;
public static final int bmi_weight=0x7f040002;
}

XML
@string/[]

string main.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/bmi_height"
/>
<EditText android:id="@+id/height"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text=""
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/bmi_weight"

/>
<EditText android:id="@+id/weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text=""
/>
<Button android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bmi_btn"
/>
<TextView android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<TextView android:id="@+id/suggest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>

Android
string.xml
BMI (View)

XML
res
R.javasrc/values

res/valuesadvice.xml BMI BMI

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="advice_light"></string>
<string name="advice_average"></string>

<string name="advice_heavy"></string>
</resources>

R.javaadvice_light
advice_average
advice_heavy
R.java
Android XML
R.java

Android

AndroidLogic

src/com/demo/android/bmiBmi.java
Eclipse+Android
1 package com.demo.android.bmi;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5
6 public class Bmi extends Activity {
7

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

@Override

public void onCreate(Bundle savedInstanceState) {

10

super.onCreate(savedInstanceState);

11

setContentView(R.layout.main);

12

13 }

XML Java

Bmi.java
1 :
package com.demo.android.bmi;

package()

package Java package


package package
com.demo.android.bmi

com/demo/android/bmi

Java
;
3,4 :
import android.app.Activity;
import android.os.Bundle;

android.app.Activityandroid.os.Bundle Package
Android Package
import() Package
Java API Package
Package
Android Package Java(j2se) Android
API Package
jar Package
API package
http://code.google.com/android/reference/packages.html
6,13 :
public class Bmi extends Activity {
}

6
public class Bmi

Bmi
class
publicBmi
Bmi package
public class Bmiextends ActivityBmi
Activity
extends(Inherit)
Activity 3
Package
Bmi Bmi
Activity
{}

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

7
/*

*/ Java

/**Java
////
/*

*/

//
8-9, 12 :
@Override
public void onCreate(Bundle savedInstanceState) {
}

9 (Method)
public void onCreate(Bundle savedInstanceState) {
}

onCreate
void(type)
public
onCreateonCreate bmi

onCreatevoid
onCreate
savedInstanceStateBundleBundle
Package Bundle
savedInstanceStateBundle
Android savedInstanceState
onCreate
Bundle savedInstanceState

Bundle
Bundle onFreeze (
onCreate ) Activity
onCreate savedInstanceState
Bundle Activity Activity Activity
onCreate/onFreeze
{} onCreate

@Override
public void onCreate(Bundle savedInstanceState)

Android Referenced
Libraries
android.appActivity.class

Activity.class Android Referenced


Libraries
android.app
Activity.class

Activity.class
/
Activity / onCreate (Method)
bmi Activity bmionCreate
onCreate Activity
@
(decorator)@OverrideonCreate
bmi
onCreate

@Override
public void onCreate(Bundle savedInstanceState) {
}

bmi onCreate onCreate


savedInstanceState Bundle
onCreate
10, 11 :
super.onCreate(savedInstanceState);

super Bmi (Activity)


super.onCreate(savedInstanceState); Activity onCreate

Google Android (View)


(Controller)(Activity) Android
Activity
onCreate Activity
onCreate
Android (@Override)BmionCreate
ActivityonCreateonCreate
super
onCreatesuper.onCreate(savedInstanceState);Activity
onCreateonCreate

onCreateonCreate
super.onCreate(savedInstanceState);savedInstanceState
onCreatesavedInstanceStatepublic void onCreate(Bundle
savedInstanceState)
setContentView(R.layout.main);

Activity View setContentView


XML Activity Activity
XML res/layout/main.xml

setContentView Activity
setContentViewR.layout.main
XML
res

AndroidLogic

src/com/demo/android/bmiBmi.java
Eclipse+Android
1 package com.demo.android.bmi;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5
6 public class Bmi extends Activity {
7

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

@Override

public void onCreate(Bundle savedInstanceState) {

10

super.onCreate(savedInstanceState);

11

setContentView(R.layout.main);

12

13 }

XML Java

Bmi.java

1 :
package com.demo.android.bmi;

package()

package Java package
package package
com.demo.android.bmi

com/demo/android/bmi

Java
;
3,4 :
import android.app.Activity;
import android.os.Bundle;

android.app.Activityandroid.os.Bundle Package
Android Package
import() Package
Java API Package
Package
Android Package Java(j2se) Android
API Package
jar Package
API package
http://code.google.com/android/reference/packages.html
6,13 :
public class Bmi extends Activity {
}

6
public class Bmi

Bmi
class
publicBmi
Bmi package

public class Bmiextends ActivityBmi


Activity
extends(Inherit)
Activity 3
Package
Bmi Bmi
Activity
{}
7 :
/** Called when the activity is first created. */

7
/*

*/ Java

/**Java
////
/* */
//
8-9, 12 :
@Override
public void onCreate(Bundle savedInstanceState) {
}

9 (Method)
public void onCreate(Bundle savedInstanceState) {
}

onCreate
void(type)
public
onCreateonCreate bmi

onCreatevoid
onCreate
savedInstanceStateBundleBundle
Package Bundle
savedInstanceStateBundle
Android savedInstanceState
onCreate
Bundle savedInstanceState

Bundle
Bundle onFreeze (
onCreate ) Activity
onCreate savedInstanceState
Bundle Activity Activity Activity
onCreate/onFreeze
{} onCreate
@Override
public void onCreate(Bundle savedInstanceState)

Android Referenced
Libraries
android.appActivity.class

Activity.class Android Referenced


Libraries
android.app
Activity.class

Activity.class
/
Activity / onCreate (Method)
bmi Activity bmionCreate
onCreate Activity
@
(decorator)@OverrideonCreate
bmi
onCreate

@Override
public void onCreate(Bundle savedInstanceState) {
}

bmi onCreate onCreate


savedInstanceState Bundle
onCreate
10, 11 :
super.onCreate(savedInstanceState);

super Bmi (Activity)


super.onCreate(savedInstanceState); Activity onCreate

Google Android (View)


(Controller)(Activity) Android

Activity
onCreate Activity
onCreate
Android (@Override)BmionCreate
ActivityonCreateonCreate
super
onCreatesuper.onCreate(savedInstanceState);Activity
onCreateonCreate

onCreateonCreate
super.onCreate(savedInstanceState);savedInstanceState
onCreatesavedInstanceStatepublic void onCreate(Bundle
savedInstanceState)
setContentView(R.layout.main);

Activity View setContentView


XML Activity Activity
XML res/layout/main.xml

setContentView Activity
setContentViewR.layout.main
XML
res

BmiRefactor

BMI

Android Android MVC


XML XML

(Bmi.java)

BMI

MVC
BMI java Android
MVC MVC Bmi.java

MVC Bmi.java

1 @Override
2 public void onCreate(Bundle savedInstanceState) {
3

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

5
6

//Listen for button clicks

Button button = (Button) findViewById(R.id.submit);

button.setOnClickListener(calcBMI);

9 }

Android

Button button = (Button) findViewById(R.id.submit);


button.setOnClickListener(calcBMI);

7
button.setOnClickListener """"

"onCreate"

MVC ()
onCreate
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
setListensers();
}

findViews
private Button calcbutton;
private EditText fieldheight;
private EditText fieldweight;
private void findViews()
{
calcbutton = (Button) findViewById(R.id.submit);
fieldheight = (EditText) findViewById(R.id.height);
fieldweight = (EditText) findViewById(R.id.weight);
}

buttoncalcbutton
calcbutton
()
//Listen for button clicks
private void setListensers() {
calcbutton.setOnClickListener(calcBMI);
}

package com.demo.android.bmi;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Bmi extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
setListensers();
}
private Button button_calc;
private EditText field_height;
private EditText field_weight;
private TextView view_result;
private TextView view_suggest;
private void findViews()
{
button_calc = (Button) findViewById(R.id.submit);
field_height = (EditText) findViewById(R.id.height);
field_weight = (EditText) findViewById(R.id.weight);
view_result = (TextView) findViewById(R.id.result);
view_suggest = (TextView) findViewById(R.id.suggest);
}
//Listen for button clicks
private void setListensers() {
button_calc.setOnClickListener(calcBMI);
}
private Button.OnClickListener calcBMI = new Button.OnClickListener()
{
public void onClick(View v)
{
DecimalFormat nf = new DecimalFormat("0.0");
double height = Double.parseDouble(field_height.getText().toString())/100;
double weight = Double.parseDouble(field_weight.getText().toString());
double BMI = weight / (height * height);

//Present result
view_result.setText(getText(R.string.bmi_result) + nf.format(BMI));
//Give health advice
if(BMI>25){
view_suggest.setText(R.string.advice_heavy);
}else if(BMI<20){
view_suggest.setText(R.string.advice_light);
}else{
view_suggest.setText(R.string.advice_average);
}
}
};
}

calcBMI
calcBMI
OnClickListener

Button.OnClickListener
(import)OnClickListener
android.view.View.OnClickListener
import android.view.View.OnClickListener;

Button.OnClickListenerButton.OnClickListener
android.widget.ButtonOnClickListenerButton
OnClickListener
OnClickListener findViews

private void findViews()


{
button_calc = (Button) findViewById(R.id.submit);
field_height = (EditText) findViewById(R.id.height);
field_weight = (EditText) findViewById(R.id.weight);
view_result = (TextView) findViewById(R.id.result);
view_suggest = (TextView) findViewById(R.id.suggest);
}

button_
field_view_


TextView result = (TextView) findViewById(R.id.result);
result.setText("Your BMI is "+nf.format(BMI));

//Present result
view_result.setText(getText(R.string.bmi_result) + nf.format(BMI));

TextView view_result findViews

AndroidDialog

BMI..
...BMI

openOptionsDialogcalcBMI
OnClickListener BMI
Android

Android (View)

Android
# #

Bmi.java
Bmi.java
1 private OnClickListener calcBMI = new OnClickListener()
2 {
3

public void onClick(View v)

.
6

....
}else{

view_suggest.setText(R.string.advice_average);

openOptionsDialog();

calcBMIopenOptionsDialog(); BMI

calcBMIOnClickListener
private void openOptionsDialog() {
new AlertDialog.Builder(Bmi.this)
.setTitle(" Android BMI")
.setMessage("Android BMI Calc")
.show();

new AlertDialog.Builder(Bmi.this).show()

AlertDialog AlertDialog Builder


show()

.setTitle(" Android BMI")

.setMessage("Android BMI Calc")

res/values/strings.xml
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
....
<string name="about_title"> Android BMI</string>
<string name="about_msg">Android BMI Calc\n
gasolin\n\n
gasolin+android [at] gmail.com</string>
....
</resources>

openOptionsDialog
private void openOptionsDialog() {
new AlertDialog.Builder(Bmi.this)
.setTitle(R.string.about_title)
.setMessage(R.string.about_msg)
.show();

BMI

Undo

.setPositiveButton("",
new DialogInterface.OnClickListener(){
public void onClick(
DialogInterface dialoginterface, int i){
}
})

setPositiveButton
setNegativeButtonsetNeutralButton
//
setPositiveButton
(DialogInterface)

private void openOptionsDialog() {


new AlertDialog.Builder(Bmi.this)
.setTitle(R.string.about_title)
.setMessage(R.string.about_msg)
.setPositiveButton(R.string.ok_label,
new DialogInterface.OnClickListener(){
public void onClick(
DialogInterface dialoginterface, int i){
}
})
.show();
}
}

http://code.google.com/android/reference/android/app/Al
ertDialog.Builder.html

-Toast


Toast
Toast

import android.widget.Toast;
...
private void openOptionsDialog() {
Toast.makeText(Bmi.this, "BMI ", Toast.LENGTH_SHORT).show();
/*new AlertDialog.Builder(this) //
...
*/
}

BMI
BMI

Toast.makeText(Bmi.this, "BMI ", Toast.LENGTH_SHORT).show();

Toast Toast (LENGTH_SHORT


) show() Toast

Toast
BMI

try...catch Toast
DecimalFormat nf = new DecimalFormat("0.00");
try{
double height =
Double.parseDouble(field_height.getText().toString())/100;
double weight = Double.parseDouble(field_weight.getText().toString());
double BMI = weight / (height * height);
//Present result
view_result.setText(getText(R.string.bmi_result) + nf.format(BMI));
//Give health advice
if(BMI>25){
view_suggest.setText(R.string.advice_heavy);
}else if(BMI<20){
view_suggest.setText(R.string.advice_light);
}else{
view_suggest.setText(R.string.advice_average);
}
}
catch(Exception err)
{
Toast.makeText(Bmi.this, "",

Toast.LENGTH_SHORT).show();
}

try
{

}
catch(Exception err)
{

try...catch Java try


catch try
try catch
catch
BMI catch

Toast.makeText(Bmi.this, "", Toast.LENGTH_SHORT).show();

Toast

res/values/strings.xml
....
<string name="input_error"></string>
</resources>

R.string.input_error
Toast.makeText(Bmi.this, R.string.input_error, Toast.LENGTH_SHORT).show();

AndroidUrl
Intent

Android Android

openOptionsDialog

openOptionsDialogres/values/string.xml
res/values/string.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3

<string name="app_name">BMI</string>

<string name="height"> (cm)</string>

<string name="weight"> (kg)</string>

<string name="bmi_btn"> BMI </string>

<string name="bmi_result"> BMI </string>

8
9
10

<string name="about_title"> Android BMI</string>


<string name="about_msg">Android BMI Calc 0.6\n

11

gasolin\n

12

gasolin+android [at] gmail.com</string>

13

<string name="ok_label"></string>

14

<string name="homepage_label"></string>

15 </resources>

openOptionsDialog
1 private void openOptionsDialog() {
2

new AlertDialog.Builder(this)

.setTitle(R.string.about_title)

.setMessage(R.string.about_msg)

.setPositiveButton(R.string.ok_label,

6
7

new DialogInterface.OnClickListener(){
public void onClick(

8
9
10
11
12
13

DialogInterface dialoginterface, int i){


}
})
.setNegativeButton(R.string.homepage_label,
new DialogInterface.OnClickListener(){
public void onClick(

14

DialogInterface dialoginterface, int i){

15

//go to url

16

Uri uri = Uri.parse("http://androidbmi.googlecode.com/");

17

Intent intent = new Intent(Intent.ACTION_VIEW, uri);

18

startActivity(intent);

19

20

})

21

.show();

22

openOptionsDialogsetNegativeButton
NegativeButton
.setNegativeButton(R.string.homepage_label,
new DialogInterface.OnClickListener(){
public void onClick(
DialogInterface dialoginterface, int i){
.....
}
})

DialogInterface
()
http://androidbmi.googlecode.com/

//go to url
Uri uri = Uri.parse("http://androidbmi.googlecode.com/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

Uri uri = Uri.parse("http://androidbmi.googlecode.com/");

Uri http://androidbmi.googlecode.com/
UriUriUri
importEclispectrl-shift-O
Windowscmd-shift-OMacandroid.net.Uri
startActivity(intent);

startActivityAndroid (Intent)

Android Activity startActivity


startActivity(Intent)
IntentActivity
Intent intent = new Intent(Intent.ACTION_VIEW, uri);

(Intent)
Intent
Intent intent = new Intent(, );

(Intent) Intent.ACTION_VIEW
Intent.ACTION_VIEW Android Eclipse Intent.
Eclipse
Intent
Intent.ACTION_VIEW
(
)()
UriIntent.ACTION_VIEW
http://androidbmi.googlecode.com/

Uri
Uri uri = Uri.parse("http://androidbmi.googlecode.com/");

Uri.parse() Resource
res/values/string.xml
res/values/string.xml
...
<string name="homepage_label"></string>
<string name="homepage_uri">http://androidbmi.googlecode.com/</string>

Uri.parse() .
Uri uri = Uri.parse(R.string.homepage_uri);

Eclipse
Uri.parse Uri.
Eclipse parse Uri.parse()



Uri uri = Uri.parse(getString(R.string.homepage_uri));

android.content.ContextgetString( getText)

AndroidMenu
(Menu)
Intent Activity
openOptionsDialog calcBMI
OnClickListeneropenOptionsDialogOnClickListener
Menu(Menu Bar)
openOptionsDialog

1 protected static final int MENU_ABOUT = Menu.FIRST;


2 protected static final int MENU_Quit = Menu.FIRST+1;
3
4 @Override
5 public boolean onCreateOptionsMenu(Menu menu) {
6

super.onCreateOptionsMenu(menu);

menu.add(0, MENU_ABOUT, 0, "...");

menu.add(0, MENU_Quit, 0, "");

return true;

10 }
11
12 @Override
13 public boolean onOptionsItemSelected(MenuItem item)
14 {
15

super.onOptionsItemSelected(item);

16

switch(item.getItemId()){

17

case MENU_ABOUT:

18

openOptionsDialog();

19
20

break;
case MENU_Quit:

21

finish();

22

break;

23

24

return true;

25 }


1.
2.
onCreateOptionsMenu Android Menu
()onCreateOptionsMenu Activity
onCreateOptionsMenu Menu()
Android
onOptionsItemSelected
...

onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return true;
}

onCreateOptionsMenupublic()
Menumenu
booleanboolean
true
@Override
Activity
onCreate (Override)
super.onCreateOptionsMenu(menu)
onCreateOptionsMenu
menu.add(0, MENU_ABOUT, 0, "...");
menu.add(0, MENU_Quit, 0, "");

Android Activity Activity menu

menu.add(0, (identifer), 0, );

protected static final int MENU_ABOUT = Menu.FIRST;


protected static final int MENU_Quit = Menu.FIRST+1;

MENU_ABOUT static final int


Menu.FIRSTMenu.FIRST

OptionsItemSelected
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
return true;
}

onOptionsItemSelectedonCreateOptionsMenu
public()
onOptionsItemSelectedMenuItem
item
booleanboolean
true
super.onOptionsItemSelected(item);
ActivityonOptionsItemSelected

switch(item.getItemId()){

item.getItemId()(identifer)

switch(){
....
}

swith item.getItemId()

case MENU_ABOUT:
openOptionsDialog();
break;
case
....
break;

onOptionsItemSelected MENU_ABOUT
openOptionsDialog
case MENU_Quit:
finish();
break;

onOptionsItemSelectedMENU_Quit Android
finish ActivityBMIBmiActivity
finishBmi ActivityBMI
Android Activity
Android (Dalvik) Activity Android Activity

AndroidManifest
Android
Intent startActivityIntent
Activity
Android Android
Activity
Android
Android Android
(ARMx86MIPS)
Android
GPS
Android

startActivity ActivityIntent()
Android Android startActivity Intent
Activity
Android Android
Activity Android
BMI BMI Android BMI
Bmi Activity
Android AndroidManifest.xml

Android Android

:
Android
1. Activity ()
2. Broadcast Intent Receiver
3. Service
4. Content Provider

Activity
eclipse Android BMI/AndroidManifest.xml
AndroidManifest.xml BMI/AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3

package="com.demo.android.bmi"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon">

<activity android:name=".Bmi" android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

10

<category android:name="android.intent.category.LAUNCHER" />

11

</intent-filter>

12

</activity>

13

</application>

14

<uses-sdk android:minSdkVersion="3" />

15 </manifest>

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
....>
....
</manifest>

AndroidManifest.xml XML Android


AndroidManifest.xmlAndroidManifest.xml
AndroidManifest.xmlmanifest()
package="com.demo.android.bmi"

packagemanifest()
com.demo.android.bmi/
android:versionCode="1"
android:versionName="1.0"

android:versionCodeandroid:versionName(
)
android:versionName1.0
2.0
android:versionCode

<application android:icon="@drawable/icon" android:label="@string/app_name">


...
</application>

manifestapplication( 1)
application
Activity
applicationandroid:icon
Android
android:icon="@drawable/icon"
res/drawable/icon 64x64
Pixel
applicationandroid:label Home

<activity android:name=".Bmi" android:label="@string/app_name">


...
</activity>

application Activity activity( 2)Activity


Android
Activity Activity
applicationactivity Activity
Activity AndroidManifest.xmlactivity
Activity Activity
activityandroid:name Activity (class)
activity
android:label Activity
setTitle()
Manifestpackage="com.demo.android.bmi"
activityandroid:name.Bmicom.demo.android.bmi.Bmi
Bmicom.demo.android.bmi.Bmi

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

intent-filteractivityintent-filteraction
category actionandroid:name
android.intent.action.MAIN Activity
main Activity
android.intent.action.EDITcategory
android:nameandroid.intent.category.LAUNCHER Activity
Launcher
com.demo.android.bmi
Bmi.java Activity; Android
res/drawable/iconAndroid
android.intent.action.MAIN.Bmiactivity

<uses-sdk android:minSdkVersion="3" />

Android SDK 1.1


SDK
1 Android SDK 1.02
SDK 1.13 SDK 1.5
Google Android Market
android:minSdkVersion
1
applicationuses-permission( SMS
)
permission
instrumentation

2
activity Android servicereceiver
provider

Android
manifest http://developer.android.com/reference/android/R.styleable.html#AndroidManif
est

Intent Action http://developer.android.com/reference/android/content/Intent.html

AndroidActivity
Activity
Activity (
)Activity
Activity MVC ControlActivity UI UI

Activity Activity Activity


Activity Activity

Activity
Activity Activity
Activity Activity Intent
startActivity Activity
Activity

Activity
Activity Activity Activity Activity
Activity
Activity;
Activity Activity Activity
Activity

Activity
BMI Android
Activity
Activity BMI
BMI Android
Activity XML
XML

Activity

1. Activity
2. Activity
3. Activity startActivity

Activity
Navigator src/com/demo/android/bmibmi
Bmi.javaR.java Activity
bmiNew->ClassEclipse New Java Class

NameReport
Report Java

SuperclassBrowse...Eclipse Superclass Selection


Choose a typeactivityMatching items
Activity - android.app - ...ok

Superclassandroid.app.ActivityFinish
Eclipse bmiReport.java
Report.java
1 package com.demo.android.bmi;
2
3 import android.app.Activity;
4
5 public class Report extends Activity {
6
7 }

Android XML
XML
class

res/layoutreport.xml xml

1 <?xml version="1.0" encoding="utf-8"?>


2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
4

android:orientation="vertical"
android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

10

android:text="Hello World, Bmi"

11 />
12 </LinearLayout>

src/com/demo/android/bmi/Report.java

1 package com.demo.android.bmi;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5
6 public class Report extends Activity {
7

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

@Override

public void onCreate(Bundle savedInstanceState) {

10

super.onCreate(savedInstanceState);

11

setContentView(R.layout.report);

12

13 }

setContentView(R.layout.main);

setContentView(R.layout.report);

XML

Activity

AndroidManifest.xmlApplicationApplication
Application NodesAndroidManifest.xml
Activity Report Activity
AndroidManifest.xml
Application NodesAdd...Activity
okApplication
Application NodesActivity
ActivityApplication NodesAttributes for Activity
Name*Browse...
ActivityReport - com.demo.android.bmiok
ApplicationName*ReportApplication Nodes
Report(Activity)

activity
ApplicationAndroidManifest.xml

activity Report activity


AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3
4
5
6
7
8
9
10
11

package="com.demo.android.bmi">
<application android:icon="@drawable/icon">
<activity android:name=".bmi" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Report"></activity>

12 </application>
13 </manifest>

activity
Android AndroidManifest.xml
AndroidManifest.xml

<activity android:name="Report"></activity>

Report Activity BMI


Activity Reportreport activitylabel

1. res/values/ report.xml Report

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="report_title">BMI </string>
</resources>

2. AndroidManifest.xml Report activity


(label)
<activity android:name="Report" android:label="@string/report_title"></activity>

Activity startActivity
AndroidManifest.xml activity

Bmi
Report Activity
BMI Activitysrc/com/demo/android/bmi/Bmi.java
OnClickListener() Bmi Activity
Report Activity
1 private Button.OnClickListener calcBMI = new Button.OnClickListener()
2 {
3

public void onClick(View v)

//Switch to report page

Intent intent = new Intent();

intent.setClass(Bmi.this, Report.class);

startActivity(intent);

10 };

Android BMi Hello


World, Bmi( Report Activity)
ActivityUndo
( Bmi Activity)

Intent intent = new Intent();

(Intent)
intent.setClass(Bmi.this, Report.class);

Activity class Activity class


startActivity(intent);

intent startActivity
startActivity intent Android
Android ( Report Activity
activity ?) Report Activity
Activity

AndroidIntent
Activity
Intent Intent Activity

ActivityAndroid Intent Intent


Activity Activity
Activity Intent
Intent Intent Intent Intent

Intent Android IntentFilter


Intent Intent Intent
Activity
Activity Intent

Intent

Report Activity
BMI ( Bmi
Activity)(Report Activity)

Intent
Intent
BMI
src/com/demo/android/bmi/Bmi.javaButton.OnClickListener
1

private Button.OnClickListener calcBMI = new Button.OnClickListener()

public void onClick(View v)

//Switch to report page

Intent intent = new Intent();

intent.setClass(Bmi.this, Report.class);

Bundle bundle = new Bundle();

bundle.putString("KEY_HEIGHT", field_height.getText().toString());

10

bundle.putString("KEY_WEIGHT", field_weight.getText().toString());

11

intent.putExtras(bundle);

12

startActivity(intent);

13

14 };

Intent intent = new Intent();


intent.setClass(Bmi.this, Report.class);
...
startActivity(intent);

Intent (Android
) Bmi Activity Report Activity
Activity
Bundle bundle = new Bundle();
...
intent.putExtras(bundle);

Activity Activity Activity


Activity Intent
intent
Report Activity Intent

Bundle intent.putExtras(bundle)bundle
Intent Intent
bundle.putString("KEY_HEIGHT", field_height.getText().toString());
bundle.putString("KEY_WEIGHT", field_weight.getText().toString());

bundle
Bundle map
KEY_HEIGHT
KEY_WEIGHT
bundle
Activity
KEY_HEIGHT
KEY_WEIGHT

Bundle API Intent Bundle

Intent
Intent Report Activity

res/values/report.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="report_title">BMI </string>
<string name="report_back"></string>
</resources>

res/layout/report.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:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""

/>
<TextView android:id="@+id/suggest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<Button android:id="@+id/report_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/report_back"
/>
</LinearLayout>

Eclipse layout
TextView Report
Activity BmiActivity

Activity
Intent Intent BMI
src/com/demo/android/bmi/Report.java
1 package com.demo.android.bmi;
2
3 import java.text.DecimalFormat;
4 import android.app.Activity;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.widget.Button;
8 import android.widget.TextView;
9
10 public class Report extends Activity {
11

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

12 @Override
13

public void onCreate(Bundle savedInstanceState) {

14

super.onCreate(savedInstanceState);

15

setContentView(R.layout.report);

16

findViews();

17

showResults();

18

setListensers();

19 }
20
21

private Button button_back;

22 private TextView view_result;


23 private TextView view_suggest;
24
25 private void findViews()
26 {
27

button_back = (Button) findViewById(R.id.report_back);

28

view_result = (TextView) findViewById(R.id.result);

29

view_suggest = (TextView) findViewById(R.id.suggest);

30 }
31
32 //Listen for button clicks
33 private void setListensers() {
34

button_back.setOnClickListener(backMain);

35 }
36
37 private Button.OnClickListener backMain = new Button.OnClickListener()
38 {
39

public void onClick(View v)

40

41

// Close this Activity

42

Report.this.finish();

43

44 };
45
46 private void showResults() {
47

DecimalFormat nf = new DecimalFormat("0.00");

48
49

Bundle bunde = this.getIntent().getExtras();

50

double height = Double.parseDouble(bunde.getString("KEY_HEIGHT"))/100;

51

double weight = Double.parseDouble(bunde.getString("KEY_WEIGHT"));

52

double BMI = weight / (height * height);

53

view_result.setText(getString(R.string.bmi_result) +nf.format(BMI));

54
55

//Give health advice

56

if(BMI>25){

57
58

view_suggest.setText(R.string.advice_heavy);
}else if(BMI<20){

59
60

view_suggest.setText(R.string.advice_light);
}else{

61
62

view_suggest.setText(R.string.advice_average);
}

63
64 }
65 }


BMI
Bundle bunde = this.getIntent().getExtras();

Intent Activity Activity.getIntent()


Intent getExtras Intent bunde
double height =
Double.parseDouble(bunde.getString("KEY_HEIGHT"))/100;
double weight = Double.parseDouble(bunde.getString("KEY_WEIGHT"));

Activity bundle bundle


KEY_HEIGHT
KEY_WEIGHT

(Double)
private Button.OnClickListener backMain = new Button.OnClickListener()
{
public void onClick(View v)
{
// Close this Activity
Report.this.finish();
}
};

backMain Report Activity Bmi Activity

Bundle
Intent setClass Activity
setStringsetInt Intent
Intent this.getIntent().getData()
getData
getDatagetStringgetInt
Bundle

AndroidDebug

Android Log
LogCat

Log
src/com/demo/android/bmi/Bmi.java
(Log)
import android.util.Log
....
public class Bmi extends Activity {
private static final String TAG = "Bmi";
....
Log.d(TAG, "find Views");
Log.d(TAG, "set Listensers");

Log
LogCat
Log
Log.(, );


Log
1. Log.v (VERBOSE)
2. Log.d (DEBUG)
3. Log.i (INFO)
4. Log.w (WARN)
5. Log.e (ERROR)
Log.d() Log.w () Log.e ()
Log.d()

private static final String TAG = "Bmi";


....
Log.d(TAG, "find Views");

Log.(v,d,i,w,e) BMI
AppDemos

Log.d(TAG, "find Views");

Log.(v,d,i,w,e)

BMI try...catchLog

public class Bmi extends Activity {


private static final String TAG = "Bmi";
....
catch(Exception err)
{
Log.e(TAG, "error: " + err.toString());
Toast.makeText(Bmi.this, getString(R.string.input_error),

Toast.LENGTH_SHORT).show();
}

catch(Exception err)
Log.e(TAG, "error: " + err.toString());
....
}

Log.e..catch(Exception err)

TAG Activity

Log.e(this.toString(), "error: " + err.toString());

Log.e
Toast AlertDialog


(Debug Mode)

Dalvik Debug Monitor Service (DDMS)

(Debug Mode) ->Run->Debug History->BMI

"Open Perspective" "Other..."


Open Perspective
"Debug"Debug

Java

DebugLogCat
VDIWE Log (Verbose, Debug, Info, Warn, Error)
+-
(Log)
Bmi LogCat (Log
Filter)Bmi

(Log Filter)
LogCat+Log FilterLog Filter
by Log TagBmiFilter Nameok
LogCat
Filter NameBmi

ddms http://code.google.com/android/reference/ddms.html

debug http://code.google.com/android/intro/tutorial-extra-credit.html

trace view http://code.google.com/android/reference/traceview.html

LifeCycle

Activity Activity

Android
Android
Android
Android
...

(Multi-Task)( Windows
Mobile)

Android -- (Life Cycle)

Activity (process) Android


Android ( Dalvik ) Activity
Activity

Activity
OnCreate ()
Activity OnCreate
Android

Android Android
Activity onCreate
(Process)(process)

Activity OnCreate OnPause()


OnResume( ) Activity Activity
Activity
Activity
Activity

Activity
Android (VM) (Stack based)

Active ()

Paused ()

Stopped ()

Dead ()

Active ()
Active Activity Activity
Android Activity (Active)(Running)
Activity (Dead)(Stopped)(Pause)

Paused ()
Paused Activity
ToastAlertDialog Activity
ToastAlertDialog Activity Activity Paused
Activity

Stopped ()
Stopped Activity Activity
HomeStopped
Stopped ActivityNotification
Notification

Dead ()
Dead Activity
Activityfinish

Stopped Activity

Dalvik
1. Activity Service/Intent Receiver (
Activity)

2. Stopped Activity Activity


Activity " LRU ..."
3. Service
4. Activity/
5. Activity
4. Activity/

Activity
Activity
(Log)Log Activity

src/com/demo/android/bmi/Bmi.javaLog
public class Bmi extends Activity {
private static final String TAG = "Bmi";
public void onCreate()
{
super.onCreate(...);
Log.v(TAG,"onCreate");
}
public void onStart()
{
super.onStart();
Log.v(TAG,"onStart");
}
public void onResume()
{
super.onResume();
Log.v(TAG,"onResume");
}
public void onPause()
{
super.onPause();
Log.v(TAG,"onPause");
}

public void onStop()


{
super.onStop();
Log.v(TAG,"onStop");
}
public void onRestart()
{
super.onRestart();
Log.v(TAG,"onReStart");
}
public void onDestroy()
{
super.onDestroy();
Log.v(TAG,"onDestroy");
}
}

Activity LogLogCat
Activity

1. (Create/Destroy)
Activity CreateDestroy(Create)
(Destroy)
2. (Start/ReStart/Stop)
Activity Start Activity Activity
Stop Activity
Back Activity Restart Start
3. Resume/Pause

ToastAlertDialog Activity Pause

Activity Resume

Activity
Android

onCreate -> onStart -> onResume

Activity ActivityCreate Activity


Start Resume

Activity
onPause(1) -> onCreate(2) -> onStart(2) - onResume(2) -> onStop(1)

ActivityPause Activity
2 Activity 1

Activity
onPause(2) -> onRestart(1) -> onStart(1) -> onResume(1) -> onStop(2) ->
onDestroy(2)

Back Activity

onPause -> onStop -> onDestroy

finish Activity
(Freeze)(Pause)(Stop)(Destroy)

onCreate -> onStart -> onResume

Activity Activity onCreate

AndroidBack
Android Activity
BackDestroy
Activity
Activity Activity Create
Activity Activity
Activity Activity
Activity onPauseonStop
onCreate
Preference

Activity Cycle

Activity http://blog.csdn.net/sharetop/archive/2007/12/16/1941935.aspx

AndroidPreference

BMI

src/com/demo/android/bmi/Bmi.javaonCreateonPausePreference
()
public class Bmi extends Activity {
private static final String TAG = "Bmi";
public static final String PREF = "BMI_PREF";
public static final String PREF_HEIGHT = "BMI_Height";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
findViews();
restorePrefs();
setListensers();
}
// Restore preferences
private void restorePrefs()
{
SharedPreferences settings = getSharedPreferences(PREF, 0);
String pref_height = settings.getString(PREF_HEIGHT, "");
if(! "".equals(pref_height))
{
field_height.setText(pref_height);
field_weight.requestFocus();
}
}
......
@Override
protected void onPause(){
super.onPause();
// Save user preferences. use Editor object to make changes.
SharedPreferences settings = getSharedPreferences(PREF, 0);
settings.edit()
.putString(PREF_HEIGHT, field_height.getText().toString())
.commit();
}


// Restore preferences
private void restorePrefs()
{
SharedPreferences settings = getSharedPreferences(PREF, 0);
String pref_height = settings.getString(PREF_HEIGHT, "");
if(! "".equals(pref_height))
{
field_height.setText(pref_height);
field_weight.requestFocus();
}
}

onCreaterestorePrefsonCreate
restorePrefs
SharedPreferences settings = getSharedPreferences(PREF, 0);

SharedPreferencessettings
getSharedPreferencesBMI_PREFPREF

settingsgetSharedPreferences 0
settings
String pref_height = settings.getString(PREF_HEIGHT, "");

getXXX(SharedPreferences)
getStringPREF_HEIGHT
pref_heightPREF_HEIGHT
pref_height
if(! "".equals(pref_height))
{
pref_height.setText(pref_height);
...
}

pref_height field_height
field_weight.requestFocus();

BMI

field_weightrequestFocus

restorePrefs
Activity

@Override
protected void onPause(){
super.onPause();
// Save user preferences. use Editor object to make changes.
SharedPreferences settings = getSharedPreferences(PREF, 0);
settings.edit()
.putString(PREF_HEIGHT, field_height.getText().toString())
.commit();
}

Home
Back Activity
onPause
(Override)onPause
super.onPauseonPause
SharedPreferences settings = getSharedPreferences(PREF, 0);

SharedPreferencessettings
getSharedPreferencesBMI_PREFPREF

settingsgetSharedPreferences 0
settings
settings.edit()
.putString(PREF_HEIGHT, field_height.getText().toString())
.commit();

settings edit
putStringcommit
(SharedPreferences)editcommit
putXXX(SharedPreferences)
putString putInt
putFloat

putStringfield_height
PREF_HEIGHT

http://code.google.com/android/devel/data/preferences.html

AndroidMarket
(Market)

Android Market

1. Android Market
2.
3. Android
Android Market ()Android(Google)Android
AndroidMarketAndroid
Android Market
Android Market
Android
Android Market
Android Market

Android Market
Android Market

Android Market

Android Market 30% (


Google Checkout ) 70%

Android Market Developer 25


Android Market

Android Market

http://www.android.com/market/ Interested in having your application


in Android Market?learn moreAndroid

http://market.android.com/publish
Android MarketAndroid Google
(Google Checkout) $25 Android
Market
Google Google
Android Market
+8869xxxxxxxx
886009xxxxxxxx
+
Google CheckoutGoogle
CheckoutGoogle CheckoutI agree and I am
willing to associate my credit card and account registration above with the Android Market
Developer Distribution Agreement. Google
Checkout

Your Registration to the Android Market is approved!You can now upload and
publish software to the Android Market.()Android Market
Developer Console
Edit profile
Android Market Developer

Android Market
Upload Application

Android Market
Upload assetsApplication .apk file...
.apk
BMI/bin/BMI.apk
Market does not accept apks signed with the debug certificate. Create a new
certificate that is valid for at least 50 years.
Market requires that the certificate used to sign the apk be valid until at least
October 22, 2033. Create a new certificate.

apkdebug

AndroidManifest.xmlOverviewExporting the unsigned .apk

The apk is not properly signed.

icon
(permissions)
PublishAndroid Market

-
Android MarketDeveloper Console

PublishedSaved Draft
Android
cyket
aTrackDog AndroidStats

MIME Apache
.htaccess:
AddType application/vnd.android.package-archive apk

.apk
Android

Android

Android



Android Market 10/27
aBMI()gBMI()
Android Market()aBMI

aBMI() 732 active installs(


) 452 61% 25 3
3
gBMI() 602 active installs
193 ( 32%) 11 2
gBMI
aBMI Android

Android Market http://www.android.com/market/

Signing and Publishing Your Applications http://code.google.com/android/devel/signpublish.html

http://docs.sun.com/app/docs/doc/820-4607/ablqz?l=zh_TW&a=view

http://www.anddev.org/viewtopic.php?p=12252

http://keytool.sourceforge.net/

Android

AndAppStore http://andappstore.com/

MobiHand OnlyAndroid http://onlyandroid.mobihand.com/

SlideMe http://www.slideme.org/

Android

NeverEnd

BMI

BMI BMI
TextView

TextView
BMI Android Market
BMI BMI

BMI

Android Market Android (T-Mobile G1)

......

BMI !

res/value/string.xml
()
BMI ()

...... BMI
NBA ...()


BMI BMI
()
spinner ListArrayAdapter Android

Android
Activity Android

Android
3D

Android

BMI Android
Android

Android

http://code.google.com/p/androidbmi/wiki/IntroAndroid

4
Android

AndroidResource
Android

[http://www.android.com/ Android

Android Market

Android

RoadMap Android

Planet Android

Google Android

Android

Anddev

30 Days of Android Apps

Android

Android

Android

Android

Android

Android

java ()

IBM Android ()

SQL

2D Game ()

3D Game ()

DroidDraw GUI

positron

SD Card Image http://www.android123.com.cn/moniqi/48.html

Android http://andappstore.com/

Eclipse http://download.eclipse.org/technology/babel/updatesite/ganymede

ZXing - Barcode reader http://code.google.com/p/zxing/

Eyes Free - TTS http://code.google.com/p/eyes-free/

Radar http://code.google.com/p/apps-for-android/

XML-RPC - http://code.google.com/p/android-xmlrpc/

PhoneGap http://phonegap.com/

AR http://www.noritsuna.com/archives/2008/11/nyartoolkitandroid.html

http://code.google.com/p/android-scripting/

OCR http://www.bitquill.net/trac/wiki/Android/OCR

OCR http://code.google.com/p/mezzofanti/

Remote Control PC http://sourceforge.net/apps/trac/android-tesla/

BlueTooth http://code.google.com/p/android-bluetooth/

Chart http://code.google.com/p/chartdroid/

intent http://www.openintents.org/en/intentstable

DRM http://andappstore.com/AndroidApplications/licensing.jsp

Google Analytics http://code.google.com/intl/zhTW/apis/analytics/docs/tracking/mobileAppsTracking.html

Native C development http://blog.sina.com.cn/s/articlelist_1242184131_6_1.html

Gears

Gears Best Practice

http://sites.google.com/site/drpaulthomasandroidstuff/Home/voxel-fun/how-it-works

Android

Android

Tango

Nuvola

http://openclipart.org/

OPhone

http://www.oms-sdn.com/

AfterWard

Android
Google! Android
gasolin*
MVC
Android

Android

Android/AndroidAndroid
AppDemos

1.
2.
3.
4.
5.

Android
Android

Android (Emulator)
Android

AndroidAndroid XML
AndroidBMI

1.
2.

3.
4.
5. BMI

AndroidBMI

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

(Dialog)
(Uri)
(Menu)
Android
Activity
Activity
(Log)

(Preference)

aBMI
1.
2.
3. (Adapter)
4. (Spinner)
5. (keytools)

6. Android (Market)

SQLite Android Android


DummyNote
1. (ListActivity)

2. (SQLite)
3. (SQLiteOpenHelper)
4. (CRUD)
5. (ActivityForResult)

Android
/twTrainStationMyLocation
1. Google API Key
2. MapView
3. (KeyEvent)
4. GPS/
5. MyLocationOverlay
6. ItemizedOverlay

1.

2.

1. Android ()

2009/8 Android 1.5

2009/3/20 Android 1.1

GetSource

http://code.google.com/p/androidbmi/source/browse/

svn
svn svn
svn

BMI

BMI ()
svn checkout http://androidbmi.googlecode.com/svn/trunk/2nd/BMI BMI

aBMI

BMI ()
svn checkout http://androidbmi.googlecode.com/svn/trunk/2nd/aBMI aBMI

DummyNote

svn checkout http://androidbmi.googlecode.com/svn/trunk/2nd/DummyNote DummyNote

twTrainStation

svn checkout http://androidbmi.googlecode.com/svn/trunk/2nd/twTrainStation twTrainStation

MyLocation


svn checkout http://androidbmi.googlecode.com/svn/trunk/2nd/MyLocation MyLocation

You might also like