Professional Documents
Culture Documents
cmd
Android SDK tools
APK Android
SDK tools
adb install Snake.pak
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
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
Eclipse 3.4
Help->Software Updates
Available SoftwareAdd Site...()
"Location"(URL) http://dlssl.google.com/android/eclipse/site.xml "OK" Eclipse
Next() EclipseYes
Eclipse 3.3
Help->Software Updates->Find and Install
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..
ManageSDK
SDK
SDK
SDK
Window > Android SDK and AVD Manager
Android SDK/AVD
Installed Packages
Installed Packages
SDK SDK
SDK
Android
SDKDelete...
Available
Packages
SDK
OpenProject
Eclipse File->New->ProjectNew
Project Eclipse
AndroidAndroid ProjectNew Android Project
(WorkSpace)
gen
XML
SDK
Android SDK 1.5 SDK
SDK
NavigatorApiDemosproperties
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
Libraries:
* com.google.android.maps (maps.jar)
API for Google Maps
Skins: HVGA (default), HVGA-L, QVGA-P, HVGA-P, QVGA-L
--target 1
Androidlist avd
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
--skin QVGA
HVGA
?
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
ReadSource
Android
(BMI) Android
BMI
BMI
BMI
...: BMI
25
BMI 20
BMI Android
http://zh.wikipedia.org/wiki/
BMI
(Create new project in workspace)
""
(Use default location)
(Properties)
Project Name
Application
Name
Package
Name
(Package)JAVA (class)
Create
Activity
Activity Activity
Build Target
Min SDK
Version
SDK
Project Name
BMI
Application
Name
BMI
Package Name
com.demo.android.b
mi
Create Activity
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
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>
Android
main.xml XML
Android ViewGroup View ViewGroup View
Android
Android XML
HTML
<LinearLayout></LinearLayout>
" "(LinearLayout)LinearLayout
/""
LinearLayout
xmlns:android="http://schemas.android.com/apk/res/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
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
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;
}
}
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
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>
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
/>
<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
<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
@Override
10
super.onCreate(savedInstanceState);
11
setContentView(R.layout.main);
12
13 }
XML Java
Bmi.java
1 :
package com.demo.android.bmi;
package()
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
/
Activity / onCreate (Method)
bmi Activity bmionCreate
onCreate Activity
@
(decorator)@OverrideonCreate
bmi
onCreate
@Override
public void onCreate(Bundle savedInstanceState) {
}
onCreateonCreate
super.onCreate(savedInstanceState);savedInstanceState
onCreatesavedInstanceStatepublic void onCreate(Bundle
savedInstanceState)
setContentView(R.layout.main);
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
@Override
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
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
/
Activity / onCreate (Method)
bmi Activity bmionCreate
onCreate Activity
@
(decorator)@OverrideonCreate
bmi
onCreate
@Override
public void onCreate(Bundle savedInstanceState) {
}
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);
setContentView Activity
setContentViewR.layout.main
XML
res
BmiRefactor
BMI
(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
button.setOnClickListener(calcBMI);
9 }
Android
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
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));
AndroidDialog
BMI..
...BMI
openOptionsDialogcalcBMI
OnClickListener BMI
Android
Android (View)
Android
# #
Bmi.java
Bmi.java
1 private OnClickListener calcBMI = new OnClickListener()
2 {
3
.
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()
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)
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
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)
{
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>
8
9
10
11
gasolin\n
12
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
14
15
//go to url
16
17
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 http://androidbmi.googlecode.com/
UriUriUri
importEclispectrl-shift-O
Windowscmd-shift-OMacandroid.net.Uri
startActivity(intent);
startActivityAndroid (Intent)
(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
super.onCreateOptionsMenu(menu);
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, "");
menu.add(0, (identifer), 0, );
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">
<intent-filter>
10
11
</intent-filter>
12
</activity>
13
</application>
14
15 </manifest>
packagemanifest()
com.demo.android.bmi/
android:versionCode="1"
android:versionName="1.0"
android:versionCodeandroid:versionName(
)
android:versionName1.0
2.0
android:versionCode
manifestapplication( 1)
application
Activity
applicationandroid:icon
Android
android:icon="@drawable/icon"
res/drawable/icon 64x64
Pixel
applicationandroid:label Home
<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
2
activity Android servicereceiver
provider
Android
manifest http://developer.android.com/reference/android/R.styleable.html#AndroidManif
est
AndroidActivity
Activity
Activity (
)Activity
Activity MVC ControlActivity UI UI
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
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
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
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
@Override
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
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>
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
intent.setClass(Bmi.this, Report.class);
startActivity(intent);
10 };
(Intent)
intent.setClass(Bmi.this, Report.class);
intent startActivity
startActivity intent Android
Android ( Report Activity
activity ?) Report Activity
Activity
AndroidIntent
Activity
Intent Intent Activity
Intent
Report Activity
BMI ( Bmi
Activity)(Report Activity)
Intent
Intent
BMI
src/com/demo/android/bmi/Bmi.javaButton.OnClickListener
1
intent.setClass(Bmi.this, Report.class);
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 (Android
) Bmi Activity Report Activity
Activity
Bundle bundle = new Bundle();
...
intent.putExtras(bundle);
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
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
12 @Override
13
14
super.onCreate(savedInstanceState);
15
setContentView(R.layout.report);
16
findViews();
17
showResults();
18
setListensers();
19 }
20
21
28
29
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
40
41
42
Report.this.finish();
43
44 };
45
46 private void showResults() {
47
48
49
50
51
52
53
view_result.setText(getString(R.string.bmi_result) +nf.format(BMI));
54
55
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();
(Double)
private Button.OnClickListener backMain = new Button.OnClickListener()
{
public void onClick(View v)
{
// Close this Activity
Report.this.finish();
}
};
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()
Log.(v,d,i,w,e) BMI
AppDemos
Log.(v,d,i,w,e)
BMI try...catchLog
Toast.LENGTH_SHORT).show();
}
catch(Exception err)
Log.e(TAG, "error: " + err.toString());
....
}
Log.e..catch(Exception err)
TAG Activity
Log.e
Toast AlertDialog
(Debug Mode)
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
LifeCycle
Activity Activity
Android
Android
Android
Android
...
(Multi-Task)( Windows
Mobile)
Activity
OnCreate ()
Activity OnCreate
Android
Android Android
Activity onCreate
(Process)(process)
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)
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");
}
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
Activity Resume
Activity
Android
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
finish Activity
(Freeze)(Pause)(Stop)(Destroy)
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
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
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
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/
SlideMe http://www.slideme.org/
Android
NeverEnd
BMI
BMI BMI
TextView
TextView
BMI Android Market
BMI BMI
BMI
......
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
Android
Android
Android
Android
Android
Android
java ()
IBM Android ()
SQL
2D Game ()
3D Game ()
DroidDraw GUI
positron
Android http://andappstore.com/
Eclipse http://download.eclipse.org/technology/babel/updatesite/ganymede
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/
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
Gears
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)
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 ()
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
twTrainStation
MyLocation
svn checkout http://androidbmi.googlecode.com/svn/trunk/2nd/MyLocation MyLocation