Вы находитесь на странице: 1из 409

-

.
ANDROID 24 .
II
GOOGLE
.
. .
.
.
.

119021, , , , . 17, . 1; .: 788-0075(76)

. : editorial@readgroup.ru
ACT
27.04.2011. 70x100/ 16.
. . . 37,7. . .
3 . 5092.


144003, . , , . , . 25

JI.
94 Android 24 .
Google/ ., . .: , 2011. 464 .
( ).
ISBN 978-5-4252-0318-2
Android
, .
, , ,
, .
Java, Android, ,
.
Android 24 24 .
, ,
.

Android.
004.4
32.973.26-018.2

ISBN 978-5-4252-0318-2

, 2011

............................................................................................................
...............................................................................................................
I. Android ..........................................................
1. Android ..........................................................
Android ..................................................
Eclipse .............................................................................
...................................................
......................................................................................................
.................................................................................
.............................................................................................

11
14
19
19
16
21
28
36
36
37

2. Android ...
Android .
DDMS ...
Android ..
Android ..
..
.....
..

39
39
41
47
50
51
51
51

3. Android
Android .
...
..
...
.
...
..
.
..

53
53
56
57
61
62
63
64
65
65

4. ......................................
...
.
...
.
..
.
..
.
.

67
67
71
74
75
79
80
81
81
82

5. Android
Android


...

..
.
..

84
84
88
92
94
97
97
97
98

6.
Android


..
.
.

100
100
105
110
111
111
112

II. ..
7. -
- ..
.
.
..
.
..

113
113
113
114
119
123
124
124

8. ...
..
...
ListView ...
..
.
.....
.
9. ..

.

..
...
....
.
XML- ...
..
.

125
125
127
130
135
138
138
138
140
140
141
143
144
147
150
151
153
154

.
10.
..
.
..
...

SharedPreferences ...
..
.
.
11. ,
..
...
DatePickerDialog ..
...
..
.
..

154
156
156
159
162
170
173
173
174
175
175
178
181
188
188
189

12. ...
..

ViewSwitcher .
...
..
.
.

190
190
192
196
200
207
207
208

III.
Android ..
13. .
...
.
ImageButton ..
...
.
..
.
..

210
210
210
212
213
220
221
223
223
223

14. ...
,
...
,
..
.

225
225
229
231

, ..
...
..
.
..

236
238
241
242
242

15. .
...
.
..

..
..
.
...
..
.
..

244
244
246
248

16. ..

.

..
.
..

265
265
265
268
276
276
276

17. ...



.
..
.
.

278

18. ...
...
,
...
....
..
.
.

292
292

IV. Android- ...


19. ...
...

307
307
307

251
253
255
260
262
263
263

278
279
287
288
289
290

299
300
304
304
305

Android .
Android- ...
,
...
..
.
..

308
313

ac 20. ..
Android ..
..
.
.

319
319
329
329
330

21. Android ...


Android .
...
...
...
Android
...

.
..
.
..

332
332
333
337
339
340
341

22. Android- .
..

..
.
..

350
350
354
365
366
366

V. ..
23. ...
...
- ...
- ...
..
..
..
.
..

368
368
368
369
371
372
375
377
377
377

24. Android Market


Android Market ..
Android-

379
379
387

315
316
316
317

345
348
348
348

..
.
..

389
390
391

VI. ..
. Android .

Java Development Kit .
Eclipse
Android SDK ...
Android
Eclipse (ADT) ...
Android SDK

...

393

. Eclipse
..

import ..
...
..

...
...
..
.
...


..
.

393
393
393
394
395
397
398
399
402
402
402
403
403
404
405
405
406
406
407
408
408
409

(Lauren Darcey)
, ,
Android, iPhone, Blackberry, Palm Pre, BREW J2ME.

,
.
, .

, .
.
,
.
,
(), , , ,
.

.
(Shane Conder)
.
.
BREW, J2ME, Palm, Windows Mobile Android

.

, .
.
, ,
. ! , Amazon Web Services, Android,
iPhone, Google App Engine ., .

,

. ,
, ,
.


Android Wireless Application
Development, Developers Library,
developer.com, informlT.com, androidbook.blogspot.com.

, ,
.
-
. , ,
.

,
, , ,
.
Sams Publishing
( Pearson) . (Trina MacDonald), (Olivia Itasegio) (Betsy Harris).

(Jonathan Jackson) ,
. , ,
, ,
, ,
. (, Android .)
(Liz Reid),
(Guy Grayson), (Lenz) ( ), (Shoshi
Brown) ( ), (Badger) ( - ),
(Richard de Castongrene), (Asher Siddiqui),
(Anthony Shaffer), (Spencer Nassar) (Mary
Thompson) .


, ,
. ,
, ,
.
, ,
.
E-mail: baranov@readgroup.ru
:
: , 119121, . , 17, .


Android .
,
(SDK, Software Development Kit), ,
. (
, )
Android. :
, ( ARCHOS 5), e-books- (,
Barnes & Noble),
. ,
. ( ? . bit.ly/bGqmZp.)


Droid, Verizon.
Android
, (,
iPhone).
, .
, , ,
.
(( DMA, GSM) (,
, , , , , ).
.

. ; v
, .
,
.
. ,
Android: ,
, .
Android , .
, SDK,

,
Android.
, , ,
,
.
.

?
Android
,
. , ,
, ,
.
Java ( ,
, ..), Android, ,
. Java, ,
,
-
Java, , .
,
(, Eclipse, Java JDK Android SDK), (
USB) . ,
Android ,
. .

24 .
Android (LBS, Location-Based Service),
. ,
,
.
.

I. Android.
I Android,
Android SDK ,
Android. I

, Android,
,
, ,
.
II. .

,
.
-, , ,
.
, ,
.
.
III. Android.

III Android SDK,


. ,
, LBS,
.
IV. ,
Android.

IV , ,
.
.
V. .

V ,
Android Market.
VI. .
VI
Android Eclipse IDE,
, .

, Android SDK 2.1


) Android
SDK.
Android SDK ( ). , SDK, ,
.
< , Android SDK
, .
.
Android

! Android,
Android - Android Wireless Application Development,
Developer Library Addison-Wesley.

:
Windows 7 Mac OS X 10.6;
Eclipse Java IDE 3.5 (Galileo);
Eclipse JDT Web Tools Platform (WTP);
Sun Java SE (JDK) 6.18;
Android SDK 2.1 (
SDK);
Android (Androi SDK 1.6, 2.0.1
2.1).
,
:
...
, .

, .
!
,
, , .
, :

.
, *, ,
, .

.

, , .
, , ,
, . ,
, ,

.
,
.

, .
,
. ,
, , ,
.

.
, ,
=>. , ,
File => New Document ( >
).

I. ANDROID
1. ANDROID
, :
Android;
Eclipse;
Android;
.
Android , .

,
. ,
, . ,
,
. (
, Android Market.) Android
.
ANDROID
, Android , .
Google

2007 ,
( Google)
(, Open Handset
Alliance) .
, ,
, .

.
, RIM BlackBerry Apple iPhone,
,
.
Android
2008
Android -.
, Android .
Android SDK.
Android -Mobile G1,

2008 . 2009 ,
, Android, .
65 ,
. Android
,
Android. ,
Android ,
.
Android .
Google
. -
Android
Android (developer.android. com). - Android
SDK, ,
.
Google
Android
Android Market. . 1.1 Android,

.
. 1.1 Android


.
.
,

. Android .
, Android
'.
Android SDK
- Android developer.android.com. Eclipse,
,
(IDE, Integrated Development Environment)
Android. Android, Eclipse,
Android.
, ,
Android. Android Java,
,
Android SDK, , java.net. , ,
Android.
, .

ECLIPSE
Android- Hello, World,
. Eclipse. , ,
ADT (Android Developmeni
Tools). ADT
, , Android:

Android Project Wizard ( Android),


;
Android ;
Android SDK and AVD Manager ( Android SDK
);
DDMS Android;
Android LogCat;
Android
;
.

ANDROID SDK
Android .
Eclipse, Android SDK ADT Eclipse. , ,
USB Android,
.
.
Android
Android Project Wizard ( Android)
Android. , Eclipse
:
1. File => New => Android Project ( => =>
Android) Android
(

) Eclipse.

______________________________________________________________
Android, , ,
: File => New => Project ( =* => ),
Android => Android Project [Android =>
Android). Eclipse , , .

2.
3.

. , Droidl.
, . ,
Create new project in workspace (
). Use default location ( ),
.
, ... ____________________________________________________
,
Use default location ( ) .
4.
.
Android, ,
,
. Google (,
Google Maps), Google API,
. , Android 2.1
API Level 7.
5.
. .
Droid #1.
6.
,
Java.
com.androidbook.*, com.androidbook.droidl.
7.
Create Activity ( ).
Activity .
DroidActivity.
?
_________________________________________
Android. , ,
.
Droid #1 DroidActivity, .
3.
, . 1.2.
8.
, Min SDK Version (
SDK) . API
( Android 2.1 API
Level 7). ,
Android SDK.
.
9.

Next ().

10. , Android .
.
, (
, J u:
), Android
Project Wizard ( Android) Eclipse.
22.
11. Finish ().

. 1.2 Android Project Wizard ( Android) Eclipse

Android
File Explorer ( ) Eclipse
Android Droid 1. JAR- Android SDK, :

AndroidManifest.xml (
);
default.properties , Eclipse ADT. ;
/src , ;
/src/com.androidbook.droidl/DroidActivity.java DroidActivity.
Android;
/gen/com.androidbook.droid1/R.java
. ; ;
/assets ^ ;
/res , .
, , ,
, ;
/res/drawable () ;
/res/layout/main.xml , DroidActivity
;
/res/values/strings.xml .

, ... __________________________________________
______
Android Eclipse Android. Create
project from existing source ( ]
Create new project in workspace ( )
New Android Project ( Android), . 1.2. , ,
/samples Android SDK.
/android-*, * .
Create project from
existing sample ( ), . , -
, , .



Android. ,
, AndroidManifest.xml,
(. . 1.3).

. 1.3. Android Eclipse


ANDROID
Android
Android. .

Manifest () , . 1.3,
,
( ).

Application ()
, ,
, , (
DroidActivity )
, .

Permissions () , .
,

, Uses-Permission
(android, permission. READCONTACTS).

Instrumentation ()
, Android
SDK.

AndroidManifest.xml XML-
.

AndroidManifest.xml, ,
:
<?xml version="l.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android "
pakage="com.androidbook.droidl"
android: versionCode=" 1"
android:versionName="l. 0">
<appplication
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android: name=". DroidActivity"
android: l abel="@string/app_name">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android: name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk
android:minSdkVersion="7" />
</manifest>

!
, ... ___________________________________________________
Android, Android,
XML. , . XML- Android,
, (
Eclipse.

1.
2.
3.
4.


ANDROID
Android.
, android:debuggable true.
:
AndroidManifest.xml .
Application ().
debuggable true.
.
, AndroidManifest.xml, ,
application :
android:debuggable="true"

Android /res.
:

/drawable-ldpi, /drawable-hdpi, /drawable-mdpi



.
Project Explorer ( ), icon.png
. .
20.

/layout .
main.xml,
.

/values ,
, .
strings.xml,
, .

- , .
, XML-
.

1.
3.



main.xml, ,

TextView. .

0string/hello.
0string/hello
:
strings.xml.
2.
hello Hello
World, DroidActivity! .
Value () Hello, Dave.

4.

.
strings.xml XML,
, <resources> :
<?xml version="l.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello, Dave</string>
<string name="app_name">Droid #l</string>
</resources>

@string/hello.
0string/app_name, .
Android, , string/
app_name .
4.
.

Android
. ADT
Eclipse. :

AVD (Android Virtual Device, Android)


;

Android .

Eclipse Android
( ), .
Android
Android,
AVD. AVD ,
, .
, SD- , ,
. AVD Android 2.1
. AVD.
1. Android SDK and AVD Manager Eclipse,
Android (
) .
Window => Android SDK and AVD Manager
( => Android SDK AVD).
2. Virtual Devices ( ) .
AVD .

3. New (), AVD.


4. AVD Name (). AVD
VanillaAVD.
5. Target (
). , Android 2.1, Android
2.1 - API Level 7.
6. SD- . SD ,
, 1024 .
( 9 , , SD .)
7. . Skin ()
. HVGA,
. .
AVD , .
1.4.
8. Create AVD ( AVD) oe.

9.

. 1.4. AVD Eclipse


Android SDK AVD Manager.
_________________________________________________________________
Android SDK AVD Manager ,
SD-, AVD SD- .

. : (
) .
:

1.

Run => Debug Configurations ( =>


).

2.

Android Application ( Android), ,


.

3.

New_configuration.

4.

DroidDebug Name ().

5.

Browse () Droid 1.
6. Target ( )
AVD.
, ... ___________________________________________________
Target ( ) Manual ()
Automatic (),
.
,
. Android .
7. , Apply ().
Debug Configurations ( )
, . 1.5.
Android
. ,
Debug () Debug Configurations ( )
(
) Eclipse,
DroidDebug .

. 1.5. DroidDebug Eclipse


________________________________________________________________
DroidDebug
. .
.
, . , Menu ()
, (. . 1.6).

. 1.6. Android ( )

Eclipse , ,
. 1.7.
, .
I ext View.
.

. 1.7. Android Droid #1,


Android DDMS
( ),
Eclipse, ADT DDMS (Dalvik Debug Monitor
Service).
Eclipse.
DDMS (. . 1.8) , DDMS ( )
Eclipse. Project Explorer ( ), Java Eclipse.

DDMS Eclipse,
, Open Perspective ( )
(
Window => Open Perspective ( = )).
Open Perspective ( ) Other (),
. DDMS

. 1.8. DDMS Eclipse


DDMS
.
SMS .
. DDMS
, Android, 2.
LogCat DDMS,
.
.
Android
.
USB- .
, :
1. Run => Debug Configurations ( =t> ).
2. DroidDebug.

3. Target ( ) Deployment Target Selection


Mode ( ) !.
Manual (). ,
,
(
AVD) .
4. , Apply ().
5. Android USB-.
6. Eclipse Debug ().
(. 1.9),
.
.
AVI).
7. Android .
( )
.
, USB- ,
, .

. 1.9. Eclipse

Eclipse Android ', .


, , .
DDMS - fiymiyio ,
DDMS
.
ECLIPSE? ____________________________________________
Eclipse, .

! Android-. IIUK;I
Android J Eclipse.
. Android.
,
Android.

.
Android?
. Java ,
Android. , C++,
. , Java, C++
,
, Android NDK.
Android NDK
developer.android. com/sdk/ndk.
. AVD Android 1.1 (
), Android SDK?
. , ,
Android .
, ,
, .
. Android
, . -
?
. Android, ,
(, /res/values/strings.xml) XML-.
, XML . XML 4.



1.

(Open Handset Alliance)?
A.

B.

C.
. .
.
2.
SDK Android
. ?
3.
IDE Android
?
A.
B.
C.

Eclipse.
IntelliJ.
Emacs.

4.
Eclipse
. ?

1.
: .
,
.
2.

, . AVD.

3.
: A. Eclipse IDE
Android. IDE ,
ADT Eclipse.
4.
, . Eclipse ,
.

1.
developer.android.com .
Dev Guide ( )

. Community () Resources
().
Android.
2.

Droid #1. strings.xml
. main.xml,
TextView. android:text
TextView ,
. , ,
.
3.
Eclipse
Android, Android SDK. ,
.

2.
ANDROID
, :
Android;
DDMS;
Android;
Android (ADB, Android Debug Bridge);
Android.
Android
, .
Android
, .
- , ,
. Android
/tools Android SDK.
, .
Android .
ANDROID
Android ,
Android. HTML-
/docs Android SDK, - .
-
Android: http:// developer.android.com.

Android (. . 2.1):

. 2.1. Android (-)

SDK SDK, .
.
, -
,
SDK.

Dev Guide ( )
Android, ,
Android .

Reference ()
Android.

Blog () Android.
.
, Android
SDK .

Videos () , .
Android ,
Google .

Resources (). Community () I


Android.
, Google
.

Android SDK .
, -,
.
DDMS
Dalvik (DDMS, Dalvik Debug Monitor Service)
, Eclipse DDMS. DDMS

(. . 2.2).

. 2.2. DDMS Android

DDMS :
;
;
;
;
.
DDMS DDMS .
.
, ... ___________________________________________________
DDMS Eclipse.
Android SDK /tools.

Devices () DDMS (
, .
, .
, ,
com.androidbook.droid1, ( ), .
2.3.
, ( ). ,
, stop ( ).

. 2.3. DDMS Droid1


DDMS _______________________________________

DDMS
. Eclipse .
Eclipse, DDMS .
Android

File Explorer ( ) DDMS (. . 2.4).
Android
(

)(

).

. 2.4. File Explorer


( ) DDMS
( )
Delete.
.

DDMS , ,
SMS .
Emulator Control ( )
DDMS. DDMS
.

________________________________________________________________
. .


Emulator Control (
) DDMS (. . 2.5). ,
( ) .

. 2.5. () Emulator Control


( ) DDMS ()


1. ,
, :
2. DDMS , .
Emulator Control ( ) Telephony
Actions ( ) (,
5551212).
3. Voice ( ).
4. Call ().
5. . ,
Send () .

6. , End () Hang Up () Emulator Control


[ ] DDMS.
SMS
Emulator Control ( ) DDMS SMS (. . 2.6). SMS
, .

. 2.6. SMS () Emulator


Control ( ) DDMS ()

SMS
SMS , ,
:
1.
DDMS , SMS.
2.
Emulator Control ( )
(, 5551212) Telephony Actions ( ).
3.
SMS.
4.
SMS.
5.
Send (). SMS.


,
, , (.
. 2.7).

. 2.7. Screen Capture ( ) DDMS





. , :
1. DDMS, ( ),
.
2. ,
. , .
3. (
).
.
4. Save (),
, .


DDMS LogCat
Android. , LogCat
, .

3.
______________________
Eclipse .
(
).
ANDROID
Android ,
. ,
. Android Eclipse IDE ADT.

Android , .
:

. *
,
.

, , nfi
,

, , .

, o- . SMS
.

USB Bluetooth .
, Android .

, .
,
(, ! ).

.


:
1.
Eclipse Droid 1, 1.
2.
Ctrl+F11
Ctrl+F12,
.
3.
Alt+Enter,
. Alt+Enter ,

. -
: developer.android.com/guide/developing/tools, emulator.html.
Android
Android,
Android . 2.1
Android.
Android


. ,

SMS. ,

-. , -


. ,


,
, ,

(skins)
, , , ,
, .

Android SDK ,
( HVGA).
.
AVD.

SD-
SD-.
SD- , ,
-, .
SD- AVD,
1. SD- 9 .

ANDROID
, ,
, Android
SDK.
Android Hierarchy Viewer ( Android) , View Properties (
), .
Draw 9-Patch tool ( NinePatch) PNG-.
AIDL Compiler ( AIDL)
(IPC) Android.
mksdeard
SD- AVD .

ANDROID ECLIPSE

___________________

Android Eclipse, . ADT Eclipse


: , ,
Android.
, Eclipse, ,
, ADT,
. , , /tools Android SDK.
android Android AVD.
aapt [Android Asset Packaging Tool, Android)
Android -

.
ddms , DDMS Eclipse.
adb (Android Debug Bridge) .

Android SDK
Android. Android
DDMS,
Eclipse
. Android Android
.

,
Android .

: Android, Android SDK, developer.android.com ?
. . , SDK,
SDK, Android SDK. Android SDK, , - .
- ,
. -.
: ?
. . .
.
, .
: ?
. . Android ,
Android.
, .

. , ,
.
, .
, .

1.
A.
B.
C.
D.
E.
F.

DDMS?
.
.
Android.
.
SMS .
.

2.

Android ?

3.

Android?

4.
Android
. ?

1.
F . DDMS , .
2.
. Android ,
,
.
3.

SDK.
Android 1.1, Android 1.5. Android 1.6, Android 2.0, Android 2.0.1
Android 2.1. Android 1.1
Google API. AVD,
Android.
4.
, . , Android
, .
Android SDK and AVD Manager.

1.
Android .
. .
2.
Android .
. . ,
. ,
.

3.
Hierarchy Viewer ( )
Droidl, 1.
TextView.

3. ANDROID

, :
Android;
;
, ;
.

. Android : Context (),
Activity (), Intent (). , ,
,
Android.
Android. ,
.
ANDROID
Android ,
(activity).
.
, Chippys
Revenge.

Chippys Revenge . .
-
. .
,
Play, Scores Help.

Play , .

Scores (
), .

Help ,
, , , .
?
, .

_________________________________________________________________
, ,
. - Android ,
, Android.
. , , ,
, . , .


.
SplashActivity .
(
) ,
MenuActivity.
MenuActivity .
,
. , ,
onClick().
PlayActivity
.
, , ,
.
ScoresActivity ,
SplashActivity. ,
TextView
.
HelpActivity
ScoreActivity, ,
. TextView.

,
, .
ScoresActivity HelpActivity, .
, ,
Textview .

. 3.1 Chippys Revenge 0.0.1 -.

. 3.1. Android (Chippys Revenge)



, , Android, ,
, , .
, ,
. :

?
?

,
Android . .


.
,
.

getApplicationContext (), :
Context context = getApplicationContext();
Activity Context, this
.
! ____________________________________________________________
Activity - . ,

Android

:
android-developers.
blogspot.com/2009/01/avoiding-memory-leaks.html.
,
.

getResources ( )
. - URI,
R.java.
hello:
String greeting = getResources ().getString (R.string,hello);


getSharedPreferences(). SharedPreferences
, .
SharedPreferences
. ,
, .
GemePref,
:
SharedPreferences settings = getSharedPreferences(GamePrefs,
MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString(UserName, Spunky);
prefEditor.putBoolean(HasCredits, true);
prefEditor.commit();

,
SharedPreferences:
SharedPreferences settings = getSharedPreferences(GamePrefs,
MODE_PRIVATE);
String userName = settings.getString(UserName, Chippy Jr.
(Default));



. ,
:
Activity;
;
(,
);
, ,
.
, Activity, ,
.

Activity Android.
.
Chippys Revenge
Activity. ,
.

, :

;
. ;

.

Android
Android. Droid1, ,
DroidActivity .

, ... ___________________________________________________
Activity .
Android .
Chippys Revenge , SplashActivity
.


startActivity(). , .
, startActivity
() .
startActivity ()
:

startActivity(new Intent(getApplicationContext(),
MenuActivity.class));

MenuActivity .
.
MenuActivity ,
Android.

, .


.


Activity. startActivityForResult ().
onActivityResult ().
.

, . ,
.
Android , .
Android ,
, . ,
. , Android

.


Activity ,
, . . 3.1
.

onCreate()


onResume()
onPause()

onDestroy()

. .

setContextView()
.
,
. .

-,
.

UI-,
. . .
,
. UI- ,

Android .
onPause (), (,
) .
. 3.2 , .

,
.
getPreferences().
. , PlayActivity

, , .

, finish().
,
.

. 3.2.
Scores, Play Help
ScoresActivity, PlayActivity
HelpActivity.


Intent ,
Android. startActivity() , Android
Inentent .
.
Android .
, .
,
.
Android.

.
.
putExtra
() , .
Android,
(, m. androidbook.chippy.NameOfExtra).

, :
Intent intent=new Intent(getApplicationContext(),HelpActivi ty.class);
intent.putExtra("com.androidbook.chippy.LEVEL", 23) ;
startActivity(intent);

HelpActivity getintent ()
.
. :
Intent callinglntent=getlntent();
int helpLevel=callingIntent.getlntExtra("com.androidbook.
chippy.LEVEL",1);

,
startActivityForResult(),
onActivityResult() Intent.
.


,
. ,
.
. ,
:

- URL;
- ,
;
Maps ;
Google Street View ;
;
;
.

(ACTION_VIEW)
- URL:
Uri address = Uri.parse(http://www.perlgurl.org);
Intent surf = new Intent(Intent.ACTION_VIEW, address);
startActivity(surf);

.
-. URL,
-.
,
.
, Back
.

, .
, __________________________________________________

openintents.org :
www.openintents.org/en/intentstable. ,
Android, .

, .

.
.
,
.
, ,
.
. 3.2
.
Activity

Activity.showDialog()
Activity.onCreateDialog()
Activity.onPrepareDialog()

Activity.ondismissDialog()
Activity.removeDialog()





.
. showDialog()
. showDialog()

.
.
,
, . :
AlertDialog,
CharacterPickerDialog,
rogressDialog TimePickerDialog.

DatePickerDialog,


D i a l o g . s e t C o n t e n t V i e w () XML- .
Dialog.findViewById
().


Android android.util.Log.
( )
. . 3.3
Log.

3.3

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

! ____________________________________________________________
Log
.
.
Log , .
Android
,
.
, TAG :
private static final String TAG="MyApp";

Log, .
:
Log.i(TAG, In onCreate() callback method);

, __________________________________________________
LogCat, Eclipse,
. .

, Android
: Context, Activity Intent.
Android .
.
() .
, Android
, ,
.
.
Android
Android, ,
Android .

. ,
?
. ( )
. ,

. ,
, .
. Android?
.
. - Submit
(), Clear (), Cancel (),
.
.
,
.


1.
A.
B.
C.

?
.
-.
.

2.
Android .
?

3.

Android?
A.
Context context=(Context) this;
B.
Context context=getAndroidObject (CONTEXT) ;
C.
Context context=getApplicationContext () ;
4.

android.util.Log . ?

1.
: . .
2.
, .
SharedPreferences.
3.
: . ,
. , , ,
.
4.
. Log : ,
, , .

Droid 1. onCreate ()
Log. i ().
.
Activity onCreate(),
onStart(), onRestartf), onResume (), onPause (), onStop () onDestroy ().
.
.
, . .
, .

4.
, :
;
;
;
;
;
.
Android
, .
Android . , Android,
.

: .
Android
. ,
Android, Android SDK.
.
,
XML-
.

Android, /res. , ,
.
Android.

, ,
.
:
,
.
.
.
.
Android (. . 4.1),
.
Android .
:

, ;
;
;
.

,
XML-. , /res/drawable, /res/layout /res/values,
Android,
.
, /res, :
.
, ,
.
( XML- name) .
, ... ___________________________________________________
,
. , mypic.jpg,
/drawable, @drawable/mypic.
.

. 4.1. Android

Android.

/res
. ,
.

! ____________________________________________________________
, (. ,
, ) Eclipse, R.java . -
, Problems () Eclipse.

R.iava. ,
Resources getResources ()
, , .
, hello, strings.xml,
:
String greeting=getResources {) .getString (R. string.hello) ;

.
,
, :
@_/_
, , , :
@string/hello
,
.

,
Android. ,
, . ,
Android (android.R.string)
, OK, Cancel (), Yes (), No (). Cut (),
Copy ( ), Paste ().
_________________________________________________________________


, , .
android.
.

, android.R.string .
ok, ,

getSystem()

Resources,
Resource,
getString(), , :
String confirm = Resources.getSystem().
getString(android.R.string.ok);
,
, :
@android:[_]/[_]
, ok,
:
@android:string/ok

, , , XML /res/values . XML-,
/.
.
_________________________________________________________________
,
Eclipse, XMl- .

,
- .
<string> /res/values/ strings.xml.
:
<?xml version=1.0 encoding=utf-8?>
<resources>
<string name=app_name>Name this App</string>
<string name=hello>Hello</string>
</resources>
. . 4.1
.
________________________________________________________________
, ( ),
.

4.1


,
,
\, \
\\

,
,
,

.
getstring();
String greeting = getResources().getString(R.string.hello);


.

<color>

/res/values/co(ors.xml. XML- ,
.

_____________________________________________________________________
XML-, , ,
File => New => Android XML File ( => =>
XML- Android). ,
Android, .
:
<?xml version=1.0 encoding=utf-8?>
<resources>
<color name=background_color>#006400</color>
<color name=app_text_color>#FFE4C4</color>
</resources>
Android 12- 24-
RGB. . 4.2 , Android.
4.2
, Android

#RGB
#ARGB

12-
12-

#00F ()
#800F (,

#RRGGBB
#AARRGGBB

24-
24-


app_text_color getColor():

50%)
#FF00FF ()
#80FF00FF (,
50%)

int textColor = getResources().getColor(R.color.app_text_color);

, ... __________________________________________________
. , html-colorcodes.info ,
.

,
Textview, .
<dimen> /res/values/ dimens.xml. XML-
.
:
<?xml version=1.0 encoding=utf-8?>
<resources>
<dimen name=thumbDim>100px</dimen>
</resources>

.
. 4.3 , Android.
4.3
, Android


px
in

(
)


160 dpi

dp

(
)

sp

mm
pt


getDimension():
float thumbnailDim = getResources().getDimension(R.dimen.thumbDim);


, , /res/ drawable
. ,
.
_________________________________________________________________
/res/ drawable
Project Explorer ( ) Eclipse. ,
,
.

, , , PNG JPG-.
, .
. 4.4, Android
.
4.4
, Android

PNG
PNG NinePatch
JPEG
GIF


( )

( )
( )

.png
.9.png
.jpg
.gif


BitmapDrawable.
, /res/drawable/logo.png,
getDrawable():
BitmapDrawable logoBitmap =
(BitmapDrawable)getResources().getDrawable(R.drawable.logo);

.

, ImageView.
logo.png ImageView
LogolmageView, :
ImageView logoView = (ImageView)findViewById(R.id.LogoImageView);
logoView.setImageResource(R.drawable.logo);


XML-
Drawable, ShapeDrawable.
ShapeDrawable ,
.
Android, android.graphics.drawable.

Android
XML-, .
/res/layout.
, , .
, __________________________________________________

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

android:text=@string/hello />
</LinearLayout>

, main.xml ,
Android.
.
LinearLayout,
TextView.
main.xml
@string/hello, strings.xml.

, ... ___________________________________________________
,
. XML- .
.
Eclipse
. XML-
.

Eclipse
(. . 4.2). /res/ layout/main.xml Eclipse,
Layout (),
, .
Outline ().
Properties ().

. 4.2. Eclipse
,
.
.
XML- .

. -
.
,
, Eclipse.
XML
XML- .
/res/layout/main.xml main.xml, XML .
________________________________________________________________
,
XML-. XML ,
.
Eclipse.

Eclipse:
1.
Droid 1, .

2.
/res/layout/main.xml
Eclipse .
3.
Layout (),
.
4.
Outline (). XML . LinearLayout,
TextView.
5.
TextView Outline ().
.
6.
Properties ().
TextView,
. Text ,
@string/hello.
7.
@string/hello Properties
(), . , ,
.
, (,
@string/app_name) , ,
.
8.
main.xml XML-, , ,
, .
Eclipse.
, ImageView TextView .
.

, ... ___________________________________________________
Outline ()
Properties (). Properties ()
Java, , Project Explorer ( ) ,
Properties () (
), Outline () ( , Properties ()).


LayoutInflater, View,
:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.main, null);

,
, TextView main.xml. ,
, (Button, ImageView
TextView), View.
, Droidl,
TextView. TextView
name.
View , id
TextView . @+id/TextView01.
TextView TextView01,
:
TextView txt = (TextView)findViewById(R.id.TextView01);


, , Android
. .
.
XML-
XML Android. XML-
,
.
XML- .
XML (. 4.5) Android.
4.5
XML

android.sax*
android.util.Xml.*
org.xml.sax.*
javax.xml.*
org.w3c.dom
org.xmlpull.*

SAX
XML, XMLPullParser
SAX (. www.saxproject.org)
SAX DOM 2-
DOM, 2-
XmlPullParser XMLSerializer (. www.xmlpull.org)

XML- /res/xml/default_values.xml getXml():


XmlResourceParser defaultDataConfig = getResources().getXml(R.xml.default_values);


Android
(raw files). - ,
.
/res/raw.

( , ).
- Android,

, .
,
. , ,
Android,
.

openRawResource(). ,
inputstream /res/raw/file1.txt:
InputStream iFile = getResources().openRawResource(R.raw.file1);

________________________________________________________________
, ,
file1.txt file1.dat .

, ... ___________________________________________________
, ,
.
Android /assets.
, /res. , ,
, , .
, ,
getResources(). /assets
AssetManager.

,
. , ,

. ,
. .
:
;
;
;
;
;
.
,
Android.

Android ,
. Eclipse
, XML-
.
. ,
XML-,
. XML- .
, ,
. ,
, .


. ,
?
. . Android
. ,
,
.
Android /toots/lib/res/default.
. ,
XML-?
. - . . ,
,
.
.
. XML- ?

. , SAX
XML ( XMLPullParser),
. ,
.
. ?
.
getResources().
,
.
getAssets().


1.
A.
B.
C.

?
12- .
24- .
64- .

2.
.
?
3.
Android?
A.
JPG.
B.
PNG.
C.
GIF.
D.
PNG NinePatch-.
4.

. ?

5.

. ?

: . 12-, 24-
.
.
.

: . , PNG-, NinePatch, .
JPG , GIF .

, . , ,
.
, . ,
.

#00ff00 Droid 1.
TextColor TextView
.
.
22pt Droidl.
textSize TextView
.
.
Droidl.
ImageView src,
.
.
Droidl.
openRawResource() InputStream .
Log.v().
.

5. ANDROID
, :
Android;
;
;
;
.
Android ,
. ,
, ,
. Android ,
, .
ANDROID
Android AndroidManifest.xml
Android. Android :
;
;
;
;
,
-.
, ... __________________________________________________
Eclipse ADT, Android AndroidManifest.xml
.
, Android Eclipse
.

Eclipse
, :
Manifest ();
Application ();
Permissions ();
Instrumentation ();
AndroidManifest.xml.
Manifest ()

Manifest () (. . 5.1) ,
,
Android SDK. .

. 5.1. Manifest () Eclipse


Application ()
Application () (. . 5.2) , ,
, , -,
, -.

. 5.2. Application () Eclipse


Permissions ()
Permissions () (. . 5.3) ,
. ,
.

. 5.3. Permissions () Eclipse


! ____________________________________________________________
Permission (! Application -) Permissions (). Permissions ()
,
API. Permission () ,
I .
Instrumentation ()
Instrumentation () (. . 5.4)
. Name ()
instrumentation , Target Package
( ) , <application>
.
.

. 5.4. Instrumentation () Eclipse

AndroidManifest.xml
Android XML-.
XML AndroidManifest.xml (. . 5.5).

. 5.5. AndroidManifest.xml Eclipse


. 5.5 Droidl, .
XML- .
, <manifest>,
.
<application>, , DroidActivity -.
<uses-sdk>, .
.


<manifest> <application>.
<manifest> Android
package, :
<manifest
xmlns:android=http://schemas.android.com/apk/res/android
package=com.androidbook.droid1
android:versionCode=1
android:versionName=1.0>

_______________________________________________________________________
Android,
. Android
.

<uses-library>
Android.
Application () . :
<uses-library
android:name=com.company.sharedutilities />


:
;
.
<manifest> :
.

,
.
. android:versionName
<manifest> .
, Droid 1 1.0.
. , .

Android .
android:versionCode <manifest> ,
Android
. android:versionCode
1.
, .
.
________________________________________________________________
. .
.

Android SDK
Android SDK. <uses-sdk>
Android SDK
. android:minSdkVersion
SDK . . 5.1
.
5.1
Android SDK
Android sdk

Android 1.0 SDK 1

Android 1.1 SDK 2

Android 1.5 SDK 3

Android 1.6 SDK 4

Android 2.0 SDK 5

Android 2.0.1 SDK 6

Android 2.1 SDK 7

, Droid1 Android SDK 2.1:


<uses-sdk
android:minSdkVersion=7 />

! ____________________________________________________________
Android SDK
.
API Level ( API), Android SDK and AVD Manager.

android:label <application> ,
. ,
:
<application
android:label=My application name>

, android: label .
Droid 1 :
<application
android:label=@string/app_name>

, app_name
strings.xml.

android:icon <application> ,
. Droidl
:
<application
android:icon=@drawable/icon>


, android:
icon <application>, .

: (1dpi).
(hdpi) (mdpi) (
Android 2.2 xhdpi, .. .
. .). Android
, .
20.

.
, :
1.
48 48 ,
.
.
2.
PNG myicon.png.
3.
/res/drawable-mdpi .
4.
1-3, 72x72
/res/drawable-hdpi.
.
5.
1-3 36x36
/res/drawable-ldpi. .
6.
android:icon,
@drawable/myicon.
,
( ), .
,
. ,
.

android:description <application> ,
.
:

<application
android:label=My application name
android:description=@string/app_desc>

Android
.

android:debuggable <application>
, ,
Eclipse, ,
true.
false.

Application ()
, . ,
- .
,
Android-
.
Application ()
Application Nodes ( ),
, .

, Android .

Android.
.
.4- ,
.
.
() , .

Application Nodes (
) Application ().
<activity> XML- . ,
XML DroidActivity:
<activity
android:name=.DroidActivity />

.

Droidl, :
1.
Droidl Eclipse.
2.
/com.androidbook.droidl /src
New (), Class ().
New Java Class ( Java).
3.
DroidActivity2.
4.
Browse () Superclass () android.app.Activity.
5.
Finish ().
.
6.
main.xml /res/layout second.xml.
, , . ,
. .
7.
DroidActivity2.
Source (), Override/Implement Methods (/ ).
8.
onCreate(Bundle).
.
9.
onCreate()

setContentview(R.layout.second). .
10.
Android Application
() .
11.
Application Nodes ( ) Application
() Add () Activity (). Atributes for Activity (
).
12.
Browse () Name ()
. DroidActivity2.
13.
. AndroidManifest.xml,
XML.
, DroidActivity2, .
! ____________________________________________________________
,
. , ,
Create a new element at the top level, in
Application ( , ) .

-
. -

<intent-filter> MAIN
LAUNCHER. Droidl Android
DroidActivity :
<activity
android:name=.DroidActivity
android:label=@string/app_name>
<intent-filter>
<action
android:name=android.intent.action.MAIN />
<category
android:name=android.intent.category.LAUNCHER />
</intent-filter>
</activity>

, <intent-filter> Android
DroidActivity.

Android Linux,
, Android.
Android
Linux (. . 5.6).

. 5.6. Android

,
,
Android. ,

- .
_________________________________________________________________
, , Linux.
Android
. ,
, , ,
<uses- permission> Android.
Permissions ()
Android.

,

:
7.
Droidl Eclipse.
8.
Android Permissions ()
.
9.
Add () Uses Permission ( ). Name () .
10. android.permission. CAMERA.
11. . AndroidManifest.xml,
XML.
, .
.
. 5.2
Android.
5.2
, Android

android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION

android.permission.READ_CONTACTS
android.permission.WRITE_CONTACTS

android.permission.READ_CALENDAR
android.permission.WRITE_CALENDAR

android.permission.SET_ORIENTATION
android.permission.SET_TIME_ZONE
android.permission.SET_WALLPAPER

android.permission.CALL_PHONE
android.permission.CALL_PRIVILEGED

android.permission.READ_SMS
android.permission.RECEIVE_MMS
android.permission.RECEIVE_SMS
android.permission.RECEIVE_WAP_PUSH
android.permission.SEND_SMS
android.permission.WRITE_SMS

android.permission.INTERNET

android.permission.RECORD_AUDIO
android.permission.MODIFY_AUDIO_SETTINGS

android.permission.ACCESS_NETWORK_STATE
android.permission.CHANGE_NETWORK_STATE

WiFi

android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_STATE

android.permission.BLUETOOTH
android.permission.CAMERA
android.permission.FLASHLIGHT
android.permission.VIBRATE
android.permission.BATTERY_STATS

android.permission.GET_ACCOUNTS
android.permission.MANAGE_ACCOUNTS

android.permission.READ_SYNC_SETTINGS
android.permission.READ_SYNC_STATS
android.permission.WRITE_SYNC_SETTINGS

, Android,
android.Manifest.permission.
! ____________________________________________________________
Android.
.
_________________________________________________________________
.
, .

, , Android
. ,
,
.
, - ,
.

Android (AndroidManifest.xml)
. . Android
XML- ,
. Android .
Eclipse Android.

. ?

. . android: label
,
. .
. Android ,
?
. ,
. Android
, , ,
.
. Activity , . ?
. , Android.
.
. Android ,
Eclipse?
.
. .
. .
?
. ,
. ,
Android , ,
,
.
.


1.
Android .
?
2.
android:versionCode , android:
versionName. ?
3.

?
A.
android.permission.USE_CAMERA.
B.
android.permission.CAMERA.
C.
android.permission.hardware.CAMERA.

4.
,
Android. ?

1.

. Android Android.
, .

2.

android:versionCode,
, .
android:versionName.

3.

: .
android.permission.CAMERA.

4.

. , ,
,
.

1.

Droidl.

2.

Activity. -
. , -
Android, , .

3.

.
, XML.
XML.

6.
, :
Android;
;
.
, .
Android ,
.
.
ANDROID

, .
,
, :
- ?
- ?
- ?
- ?
, ,
. Been There, Done That!.

,
. :
;
;
;
.

. ,
.
, Been There, Done That!*
:
-;
;
;
;
;
.
.

,
Been There, Done That!
Activity. . 6.1 ,
.

. 6.1.
Been Three, Done That!
, Activity
. QuizActivity. ,
, Been There, Done That!:
QuizActivity, android, . Activity, .

.
QuizSplashActivily, QuizActivity,
-.
QuizMcnuActivity, QuizActivity,
.
QuizHelpActivity, QuizActivity,
.
QuizScoresActivity, QuizActivity,
.
QuizSettingsActivity, QuizActivity,
.
QuizGameActivity, QuizActivity,
.




Been There, Done That!.

- Been There, Done That!


. QuizSplashActivity.
:


;
. 6.2. -
Been There, Done That!.

.
. 6.2 -.

.

. QuizMenuActivity.
:
;
Play Game,
Settings, Scores Help.
. 6.3 .

,
QuizHelpActivity.
:

. 6.3.
Been There, Done That!

. 6.4.
Been There, Done That!


;
.
. 6.4 -.

,
.
QuizScoresActivity.
:
;
,
.
. 6.5 .

,
.
QuizSettingsActivity. :

. 6.5.
Been There, Done That!

. 6.6.
Been There, Done That!

;
.
. 6.6 .

.
QuizGameActivity.
:
/;
, ;
.
. 6.7 .

. 6.7.
Been There, Done That!

, ,
Been There, Done That! , .
:
1.
Android Eclipse/
2.
, .
3.
.
4.

5.
.
________________________________________________________________
//
06.
Android
Android
Eclipse.
:
Project name ( ): TriviaQuiz.
Build target ( ): Android 2.1.
Application name ( ): Been There. Done That!.
Package name ( ): com.androidbook.triviaquiz.
Create activity ( ): QuizSplashActivity.
Android. ,
.

, ... ___________________________________________________
( ),
, . ,
com.androidbook.triviaquiz6. , .

Been There, Done That!
. ,
.
.

strings.xml.
hello .
, help Help Screen.
strings.xml :
<?xml version=1.0 encoding=utf-8?>
<resources>
<string
name=app_name>Been There, Done That!</string>
<string
name=help>Help Screen</string>
<string
name=menu>Main Menu Screen</string>
<string
name=splash>Splash Screen</string>
<string
name=settings>Settings Screen</string>
<string
name=game>Game Screen</string>
<string
name=scores>Scores Screen</string>
</resources>


.
main.xml splash.xml. splash.xml
: game.xml, help.xml, menu.xml, scores.xml settings.xml.
.
, TextView
@string/hello, .
,
TextView. , game.xml
@string/hello @string/game.
.


. , quizicon.png 48x48
/drawable. icon.png,
.
, ... __________________________________________________
/drawable. , Android , ,
[/drawabte-ldpi, /drawable-mdpi /drawable-hdpi).
quizicon.png , , .


Activity,

QuizSplashActivity.java.

QuizActivity. :
package com.androidbook.triviaquiz6;
import android.app.Activity;
public class QuizActivity extends Activity {
public static final String GAME_PREFERENCES = GamePrefs;
}

, QuizSplashActivity,
QuizActivity Activity .

QuizSplashActivity
: QuizMenuActivity, QuizHelpActivity, QuizScoresActivity,
QuizSettingsActivity QuizGameActivity.
, Eclipse
. ,
com.androidbook.triviaquiz New Class ( ).
.
_________________________________________________________________
, Eclipse,
.
, Java- .
- , main.xml,
. ,

, . ,
QuizHelpActivity setContentView(),
, , :
setContentView(R.layout.help);

,
setContentView() .
ANDROID
Android. , ,
@drawable/quizicon . -,
, .
, true Debuggable
, QuizSplashActivity
.
_________________________________________________________________
. ,
(/drawableIdpi, /drawable-mdpi /drawable-hdpi),
. , . ,
Android , .

Been There, Done That!
.

Android
(android.content.SharedPreferences).

.
QuizActivity , :
public static final String GAME_PREFERENCES = GamePrefs;

, ... ___________________________________________________
, ,
,
, , .
, -
:

1.
getSharedPreferences()
SharedPreferences.
2.
SharedPreferences.Editor,
.
3.
.
4.
commit () .

/.
:
;
;
;
;
.
, , ,
SharedPreferences, Editor
.
:
import android.content.SharedPreferences;
// ...
SharedPreferences settings =
getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString(UserName, JaneDoe);
prefEditor.putInt(UserAge, 22);
prefEditor.commit();

clear() remove ().



, ,
. ,
:
SharedPreferences settings =
getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
if (settings.contains(UserName) == true) {
// We have a user name
String user = Settings.getString(UserName, Default);
}

SharedPreferences
,
.
, Been Three, Done That!
,

. ,
.

.
Eclipse.

Eclipse . ,
AVD Google API (API Level 7).
AVD ( ), Android SDK and AVD
Manager Eclipse. , AVD
, , .


Been There, Done That! Android.
, Eclipse Run
() .

. 6.8. Been Three, Done That!



. 6.8. .
, ,
OuizSplashActivity.
TextView, , -.
,

.
.
.

Been There, Done That! ,
Android
.
Application Manager ( )
. ,
Menu (), Settings=>Applications=>
Manage applications (=>=> )
Been There, Done That! .
,
, ..
Dev Tools ( ) -
. ,
, Dev Tools ( )
Package Browser ( ). com.androidbook.triviaquiz.

.
, DDMS. .
com.androidbook.triviaquiz

Android.

QuizSplashActivity.

,
.
. Android,
.
.

.
?

. Activity,
.
. ?

. , ,
.
getSharedPreferences().
getPreferences().
.
Android Eclipse?
. AVD, .
Eclipse
.


1.
Been There, Done That! . ?
2.
?
A.
, , ,
.
B.
, .
C.
, Java.
3.
(,
QuizActivity) Android. ?

1.
, . Been There, Done That!
.
, .
.
2.
: . ,
, , .
3.
.
Android.

1.
lastLaunch.
QuizSplashActivity
onCreate().
Log.i().

2.
QuizSettingsActivity
getPreferences() getSharedPreferences().
II.
7.
-
, :
-;
-;
.
- Been
There, Done That!*. -, ,
View Android
splash.xml.
- . , - . ,
- ,
.
-
Been There, Done That!, -,
.
6, . ,
( ), ,
,
. . 7.1 -.
- ,
,
. -, ,

, . , , .

. 7.1. -
Been There, Done That!

, , - ,
-. ,
/res/layout/sptash.xml QuizSplashActivity.
, TextView
( , -),
, .

. ,
. -
:
TextView .
ImageView .
View . -
Layout. , - LinearLayout
-
.
- LinearLayout Layout. -
, .
, LinearLayout. . 7.1 Layout.
7.1
- Layout


- Layout
LinearLayout

RelativeLayout

FrameLayout

TableLayout

- -
- - -
-
,
. - View
-
,

/
(
)
,
-
-
View
-
View ,

gravity

TableRow

_________________________________________________________________
Android 1.5 SDK - AbsoluteLayout, X/Y , , . - , - X/Y
, . WebView SDK.
- - View

, . ,
-

android:layout_width

android:layout_height, .
, ,
fill_parent wrap_content.
fill_parent, -
-, wrap_content
- - View,
- View. ,
-,

, .
- TableLayout,
- ImageView, .

. 7.2. - -
Been There, Done That!
- - LinearLayout
,
: TextView, -
TableLayout TableRow,
ImageView, TextView. . 7.2 -.

, - - , , ,
.
/res/drawable (
): splashl.png, splash2.png, splash3.png splashl.png.
- Table Layout
-.

_________________________________________________________________
//
07

/res/values/strings.xml: (Been There), (Done That!) (
). splash ,
.
/res/values/colors.xml,
: (), (-) ,
(-).
________________________________________________________________
, . Android @android:color/black.
,
/res/values/dimens.xml. :
(24pt),
(5pt)
(3pt).
, ,
splash.xml.
-
splash.xml,
, Eclipse, .
, .
1.
- LinearLayout
background @android:color/black,
orientation vertical.
- LinearLayout.
- View
Outline ().
2.
TextView TextViewTopTitie. layout_width fill_parent,
layout_height wrap_content. text
, textColor ,
textSize .

3.
oae
ee-oeep
TableLayout
o
aae
TabeLayout01. layout_width
fill_parent, layout_height wrap_content.
stretchColumns *, , .
4.
TableRow -
TableLayout. ImageView TableRow.

ImageView

src

@drawable/splashl, splas1.png.
ImageView src ,
splash2.png.
5.
4, TableRow.
ImageView splash3.png splash4. png.
6.
2 TextView TextViewBottomTitle -
LinearLayout. layout_width fill__parent,
layout_height wrap_content. text
, textColor ,
textSize .
7.
TextView
TextViewBottomVersion. layout_width
layout_height

fill_parent.

text
, textColor ,
textSize .
background

(-)

lineSpacingExtra ,
.
8.
layout_gravity gravity
,
Eclipse.
splash.xml Been There, Done
That! Android. - , .
7.3.
, -
. , -
.

. 7.3. -
Been There, Done That!


- .
Android :
GIF-. ,
.
. Android SDK
, GIF,
(
AnimationDrawable).
.
,
- -.
OpenGL ES. OpenGL ES Android
, ,
.
. Android
- , ,
. ,
, .
, -.

,
/res/anim.
XML-,
- View.
, ... __________________________________________________
,
android.R.anim.

XML-
/res/anim: fade_in.xml, fade_in2.xml custom_anim.xml.
, fade_in.xml,
(-) 0 ()
1 () 2500 , 2,5 . Eclipse
. XML- fade_in.xml :
<?xml version=1.0 encoding=utf-8 ?>
<set
xmlns:android=http://schemas.android.com/apk/res/android
android:shareInterpolator=false>
<alpha
android:fromAlpha=0.0
android:toAlpha=1.0
android:duration=2500>
</alpha>
</set>

TextView
.
fade_in2.xml. , fade_in.xml,
, startOffset 2500 .
, 5 : ,
2,5 , - 2,5
. ,
fade_in2.xml .
, TableLayout.

:
,
-, View .
custom_anim.xml :
<?xml version=1.0 encoding=utf-8 ?>

<set

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

android:shareInterpolator=false>
<rotate
android:fromDegrees=0
android:toDegrees=360
android:pivotX=50%
android:pivotY=50%
android:duration=2000 />
<alpha
android:fromAlpha=0.0
android:toAlpha=1.0
android:duration=2000>
</alpha>
<scale
android:pivotX=50%
android:pivotY=50%
android:fromXScale=.1
android:fromYScale=.1
android:toXScale=1.0
android:toYScale=1.0
android:duration=2000 />
</set>

, 360 2 ,
-. ;
2 . , 10% 100%
.
2 .
,
-.
-
.
, ,
- .
,
.
: fade_in.xml - TextView TextviewTopTitle,
. , , -
TextView onCreate() QuizSplashActivity,
Animation startAnimation()
- TextView:
TextView logo1 = (TextView) findViewById(R.id.TextViewTopTitle);
Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo1.startAnimation(fade1);

,
onPause() ,

clearAnimation(). , onPause()
:
@Override
protected void onPause() {
super.onPause();
// Stop the animation
TextView logo1 = (TextView) findViewById(R.id.TextViewTopTitle);
logo1.clearAnimation();
TextView logo2 = (TextView)
findViewById(R.id.TextViewBottomTitle);
logo2.clearAnimation();
// ... stop other animations
}

-
View,
View, (, - TableLayout
TableRow), LayoutAnimationController.
- View ,
,
LayoutAnimationController,
setLayoutAnimation() -. ,
custum_anim,
LayoutAnimationController
TableRow - TableLayout:
Animation spinin = AnimationUtils.loadAnimation(this,
R.anim.custom_anim);
LayoutAnimationController controller =
new LayoutAnimationController(spinin);
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
for (int i = 0; i < table.getChildCount(); i++) {
TableRow row = (TableRow) table.getChildAt(i);
row.setLayoutAnimation(controller);
}

startAnimation(),
LayoutAnimationController.
, .
( 50%
1 .) , ImageView .

LayoutAnimationController,
: clearAnimation().

, onPause(),
:
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
for (int i = 0; i < table.getChildCount(); i++) {
TableRow row = (TableRow) table.getChildAt(i);
row.clearAnimation();
}


, ,
QuizSplashActivity QuizMenuActivity
. Intent,
QuizMenuActivity startActivity().
finish() QuizMenuActivity,
( ,
Back -).

fade_in2, 5 .
, in
.
AnimationListener,
: , .
onAnimationEnd(). ,

AnimationListener

onAnimationEnd():
Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.fade_in2);
fade2.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(QuizSplashActivity.this,
QuizMenuActivity.class));
QuizSplashActivity.this.finish();
}
});

, Been There, Done That!


, -.

, .

! Been There, Done That!. -,


- View,
.
splash.xml. ,

QuizSplashActivity QuizManuActivity.


: Android ?
: Android .

. ,
- VideoView ,
.
, , ,
, .
: TableLayout ,
TableRow(R. id. TableRowOl R. id.TableRow02) ?
: TableRow
, .
,
. -
.
:

Layout
AnimationController - TableLayout,
TableRow?
: LayoutAnimationController TableLayout,
TableRow, ImageView. ,
, .


1.
?

2.

?
A.
B.
C.

-, .
-, , .
, .

3.
- LinearLayout - View ( ).
?

8.
, :
;
;
ListView;
.
,
Android. Been There, Done
That!, ListView RelativeLayout.
, ,
.

,
. , ,
.
: ,
, .
. 8.1 .

. 8.1.
Been There, Done That!
.
, ,
.

. , ,
ListView. ,
,
.
- , .

, . ,

.
, - ListView, ,
.
,
Been There, Done That!.

, , ,
-
/res/layout/menu.xml.
QuizMenuActivity.
, LisiView
ImageView.

RelativeLayout
,
TextView.
TextView, ? -
RelativeLayout, -
-. ,
- RelativeLayout
-:
ImageView,
-;
TextView,
-;
ImageView,
-.
ListView
ListView. ListView
, View.
ListView TextView,
ListView View.
ListView, TextView,
. TextView,
ListView, ,
TextView

ListView. ,
ListView.

,
ImageView, ListView.
, -
LinearLayout RelativeLayout,
ListView ImageView . . 8.2
.

. 8.2.
Been There, Done That!

.
menu, xml, - .

, - , ,
, ,
.

/res/drawable:
bkgrnd.jpg, divider.png, half.png textured.png.
- LinearLayout bkgrnd.jpg.
ListView divider.png
textured.png . ImageView,
, half.png.

/res/values/strings.xml,
TextView, .
, /res/menu/colors.xml,
, ,
TextView, TextView,
ListView. ,
/res/values/dimens.xml, ,
ListView.
,
, .
,
.

, ,
. menu, xml
. ,
ListView.

, Eclipse
menu.xml. ,

-.
1.
- LinearLayout background @drawable/bkgrnd.
- LinearLayout.
2.
- RelativeLayout.
layout_width wrap_content, layout_height
wrap_content.
3.

ImageView

-
RelativeLayout.

layout_alignParentLeft

layout_alignParentTop ImageView
true. src @drawable/quizicon.

4.
TextView, , - RelativeLayout.
text, textSize textColor TextView
, .
layout_centerHorizontal layout_ alignParentTop
true.

. ... __________________________________________________
, TextView,
,
shadowColor, shadowDx, shadowDy shadowRadius.
5.
- RelativeLayout,
ImageView. layout_align
ParentRight layout_alignParentTop ImageView true. src @drawable/quizicon.
6.

-
RelativeLayout,
ListView ImageView. -
RelativeLayout - RelativeLayout,
, -
RelativeLayout
LinearLayout.
7.
ImageView - RelativeLayout. src
@drawable/half, layout_width layout_height
fill_parent, ,
.
layout_alignParentBottom true, scaleType
fitEnd, -
.
8.
ListView ListView_Menu
ImageView.
layout_width fill_parent, layout_height
wrap_content. layout_alignTop true.
, .
menu.xml.
! ____________________________________________________________
Eclipse
ListView . ListView,
Android.
.

ListView
/res/layout/menu_item.xml,
ListView.
menu_item.xml TextView.
TextView ,
, .
text ListView.
textColor textsize TextView,
, .
menu_item.xml :
<TextView
xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=fill_parent
android:textSize=@dimen/menu_item_size
android:text=test string
android:layout_gravity=center_horizontal
android:layout_height=wrap_content
android:shadowRadius=5
android:gravity=center
android:textColor=@color/menu_color
android:shadowColor=@color/menu_glow
android:shadowDy=3
android:shadowDx=3 />

. ... __________________________________________________
, TextView Eclipse, text - .
ListView .
menu_item.xml.
LISTVIEW
QuizMenuActivity.java.
ListView. -, ListView ,

ListView .
ListView
ListView . ListView
, ,

. ,
, String
ListView.
ListView
setContentview() onCreate() .
ListView, ,
findViewById(), :
ListView menuList = (ListView) findViewById(R.id.ListView_Menu);

string
TextView, ListView.
, :
String[] items = { getResources().getString(R.string.menu_item_play),
getResources().getString(R.string.menu_item_scores),
getResources().getString(R.string.menu_item_settings),
getResources().getString(R.string.menu_item_help) };

, ListView ,
, ,
(menu_item.xml).
.
ArrayAdapter:
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,
R.layout.menu_item, items);

, ListView :
menuList.setAdapter(adapt);

QuizMenuActivity.java
Been There, Done That! Android. -
, . 8.3.

. 8.3.
Been There, Done That!
, ... ___________________________________________________
, AndroidManifest.xml,
QuizMenuActivity, .
, . ,
.
ListView
,
ListView.
, , ,
ListView.
,
ListView, setOnltemClickListener()
ListView. , onltemClick(),
AdapterView.OnltemClickListener, :
menuList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View itemClicked,
int position, long id) {
TextView textView = (TextView) itemClicked;
String strText = textView.getText().toString();
if (strText.equalsIgnoreCase(getResources().getString(
R.string.menu_item_play))) {
// Launch the Game Activity
startActivity(new Intent(QuizMenuActivity.this,

QuizGameActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(
R.string.menu_item_help))) {
// Launch the Help Activity
startActivity(new Intent(QuizMenuActivity.this,
QuizHelpActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(
R.string.menu_item_settings))) {
// Launch the Settings Activity
startActivity(new Intent(QuizMenuActivity.this,
QuizSettingsActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(
R.string.menu_item_scores))) {
// Launch the Scores Activity
startActivity(new Intent(QuizMenuActivity.this,
QuizScoresActivity.class));
}

});

onItemClick() ,
, .

TextView ( ,
TextView, ,
instanceof) Text
. ,
, id View.
, OnitemClickListener()
,
Been There, Done That!.
ListView
( ,
) ListView, -. ListView
, . ListView
.
, ... __________________________________________________
ListView, ListActivity,
ListView.

ListView
. divider ,
. ,
( ).
,

.
.

ListView ,
. ListView
listselector. ListView
- .
_____________________________________________
ListView,
menu.xml divider ListView
@drawable/divider, (
), .
ListView.
menu.xml
listSelector ListView @drawable/textured, ( ),
.

. 8.4.
Been There, Done That!,
ListView

L i s t V i e w ,
Been There, That! ,
, . 8.4.


Android :
. ,
View.
ListView, ,
.
,
, , , Play (). Delete () Add to Playlist ( ),
.
. ,
Menu .

.
, ,
Been There, Done That!.
,
,
.

,
, .
QuizGameActivity,
.


XML /res/menu gameoptions.xml.
<menu>,
<item>. <item>
. .

id , .

title
.

icon ,
.

; Settings () Help
(). , gameoptions.xml
:
<menu
xmlns:android=http://schemas.android.com/apk/res/android>
<item
android:id=@+id/settings_menu_item
android:title=@string/menu_item_settings
android:icon=@android:drawable/ic_menu_preferences></item>
<item
android:id=@+id/help_menu_item
android:title=@string/menu_item_help
android:icon=@android:drawable/ic_menu_help></item>
</menu>

title ,
. ,

Android SDK,
.

. ... ___________________________________________________
,
android.R.drawable, , ,
. ,
, Android SDK, . , /platforms,
/data/res/drawable.

, Menu,

onCreateOptionsMenu() QuizGameActivity. ,
()

Intent

onCreateOptionsMenu() QuizGameActivity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.gameoptions, menu);
menu.findItem(R.id.help_menu_item).setIntent(
new Intent(this, QuizHelpActivity.class));
menu.findItem(R.id.settings_menu_item).setIntent(
new Intent(this, QuizSettingsActivity.class));
return true;
}


,
, onOptionsltemSelected()
. , ,
, :
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
startActivity(item.getIntent());
return true;
}

, ... ___________________________________________________
onOptionsltemSelected() . , ,
setlntent(), .
,
Menultem, .
, : .
, , ,
Menu (. 8.5).

. 8.5.
Been There, Done That!

.
, Android- ,
-, RelativeLayout,
ListView.
,
Android, .

: setOnClickListener()
setOnltemClickListener() ListView?
: setOnClickListener()
ListView. setOnltemClick Listener()
-
View ListView.
: ListView ,
. ,
?
: ListView
, setSelection().


1.

? Menu.

2.

ListView?
A.
B.
C.

.
.
.

3.
-
View ?
A.
B.
C.

RelativeLayout.
AbsoluteLayout.
LinearLayout.

4.
? ListActivity
,
ListView.


1.
. Menu . ,
View.
2.
,
ListView ,
.
3.
- RelativeLayout
, view ,
, . - RelativeLayout

View
-.
4.

. ListActivity ListView.

1.
,
ListView. ListView
, ,
- ImageView, - TextView.
(: .)
2.
,
.
3.
LinearLayout menu.xml,
, ,
.

9.

, :
;
;
;
;
XML-.
Been There, Done That!:
.
, TextView ,
, ,
Android SDK.
.
,
TabHost, . , ,
XML-
.

, , :
.
. 9.1 .
, .
, ,
. -,

/res/layout/help.xml

QuizHelpActivity.

. 9.1.

Been There, Done That!

9.2. -
Been There, Done That!
,
( - RelativeLayout)
TextView .
. 9.2 .

.
help.xml, -
.

, ,
,
.
/res/raw/quizhelp.txt,
TextView .

. ... ___________________________________________________

. .
HTML. , .

help.xml .
Eclipse . ,
- .
- LinearLayout
background @drawable/bkgrnd.
- LinearLayout.
, menu.xml.
- RelativeLayout,
ImageView TextView. text TextView
@string/help,
.
TextView TextView_HelpText LinearLayout, - RelativeLayout.
layout_width layout_height fill_parent.
, ... ___________________________________________________
, -,
, TextView,
, , Email Android, linksClickable TextView true, autoLink
all.
, help.xml.
, ... ___________________________________________________
,
TextView, textStyle.


, help.xml ,
QuizHelpActivity , quizhelp.txt
TextView TextView_HelpText.

, , , quizhelp.txt.
/raw.
. ,
,
,
Android Eclipse.
. ... ___________________________________________________
Android-
Android .
Fi1 Stream,
Context:
getFilesDir(), getDir(), openFileInput(), openFileOutput(),
deleteFile() getFileStreamPath().

Android , Java,
/,
. ,
openRawResource(), :
InputStream iFile = getResources().openRawResource(R.raw.quizhelp);

, , InputStream,
.
.
, TextView,
findViewById(), ,
setText(), :
TextView helpText = (TextView) findViewById(R.id.TextView_HelpText);
String strFile = inputStreamToString(iFile);
helpText.setText(strFile);

____________________________________________________________

inputStreamToString(), QuizHelpActivity,
. .
InputStream
string, InputStream.

QuizHelpActivity.java Been There, Done


That! Android. ,
, .
. 9.3.

______________________________________
, , ,
, . 9.3.
TextView.
, , , , ..
,
TextView , , .

. 9.3.
Been There, Done That!

, ,
.
, ,
. :
.
. , .

. ,
TextView ListView
. ,
.
,
. . 9.4
.

. 9.4.
Been There, Done That!

, , ,
-.
/res/layout/scores.xml,
QuizScoresActivity. ,
- RelativeLayout,
.
TabHost ,
- TableLayout
, .
TabHost
,
TabHost, ,
. TabHost

. XML- ,
:
TabHost;
TabHost
- LinearLayout, TabWidget
- FrameLayout;
-
FrameLayout.
. 9.5 - .

. 9.5. -
Been There, Done That!



. scores.xml,
- .

, , 6\\ ,
L.-. Been There, Done That!
,
, .
XML-, ,
.
/res/xml XML-
allscores.xml friendscores.xml,
.
<?xml version=1.0 encoding=utf-8?>
<! XML- >
<scores>
<score
username=LED
score=12345
rank=1 />
<score
username=SAC
score=12344
rank=2 />
<score
username=NAD
score=12339
rank=3 />
</scores>

.
<scores> <score>.
<score> : username, score rank.
,
20 ( ) .


scores.xml. -,
.

! ____________________________________________________________
Eclipse
TabHost. ,
, XML-.
Android , Android.
1.

, .
2.
- LinearLayout, android:background @drawable/bkgrnd.
- LinearLayout.
3.
, .
- RelativeLayout
ImageView TextView. text TextView

@string/scores,

.
4.
- LinearLayout, - RelativeLayout, TabHost TabHostl.
layout width layout_height
fill_parent.
5.
TabHost -
LinearLayout, orientation vertical.
layout_width layout_height -
fill_parent.
6.
- LinearLayout, , TabWidget. id
@android:id/tabs.
7.
- LinearLayout, 5,
, TabWidget,
- FrameLayout. id FrameLayout @android:id/tabcontent,
layout_width layout_height fill_parent.
8.
. -
FrameLayout - TableLayout
. - TableLayout
. -
TableLayout_FriendScores.

layout_width

layout_height - fill_parent.

stretchColums *,
.

! ____________________________________________________________

, , @android:id/tabcontent;
, .
Android.
, @+id/tabcontent. .
,
ScrollView. ,
- TableLayout,
- ScrollView scrollbars
ScrollView vertical.
layout_width layout_height ScrollView.
, TabHost (
,
- TableLayout), :
<TabHost
android:id=@+id/TabHost1
android:layout_width=fill_parent
android:layout_height=fill_parent>
<LinearLayout
android:orientation=vertical
android:layout_width=fill_parent
android:layout_height=fill_parent>
<TabWidget
android:id=@android:id/tabs
android:layout_width=fill_parent
android:layout_height=wrap_content />
<FrameLayout
android:id=@android:id/tabcontent
android:layout_width=fill_parent
android:layout_height=fill_parent>
<ScrollView
android:id=@+id/ScrollViewAllScores
android:layout_width=fill_parent
android:layout_height=fill_parent
android:scrollbars=vertical>
<TableLayout
android:id=@+id/TableLayout_AllScores
android:layout_width=fill_parent
android:layout_height=fill_parent
android:stretchColumns=*>
</TableLayout>
</ScrollView>

<ScrollView
android:id=@+id/ScrollViewFriendScores
android:layout_width=fill_parent
android:layout_height=fill_parent
android:scrollbars=vertical>
<TableLayout
android:id=@+id/TableLayout_FriendScores
android:layout_width=fill_parent
android:layout_height=fill_parent
android:stretchColumns=*></TableLayout>
</ScrollView>
</FrameLayout>
</LinearLayout>
</TabHost>

scores.xml.
! ____________________________________________________________
, Eclipse NullPointerException:null.
TabHost Android 2.1.
.


QuizScoresActivity.java
, TabHost.
TabHost ,
All Scores ( )
. ,
- TableLayout .
TabHost
TabHost
. ,
, findViewById().
setup() TabHost, TabHost
TabWidget FrameLayout
, , :
TabHost host = (TabHost) findViewById(R.id.TabHost1);
host.setup();

TabHost

, TabHost , , addTab().
TabSpec,
. , All Scores ( ):
TabSpec allScoresTab = host.newTabSpec(allTab);
allScoresTab.setIndicator(getResources().getString(R.string.all_scores
),
getResources().getDrawable(android.R.drawable.star_on));
allScoresTab.setContent(R.id.ScrollViewAllScores);
host.addTab(allScoresTab);

TabSpec allScoresTab allTab.


TextView ().
,
ScrollViewAllScores, - TableLayout
TableLayout_AllScores, scores.xml.
friendTab
TabHost. , ,
(
).
,
, .
setCurrentTabById()
:
host.setCurrentTabByTag(allTab);

QuizScoresActivity.java
Android. , ,
- .
! ____________________________________________________________
TabHost,
TabActivity, TabHost.
.
XML-
Android XML-,
:

SAX (Simple API for XML);


XML Pull Parser;
Limited DOM Level 2 core support.

XML-
. XML-
.

XML-
XML-,
. , /res/ xml/allscores.xml.
XmlResourceParser,
getXML(), :
XmlResourceParser mockAllScores =
getResources().getXml(R.xml.allscores);

XML- XmlResourceParser
,
:<scores> <score >. <score>
username, rank score. ,
, L-
while(),
next(), :
int eventType = -1;
boolean bFoundScores = false;
// Find Score records from XML
while (eventType != XmlResourceParser.END_DOCUMENT) {
if (eventType == XmlResourceParser.START_TAG) {
// Get the name of the tag (eg scores or score)
String strName = scores.getName();
if (strName.equals(score)) {
bFoundScores = true;
String scoreValue = scores.getAttributeValue(null,
score);
String scoreRank = scores.getAttributeValue(null, rank);
String scoreUserName =
scores.getAttributeValue(null, username);
insertScoreRow(scoreTable, scoreValue, scoreRank,
scoreUserName);
}
}
eventType = scores.next();
}

START_TAG.
<score>, , .
,
getAttributeValue().
TableRow - TableLayout (
).

L- TableLayout TabHost,
QuizScoresActivity.
TableRow - TableLayout,
, ,
.
,
. ,
, ( . 9.6).

. 9.6.
Been There, Done That!

- Been There, Done That!.


,
TextView
. ,
.

TabHost, , L .


: , TabHost,
id,
Android?
: ,
.
, , Android
( ) ,
.
Android SDK.
: .
?
: ,
. -, L-,
. -,
- view .
, ,
,
, ,
Android - ,
. ,
L-, ,
, . ,
Eclipse
.


1.
.

? TextView

2.

?
A.
B.
C.

Tabify.
TabActivity.
TabController.

3.
? XML- XML-pe,
.
4.

?
A.
B.
C.

Scroll Layout.
Scroller.
ScrollView.

. TextView
.
. , ,
TabActivity,
.
. XML- , L- .
XML-, XML-
XML Pull Parser.
. ScrollView
view .

1.
,
. ,
Android- Android.
, .

www.openintents.org/en/intentstable.
2.
,
.
, TableLayout TextView.
.

10.

, :
;
EditText;
Button;
Spinner;
SharedPreferences.
Been There,
Done That!.
, .

, EditText, Spinner Button. ,
, .


.
, , ,
. (
, .)
, :
Nickname (). , .
, ,
20 ,
.
Email. .
.
Password (). .
. ,
. .

Date of Birth ( ).
, .
,
.
Gender (). ,

. : Male
() (1), Female () (2) Prefer Not to Say (He ) (0).
. 10.1 .

. 10.1.
Been There, Done That!

,
. ,
.
.
,

ScrollView.
, ,
. ScrollView
, LinearLayout .
-
LinearLayout: , TextView,
, , . ,
Nickname () TextView,
(Nickname:), EditText,
.
,
:
Nickname () Email
,
EditText.
Password ()
EditText.
. Button,
Dialog, (
EditText).
, ,
TextView.
Date of Birth ( ) DatePicker.
DatePicker
, ,
. , ,
, Button,
DatePickerDialog.
, (
) TextView.
Gender () ,
Spinner (
).
. 10.2 - .

. 10.2. -
Been There, Done That!


. settings.xml, - .

, ,
, . ,
. ,

,
.

.
strings.xml:
,
TextView (, NickName:);

Button (, SetPassword);

, TextView
;
, TextView ,
Date of Birth ( ) ;
, TextView ,
;
Gender (),
Spinner (, Male).
, strings.xml.

Spinner, ListView, .
,
Gender (), ,
.
String (
ArrayAdapter). ,
(Male, Female, Prefer Not To Say), ,
, .
,
/res/values/arrays.xml.
string-array genders. string-array
item .
, strings.xml
,
:

<string
name=gender_male>Male</string>
<string
name=gender_female>Female</string>
<string
name=gender_neutral>Prefer Not To Say</string>

arrays.xml item
genders . , item
( 0) @string/gender_neutral.
arrays.xml :
<?xml version=1.0 encoding=utf-8?>
<resources>
<string-array
name=genders>
<item>@string/gender_neutral</item>
<item>@string/gender_male</item>
<item>@string/gender_female</item>
</string-array>
</resources>

arrays.xml. ,
genders ,
, R.array.genders.

settings.xml .
, Eclipse
. ,
-.
- LinearLayout,
background @drawable/bkgrnd.
- LinearLayout.
, ,
.
ScrollView,
. isScrollContainer
true, scrollbars vertical.
layout_width layout_height
fill_parent.
ScrollView - LinearLayout,
. orientation
vertical, layout_width layout_height
fill_parent.
- LinearLayout.

- LinearLayout TextView,
Nickname ().
TextView EditText.
id EditText_Nickname, maxLength
20, maxLines 1, inputType
textPersonName.
TextView,
Email. EditText,
id EditText_Email, maxLines
1, inputType textEmailAddress.
Password (),
TextView,
. - LinearLayout
, :
Button TextView. id Button
Button_Password, text
Password (). TextView , Password () (
, Password not set).
, Password
(), Date of Birth ( ).
-
Date of Birth ( ). -
LinearLayout ,
: Button TextView. id
Button Button_DOB, text
Date of Birth ( ). TextView
,
Date of Birth ( ) ( , Date not set).
Gender (), TextView,
Gender ().
Spinner id Spinner_Gender.
, , , ,
, , .
settings.xml.


, settings.xml ,
QuizSettingsActivity,

. ,
.

EditText
EditText, TextView.
, . . 10.3
EditText.

. 10.3. EditText
EDITTEXT
, TextView (, textColor
textSize), EditText.
EditText, .
inputType. Android ,
. , inputType
EditText, Email,
textEmailAddress, Android
, ( @).
textPassword inputType ,
.
minLines maxLines. ,
.
maxLength. ,
. , Nickname
(), maxLength EditText, ,
20.

TextView, ,
EditText, getText() setText(). ,
, EditText EditText_Nickname,
getText(), :

EditText nicknameText = (EditText)


findViewById(R.id.EditText_Nickname);
String strNicknameToSave = nicknameText.getText().toString();

getText() Editable,
.

, EDITTEXT
, EditText,
.
EditText , . ,
Enter
Nickname () View. OnKeyListener,
setOnKeyListener() EditText, :
final EditText nicknameText =
(EditText) findViewById(R.id.EditText_Nickname);
nicknameText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String strNicknameToSave =
nicknameText.getText().toString();
// TODO: Save Nickname setting (strNicknameToSave)
return true;
}
return false;
}
});

EDITTEXT
. ,

EditText, , EditText_Pwdl
EditText_Pwd2. TextView TextView_PwdProblem
, .
:
final EditText p1 = (EditText) findViewById(R.id.EditText_Pwd1);
final EditText p2 = (EditText) findViewById(R.id.EditText_Pwd2);
final TextView error = (TextView)
findViewById(R.id.TextView_PwdProblem);

TextWatcher EditText,
addTextChangedListener(), :
p2.addTextChangedListener(new TextWatcher() {

@Override
public void afterTextChanged(Editable s) {
String strPass1 = p1.getText().toString();
String strPass2 = p2.getText().toString();
if (strPass1.equals(strPass2)) {
error.setText(R.string.settings_pwd_equal);
} else {
error.setText(R.string.settings_pwd_not_equal);
}
}
// Other required overrides do nothing
});

EditText EditText_Pwd1
. ,
EditText_Pwd2, EditText
TextView TextView _PwdProblem,
, (. 10.4).

. 10.4. Password ()
EditText TextView
Button
Button Android , ,
. , Button
, . . 10.5 Button.

. 10.5. Button

BUTTON
, TextView, textColor
textSize, Button.

Button: Dialog,
, DatePickerDialog.
Button,
text,
layout_wtdth layout_height Button
wrap_content,
.
, ... __________________________________________________
Android Button; Button ImageButton. ImageButton , Button, ,
Drawable.
, Button. ,
(

),

background,
drawableTop,
drawableBottom, drawableLeft drawableRight Button
Drawable.
______________________________________
Button Button_DCB,
settings.xml:
1.
background Button, Drawable
@drawable/textured.
2.
drawableTop Button,
Drawable
@drawable/divider.
3.
drawableBottom Button,
Drawable
@drawable/divider. , Button
. !
4.
Button ,
background, drawableTop drawableBottom
Button_DOB.

Button
setOnClickListener(). , onClick()
View.OnClickListener. .
, , Button
Button_DOB, onCreate()
QuizSettingsActivity :
Button pickDate = (Button) findViewById(R.id.Button_DOB);
pickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

// Handle date picking dialog

});

Button, findViewByld(), View.OnClickListener,


setOnClickListener(). onClick() DatePickerDialog.
Dialog. -, .

, ... ______________________________________________________
,
.

. 10.6. ,

, onClick()
Button_DOB, :
Toast.makeText(QuizSettingsActivity.this,
TODO: Launch DatePickerDialog,
Toast.LENGTH_LONG).show();

. 10.6 .

Spinner
Spinner
Android.
(. 10.7, ), Spinner , (. 10.7, ) .

. 10.7. Spinner ()
()
SPINNER
Spinner .
ListView, Spinner , .
Spinner, :
1.
2.
3.

Spinner .
.
setAdapter() Spinner.

Spinner ,
findViewById():

final Spinner spinner = (Spinner) findViewById(R.id.Spinner_Gender);

. Spinner
-. . , Android , Spinner, . , android.R.layout.simple_spinner_item

Spinner.

android.R.layout.simple_spinner_dropdown_item .

,
genders ArrayAdapter,
createFromResource():
ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this,
R.array.genders, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);

, setAdapter() Spinner,
:
spinner.setAdapter(adapter);

SPINNER
, Spinner , ,
, setSelection(). , , ,
, 2 (
). ,
Spinner Female (), :
spinner.setSelection(2);

Spinner .

Spinner ,

setOnItemSelectedListener() Spinner,
. ,
onItemSelected() AdapterView.setOnItemSelectedListener(),
:
spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View
itemSelected,
int selectedItemPosition, long selectedId) {
// TODO: Save item index (selectedItemPosition) as Gender
setting
}
// Other required overrides
});

. 10.8.
Been There, Done That!
___________________________________________________________________
Android SDK

AdapterView.OnItemSelectedListener.

SHAREDPREFERENCES

, SharedPreferences. ,
.
SharedPreferences
QuizActivity :
public static final String GAME_PREFERENCES = GamePrefs;

QuizActivity , :
public static final String GAME_PREFERENCES_NICKNAME = Nickname;
// String
public static final String GAME_PREFERENCES_EMAIL = Email;
// String
public static final String GAME_PREFERENCES_PASSWORD = Password;

// String
public static final String GAME_PREFERENCES_DOB = DOB;
// Long
public static final String GAME_PREFERENCES_GENDER = Gender;
// Int


SharedPreferences
, ,
, . QuizSettingsActivity

-,

SharedPreferences:
SharedPreferences mGameSettings;

onCreate() -
:
mGameSettings =
getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE);

SharedPreferences
( GAME_PREFERENCES,
QuizActivity). MODE_PRIVATE ,
.
, , - , SharedPreferences.Editor,
. , EditText Nickname (), , getText() EditText:
final EditText nicknameText =
(EditText) findViewById(R.id.EditText_Nickname);
String strNickname = nicknameText.getText().toString();

, String ,
EditText,

SharedPreferences.Editor, putString():
Editor editor = mGameSettings.edit();
editor.putString(GAME_PREFERENCES_NICKNAME, strNickname);
editor.commit();

Nickname (), Email Password ()


string, Date of Birth ( ) Gender
() long integer .
,
, ,
putLong() putInt() Shared Preferences.Editor.

Nickname (),
Email Gender (). Date of Birth ( ) Password ()
,
DatePickerDialog ( ) Dialog ( ).
QuizSettingsActivity ,
TODO, , .

SharedPreferences
,
( ).
. . ,
, Nickname (), ,
, EditText EditTextNickname.
contains() getString()
SharedPreferences:
final EditText nicknameText =
(EditText) findViewById(R.id.EditText_Nickname);
if (mGameSettings.contains(GAME_PREFERENCES_NICKNAME)) {
nicknameText.setText(mGameSettings.getString(
GAME_PREFERENCES_NICKNAME, ));
}

,
GAME_PREFERENCES_NICKNAME, SharedPreferences, contains(). contains() true,
( String) SharedPreferences, getString().
Nickname (), Email Password () getString().
Date of Birth ( ) getLong(), Gender () getInt().
, ... ______________________________________________________
Android XML-. File Explorer () DDMS Eclipse. ,
SharedPreferences, :
/data/data/< >/shared_prefS/< >.xml.
onDestroy()
QuizSettingsActivity , :

@Override
protected void onDestroy() {
Log.d(DEBUG_TAG, SHARED PREFERENCES);
Log.d(DEBUG_TAG, Nickname is:
+ mGameSettings.getString(GAME_PREFERENCES_NICKNAME, Not set));
Log.d(DEBUG_TAG, Email is:
+ mGameSettings.getString(GAME_PREFERENCES_EMAIL, Not set));
Log.d(DEBUG_TAG, Gender (M=1, F=2, U=0) is:
+ mGameSettings.getInt(GAME_PREFERENCES_GENDER, 0));
// We are not saving the password yet
Log.d(DEBUG_TAG, Password is:
+ mGameSettings.getString(GAME_PREFERENCES_PASSWORD, Not set));
// We are not saving the date of birth yet
Log.d(DEBUG_TAG, DOB is:
+ DateFormat.format(MMMM dd, yyyy, mGameSettings.getLong(
GAME_PREFERENCES_DOB, 0)));
super.onDestroy();
}

,
QuizSettingsActivity (, Back ), LogCat.

- Been There,
Done That!. , ,
EditText, ,
Spinner. , Button, Dialog. , SharedPreferences.

: Save () Cancel (), -?
: , ,
, , .
( , , .).
.
: Spinner ?

: , , Spinner, . , Spinner .


1.

? EditText TextView.

2.

Android?
Button.
TextButton.
ImageButton.
3.
? Calendar
SharedPreferences.
A.
B.
C.

1.
. TextView ,
, getText() setText(),
EditText.
2.
. Android : Button
. ImageButton
Drawabe.
3.
. SharedPreferences
: Boolean, float, int, long String.
long (
).

1.

Button Password ().
Clicked! (!) ,
Button.
2.
EditText ,
(KEYCODE_DPAD_UP)
(KEYCODE_DPAD_DOWN) , Enter
(KEYCODE_ENTER).
3.
Clear (), ,
clear() SharedPreferences.Editor.
commit().

11.
,
, :
;
DatePickerDialog;
;
.
Been There,
Done
That!,

Dialog

QuizSettingsActivity. Dialog
, .
Dialog, DatePickerDialog
, ,
Dialog, .

Dialog
, . ,
, .
Dialog
Activity .
Dialog
Android SDK
Dialog, :
Dialog Dialog (. 11.la).
AlertDialog , Button
(. 11.1 b).
Character PickerDialog ,
, (. 11.1c).

DatePickerDialog DatePicker ( 11.1d).

ProgressDialog ProgressBar,

(. 11.1e)

TimePickerDialog TimePicker (. 11.1f)

. 11.1. Dialog,
Android
Dialog
, Dialog, . Dialog
.

Dialog ,
. Dialog , . , Dialog, Dialog. ,
Dialog:

showDialog() Dialog.

dismissDialog()
Dialog . Dialog
Dialog .
Dialog showDialog()
.

removeDialog() Dialog
Dialog . Dialog
.
showDialog() Dialog .

, , .
Dialog ( ).
showDialog() Dialog
. onCreateDialog(),
.
onCreateDialog() .
Dialog,
onCreateDialog() switch
Dialog Dialog.

Dialog Dialog ,
, .
onPrepareDialog() .
onCreateDialog()
, onPrepareDialog() showDialoq(), ,
.

, , showDialog()
Dialog ,
onCreateDialog().

Dialog ,
. , , dismissDialog()
Dialog.

Dialog . .

, ,
removeDialog()
Dialog.
DATEPICKERDIALOG

Dialog

QuizSettingsActivity. ,
DatePickerDialog, . DatePickerDialog QuizSettingsActivity
.
1.

.
2.
onCreateDialog() ,
DatePickerDialog
, 1.
3.
onPrepareDialog() ,
DatePickerDialog
.
4.
DatePickerDialog
showDialog(),
Dialog.
DatePickerDialog
QuizSettingsActivity
DatePickerDialog, , ,
:
static final int DATE_DIALOG_ID = 0;

onCreateDialog()

QuizSettingsActivity case
Dialog:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// TODO: Return a DatePickerDialog here
Using DatePickerDialog 185
}
return null;
}

,
DatePickerDialog. switch DATE_ DIALOG_ID
DatePickerDialog
. DatePickerDialog
DatePickerDialog.OnDateSetListener, , onDateSet(), ,
,
SharedPreferences:
DatePickerDialog dateDialog =
new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Time dateOfBirth = new Time();
dateOfBirth.set(dayOfMonth, monthOfYear, year);
long dtDob = dateOfBirth.toMillis(true);
dob.setText(DateFormat
.format(MMMM dd, yyyy, dtDob));
Editor editor = mGameSettings.edit();
editor.putLong(GAME_PREFERENCES_DOB, dtDob);
editor.commit();
}
}, 0, 0, 0);

DatePicker , .
, DatePickerDialog,
. DatePickerDialog
, ,
, onCreateDialog(),
( ). , DatePickerDialog,
switch onCreateDialog():
return dateDialog;

DatePickerDialog
DatePickerDialog ,
, onPrepareDialog(),
DatePicker , , .
! ______________________________________________________________
, Calendar.
Calendar , .
Calendar, DatePickerDialog.
onPrepareDialog()
Dialog, Dialog,
. , , -

DatePickerDialog,
updateDate():

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
switch (id) {
case DATE_DIALOG_ID:
// Handle any DatePickerDialog initialization here
DatePickerDialog dateDialog = (DatePickerDialog) dialog;
int iDay, iMonth, iYear;
// Check for date of birth preference
if (mGameSettings.contains(GAME_PREFERENCES_DOB)) {
// Retrieve Birth date setting from preferences
long msBirthDate =
mGameSettings.getLong(GAME_PREFERENCES_DOB, 0);
Time dateOfBirth = new Time();
dateOfBirth.set(msBirthDate);
iDay = dateOfBirth.monthDay;
iMonth = dateOfBirth.month;
iYear = dateOfBirth.year;
} else {
Calendar cal = Calendar.getInstance();
// Todays date fields
iDay = cal.get(Calendar.DAY_OF_MONTH);
iMonth = cal.get(Calendar.MONTH);
iYear = cal.get(Calendar.YEAR);
}
// Set the date in the DatePicker to the date of birth OR to
the
// current date
dateDialog.updateDate(iYear, iMonth, iDay);
return;
}
}

DatePickerDialog
DatePickerDialog,
, Button
. DatePickerDialog,
Button Button_DOB.
Button_DOB,
Toast.
, showDialog() DatePickerDialog, . 11.2:
Button pickDate = (Button) findViewById(R.id.Button_DOB);
pickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});

. 11.2. DatePickerDialog,

, ______________________________________________________
DateFormat
. DateFormat , Calendar, , long
( ). , , long, January 1,2010, format() DateFormat, :
String strDate = DateFormat.format(MMMM dd, yyyy, dtDob);


Dialog ,
. AlertDialog AlertDialog.Builder . , :
1.
,
AlertDialog.
2.

Dialog.

3.
onCreateDialog() , AlertDialog.
4.

, showDialog().


Been There, Done That! . . 11.3
( ).
, ,
, ,
. ,
. . 11.4
.
, EditText. TextView ,
, .

. 11.3.

. 11.4.
Been There, Done That!

. 11.5. -
Been There, Done That!
. 11.5 - .
Button,
AlertDialog.
-.


,
.
password_dialog.xml. . , :
Eclipse
/res/layout/password_dialog.xml.
- LinearLayout. id root,
orientation vertical. layout_width
layout_height fill_parent. - LinearLayout.
TextView Password (). EditText id EditText_Pwdl, maxLines 1, inputType textPassword.
TextView Password (Again) ( ()). EditText id EditText_Pwd2, maxLines 1,
inputType textPassword.
, TextView id,
TextView_PwdProblem,
. TextView ,
.
password_.dialog.xml.

QUIZSETTINGSACTIVITY
AlertDialog, , ,
:
static final int PASSWORD_DIALOG_ID = 1;

onCreateDialog()

QuizSettingsActivity, case Dialog:


case PASSWORD_DIALOG_ID:
// Build Dialog
// Return Dialog

. View (. .
):
LayoutInflater inflater =
(LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout =
inflater.inflate(R.layout.password_dialog,
(ViewGroup) findViewById(R.id.root));
password_dialog.xml View,
LayoutInflater atop, ,
( - LinearLayout root,
).
, view,
, . .
, EditText_Pwd1 View layout, findViewByid(), :
final EditText
(EditText)
final EditText
(EditText)

p1 =
layout.findViewById(R.id.EditText_Pwd1);
p2 =
layout.findViewById(R.id.EditText_Pwd2);


EditText, , ,
EditText .
, EditText, TextWatcher,
EditText.
TextView
TextView_PwdProblem, :
final TextView error =
(TextView) layout.findViewById(R.id.TextView_PwdProblem);
p2.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String strPass1 = p1.getText().toString();
String strPass2 = p2.getText().toString();
if (strPass1.equals(strPass2)) {
error.setText(R.string.settings_pwd_equal);
} else {
error.setText(R.string.settings_pwd_not_equal);

});

}
}
// ... other required overrides do nothing

TextWatcher , .
, afterTextChanged().
, View , AlertDialog.
AlertDialog.Builder :
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle(R.string.settings_button_pwd);

-
AlertDialog.Builder .
Dialog setTitle().
Button:
() (Cancel). , ,

Button

removeDialog(), :
QuizSettingsActivity.this
.removeDialog(PASSWORD_DIALOG_ID);

() .
,
EditText, , ,
:
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
TextView passwordInfo =
(TextView) findViewById(R.id.TextView_Password_Info);
String strPassword1 = p1.getText().toString();
String strPassword2 = p2.getText().toString();
if (strPassword1.equals(strPassword2)) {
Editor editor = mGameSettings.edit();
editor.putString(GAME_PREFERENCES_PASSWORD,
strPassword1);
editor.commit();
passwordInfo.setText(R.string.settings_pwd_set);
} else {
Log.d(DEBUG_TAG, Passwords do not match.
+ Not saving. Keeping old password (if set).);
}
QuizSettingsActivity.this
.removeDialog(PASSWORD_DIALOG_ID);
}

});

, Cancel,
.
setNegativeButton() Builder:
builder.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
QuizSettingsActivity.this
.removeDialog(PASSWORD_DIALOG_ID);
}
});


Builder, create(), AlertDialog:
AlertDialog passwordDialog = builder.create();
return passwordDialog;

, , ,
, , ,
showDialog() .
Been There, Done That!
, Button
Button_Password. ,
Button
:
Button setPassword = (Button) findViewById(R.id.Button_Password);
setPassword.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(PASSWORD_DIALOG_ID);
}
});

. 11.6 ,
Dialog.

. 11.6.
Been There, Done That!

,
Dialog
Been There, Done That!. . Dialog ,
, Dialog . , Dialog.

: ?
: Dialog,
Dialog .
, showDialog(),
. ,

removeDialog().
: , Dialog?
:
Dialog, getOwneActivity() Dialog.



1.

?
A.
B.
C.

Popup.
ActivityWindow.
Dialog.

2.
?
, .
3.
?
, Alert ContinueOrCancel.

1.
. Dialog

onCreateDialog() showDialog().
2.
. onPrepareDialog().
3.
.

1.
onDataSet() DatePickerDialog
,
TextView .
2.
,
TextView
( ).

12.
, :
; ViewSwitcher;
XML-;
.
Been There, Done Thai! . - .
, - View,
imageSwitcher TextSwitcher, . ,
QuizGameActivity ,
.

( ).
, . ,
, , - .
,
. . . 12.1 .

. 12.1.
Been There, Done That!

, , : , , .
-,
/res/ tayout/game.xml QuizGameActivity.

. 12.2. -
Been There, Done That!
- RelativeLayout . , - RelativeLayout ,
: ImageView, TextView Button
.
12.2 - .
, Button,
ImageView TextView
. ( )
ImageSwitcher
TextSwitcher, ViewSwitcher.
ViewSwitcher
- View: View View,
.
View, View

, , / .
View ViewFactory.
, ImageSwitcher
ViewFactory ImageView
ImageView , Button.
, TextSwitcher - TextView,
.
. 12.3 - ,
ImageSwitcher TextSwitcher.

. 12.3. -
Been There, Done That! ImageSwitcher TextSwitcher

.
game.xml,
- .

:

( string) ,
Button, , ;
( Dimension) ( Color),
;

XML-, .

String,
Dimension Color, ,
.
, Been There, Done That!,
.
,

XML-:
/res/xml/samplequestions.xml /res/xml/smaplequestions2.xml. ,
, XML-
. ,
, , .
-
- XML- . :
<?xml version=1.0 encoding=utf-8?>
<! This is a mock question XML chunk >
<questions>
<question
number=1
text=
Have you ever been on an African safari?
imageUrl=
http://www.perlgurl.org/Android/BeenThereDoneThat/Question
s/q1.png
/>
<question
number=2
text=
Have you ever climbed a mountain?
imageUrl=
http://www.perlgurl.org/Android/BeenThereDoneThat/Question
s/q2.png
/>
<question
number=3
text=
Have you ever milked a cow?
imageUrl=
http://www.perlgurl.org/Android/BeenThereDoneThat/Question
s/q3.png
/>
</questions>

, XML- .
<questions>, <question>. <question> : (number),
(text) URL , (imageUrl).
,
, .

game.xml .
, Eclipse
. , - .
1.
- LinearLayout
background @drawable/bkgrnd.
- LinearLayout;
2.
- RelativeLayout
layout_width layout_height wrap_content.
3.
ImageView -
RelativeLayout .

src

ImageView

@drawable/quizicon. id
ImageView

@+id/ImageView_Header,
@+id/ImageView_Header2,
@+id/ImageView_Header3

@+
id/ImageView_Header4.
4.
ImageView id,
ImageView_Header, layout_alignParentLett
layout_alignParentTop true.
5.
ImageView id,
ImageView_Header2,

layout_
alignParentRight layout_alignParentTop true.
6.
ImageView id,
ImageView_Header3, layout_alignParentLeft
layout_alignParentBottom true.
7.
ImageView id,
ImageView_Header4,

layout_alignParentRight layout_alignParentBottom
true.
8.
- RelativeLayout - RelativeLayout,
ImageView, . -

id @+id/RelativeLayout_Content.
layout_width layout_height wrap_content. ,
gravity center,
layout_margin 45.
9.
- RelativeLayout
ImageSwitcher id,
@+id/ImageSwitcher_QuestionImage.

layout_width layout_height wrap_content.


layout_alignParentTop layout_centerInParent
true.
10.
TextSwitcher id,

@+id/TextSwitcher_QuestionText,

ImageSwitcher. layout_width layout_height

wap_content.

layout_centerInParent true, layout_below


@+id/ ImageSwitcher_QuestionImage.
11.
Button id,
@+id/Button_Yes, TextSwitcher.
layout_width layout_height wrap_content.

layout_alignParentBottom

layout_alignParentLeft true. text


( Yes) ,
Button .
12.
Button id,
@+id/Button_No, Button.
layout_width layout_height
wrap_content. layout_ alignParentBottom
layout_alignParentRight true. text
( No) ,
Button .
game.xml.
! _______________________________________________________________
Eclipse
TextSwitcher ImageSwitcher .
TextView ImageView, , Android.
.

VIEWSWITCHER
, , View, Android SDK , ViewSwitcher. ViewSwitcher
. ViewSwitcher
-
- -,
. - View
ViewSwitcher
ViewFactory.
ViewSwitcher ;
TextSwitcher. ViewSwitcher,
TextView.

ImageSwitcher. ViewSwitcher,
ImageView.
ViewSwitcher
-,

-
View.

ViewFactory -,
ImageSwitcher TextSwitcher
.
, ... ______________________________________________________
ViewSwitcher.
, ViewSwitcher.
ViewFactory - View ViewSwitcher.
ViewFactory
- View ViewSwitcher
ViewSwitcher
ViewFactory, setFactory(). ViewFactory
makeView(). View . , ViewFactory
TextSwitcher -
TextView, ViewFactory ImageSwitcher
- ImageView.
ViewFactory ImageSwitcher,

:

private class MyImageSwitcherFactory implements


ViewSwitcher.ViewFactory {
public View makeView() {
ImageView imageView = new ImageView(QuizGameActivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView .setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageView ;
}
}

, , , -
makeView(). ,
- ViewSwitcher,
-.
ViewSwitcher,
VlewFactory setFactory(). ,
ViewFactory ImageSwitcher
MyImageSwitcherFactory, :
ImageSwitcher questionImageSwitcher =
(ImageSwitcher) findViewById(R.id.ImageSwitcher_QuestionImage);
questionImageSwitcher.setFactory(new MyImageSwitcherFactory());

ViewFactory,
- TextView
. MyTextSwitcherFactory,
ViewFactory, , :
private class MyTextSwitcherFactory implements
ViewSwitcher.ViewFactory {
public View makeView() {
TextView textView = new TextView(QuizGameActivity.this);
textView.setGravity(Gravity.CENTER);
Resources res = getResources();
float dimension = res.getDimension(R.dimen.game_question_size);
int titleColor = res.getColor(R.color.title_color);
int shadowColor = res.getColor(R.color.title_glow);
textView.setTextSize(dimension);
textView.setTextColor(titleColor);
textView.setShadowLayer(10, 5, 5, shadowColor);
return textView;
}
}

,
,

MyImageSwitcherFactory, MyTextSwitcherFactory
makeView() TextView ,
.

TextSwitcher
TextSwitcher
- TextView.
TextSwitcher TextSwitcher QuestiorT^x*
.

TEXTSWITCHER
TextSwitcher

ViewFactory

setCurrentText(), :
TextSwitcher questionTextSwitcher = (TextSwitcher)
findViewById(R.id.TextSwitcher_QuestionText);
questionTextSwitcher.setFactory(new MyTextSwitcherFactory());
questionTextSwitcher.setCurrentText(First Text String);

TEXTSWITCHER
TextSwitcher,
- TextView, setText():
TextSwitcher questionTextSwitcher = (TextSwitcher)
findViewById(R.id.TextSwitcher_QuestionText);
questionTextSwitcher.setText(Next Text String);

setText()

MyTextSwitcherFactory

-
TextView String, setText().
ImageSwitcher
ImageSwitcher
- ImageView.
ImageSwitcher ImageSwitcher_QuestionText
, ,
.
IMAGESWITCHER

ImageSwitcher
ViewFactory ,
.
setImageDrawable():
__________________________________________________________________
, setlmageURL()
ImageSwitcher URL , ,
. , URL
Drawable.
getQuestionImageDrawable() , :

private Drawable getQuestionImageDrawable(int questionNumber) {


Drawable image;
URL imageUrl;
try {
// Create a Drawable by decoding a stream from a remote URL
imageUrl = new URL(getQuestionImageUrl(questionNumber));
Bitmap bitmap = BitmapFactory.decodeStream(imageUrl.openStream());
image = new BitmapDrawable(bitmap);
} catch (Exception e) {
Log.e(DEBUG_TAG, Decoding Bitmap stream failed.);
image = getResources().getDrawable(R.drawable.noquestion);
}
return image;
}

getQuestionImageUrl() , - .
Hashtable . (
, , .)
getQuestionlmageUrl() ,
.
URL
PNG, ImageSwitcher.
BitmapDrawable. , ,
android.permission.INTERNET,
Android.

IMAGESWITCHER
ImageSwitcher,
- ImageView, setImageDrawable():

ImageSwitcher questionImageSwitcher =
(ImageSwitcher) findViewById(R.id.ImageSwitcher_QuestionImage);
Drawable image = getQuestionImageDrawable(nextQuestionNumber);
questionImageSwitcher.setImageDrawable(image);

setlmageDrawable()

MyImageSwitcher Factory - irageView


Drawable,
setImageDrawable().
ViewSwitcher
- View
ViewSwitcher,

setInAnimation()

setOutAnimation(). ,
- TextSwitcher,
Android,
:
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
TextSwitcher questionTextSwitcher =
(TextSwitcher) findViewById(R.id.TextSwitcher_QuestionText);
questionTextSwitcher.setInAnimation(in);
questionTextSwitcher.setOutAnimation(out);

setText() setCurrentText() TextSwitcher ,


.
, ImageSwitcher,
, .

Been There, Done That! . ,
. ,
, ,
, .

. XML-.
.
,
. ,
:

1.
, ,
SharedPreferences.
2.
(
XML) .
3.
Button, ImageSwitcher TextSwitcher,
.
4.
, , ,
.
5.
.

, ,
SHAREDPREFERENCES
,
Integer SharedPreferences :
.

QuizActivity:
public static final String GAME_PREFERENCES_SCORE = Score;
public
static
final
String
GAME_PREFERENCES_CURRENT_QUESTION
CurQuestion;

SharedPreferences -
QuizGameActivity:
SharedPreferences mGameSettings;

- mGameSettings onCreate()
QuizGameActivity:
mGameSettings = getSharedPreferences(GAME_PREFERENCES,
Context.MODE_PRIVATE);

, no , /'.
SharedPreferences ,
. ,
(, getInt()
SharedPreferences, :
int startingQuestionNumber =
mGameSettings.getInt(GAME_PREFERENCES_CURRENT_QUESTION, 0);

Been There, Done That! ,


.
, ,
XML- .
XML-,
. ,
, ,
- Hashtable.

, XML-,
, .
, ,
XML- , QuizActivity:
public static final String XML_TAG_QUESTION_BLOCK = questions;
public static final String XML_TAG_QUESTION = question;
public static final String XML_TAG_QUESTION_ATTRIBUTE_NUMBER =
number;
public static final String XML_TAG_QUESTION_ATTRIBUTE_TEXT = text;
public static final String XML_TAG_QUESTION_ATTRIBUTE_IMAGEURL
imageUrl;

, ,
,
XML-:
public static final int QUESTION_BATCH_SIZE = 15;


HASHTABLE
QuizGameActivity
Question,
:
private class Question {
int mNumber;
String mText;
String mImageUrl;
public Question(int questionNum, String questionText, String
questionImageUrl) {
mNumber = questionNum;
mText = questionText;
mImageUrl = questionImageUrl;
}
}

.
( XML-,

). ,

Hashtable

QuizGameActivity, Question
XML-:
Hashtable<Integer, Question> mQuestions;
, ... _______________________________________________
Android SDK ,
Java. , java.util
(, Hashtable)
, android.util .
Hashtable
- onCreate () QuizGameActivity,
:
mQuestions = new Hashtable<Integer, Question>(QUESTION_BATCH_SIZE);

, , XML-,
, XmlResourceParser questionBatch. XmlResourceParser
Question
- Hashtable put(), :
String questionNumber =
questionBatch.getAttributeValue(null,
XML_TAG_QUESTION_ATTRIBUTE_NUMBER);
Integer questionNum =
new Integer(questionNumber);
String questionText =
questionBatch.getAttributeValue(null,
XML_TAG_QUESTION_ATTRIBUTE_TEXT);
String questionImageUrl =
questionBatch.getAttributeValue(null,
XML_TAG_QUESTION_ATTRIBUTE_IMAGEURL);
// Save data to our hashtable
mQuestions.put(questionNum,
new Question(questionNum, questionText, questionImageUrl));

-
Hashtable containsKey().
Question ,
get():
Question curQuestion = (Question) mQuestions.get(questionNumber);


Button
ImageSwitcher TextSwitcher. ,
Button,
ViewSwitcher .
, Button ,
.
Yes ()
No (). OnClickListener.OnClick()
Button, Yes ():

Button yesButton = (Button) findViewById(R.id.Button_Yes);


yesButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
handleAnswerAndShowNextQuestion(true);
}
});

View.onClick.Listener() Button,
No (He), Yes (),
. ,
handleAnswerAndShowNextQuestion() false. ,
, ViewSwitcher.

handleAnswerAndShowNextQuestion(),
boolean bAnswer. - , ,
Button:
private void handleAnswerAndShowNextQuestion(boolean bAnswer) {
// ,
// ,
yes
// , ,

.
,

SharedPreferences:
int curScore =
mGameSettings.getInt(GAME_PREFERENCES_SCORE, 0);

int nextQuestionNumber =
mGameSettings.getInt(GAME_PREFERENCES_CURRENT_QUESTION, 1) + 1;

,
SharedPreferences:
Editor editor = mGameSettings.edit();
editor.putInt(GAME_PREFERENCES_CURRENT_QUESTION, nextQuestionNumber);

Yes (),

SharedPreferences:
if (bAnswer == true) {
editor.putInt(GAME_PREFERENCES_SCORE, curScore + 1);
}

,
SharedPreferences, commit()
Editor:
editor.commit();

, Hashtable .
, :
if (mQuestions.containsKey(nextQuestionNumber) == false) {
//
try {
loadQuestionBatch(nextQuestionNumber);
} catch (Exception e) {
Log.e(DEBUG_TAG, Loading updated question batch
failed, e);
}
}
, TextSwitcher ImageSwitcher,
:
if (mQuestions.containsKey(nextQuestionNumber) == true) {
//
TextSwitcher questionTextSwitcher =
(TextSwitcher) findViewById(R.id.TextSwitcher_QuestionText);
questionTextSwitcher.setText(getQuestionText(nextQuestionNumber));
//
ImageSwitcher questionImageSwitcher =
(ImageSwitcher)
findViewById(R.id.ImageSwitcher_QuestionImage);
Drawable image = getQuestionImageDrawable(nextQuestionNumber);
questionImageSwitcher.setImageDrawable(image);
} else {
handleNoQuestions();
}

. 12.4.
Been There, Done That!
,
, . 12.4.
, ... ____________________________________________________

. ,
SharedPreferences ,
Android . DDMS Eclipse ,
SharedPreferences. , Settings () Android. Been
There, Done That! .

, .
handleNoQuestions()
,
:
TextSwitcher questionTextSwitcher =
(TextSwitcher) findViewById(R.id.TextSwitcher_QuestionText);
questionTextSwitcher.setText(getResources().getText(R.string.no_questi
ons));
ImageSwitcher questionImageSwitcher =
(ImageSwitcher) findViewById(R.id.ImageSwitcher_QuestionImage);
questionImageSwitcher.setImageResource(R.drawable.noquestion);
The Been There,
Done That!
game screen.
:

Button yesButton =
(Button) findViewById(R.id.Button_Yes);
yesButton.setEnabled(false);
Button noButton =
(Button) findViewById(R.id.Button_No);
noButton.setEnabled(false);

. 12.5.
Been There, Done That!,

, ,
. 12.5. ,
Button.
Back .

Been There. Done That!


. , view ImageSwitcher TextSwitcher.
,
Android SDK, - Hashtable
, XML-. ,
SharedPreferences ,
.

: ,
setlmageURL() ImageSwitcher setImageDrawable()?

: . ,
. , setImageURL()
,
ImageSwitcher ( ImageView).
Drawable .
: ViewSwitcher
?
: , ,
- View View. , ,
- View,
- -
- View. ViewSwitcher
- View.


1.

ViewSwitcher?

A.
B.
C.
D.

TextSwitcher;
VideoSwitcher;
ImageSwitcher;
AudioSwitcher.

2.
? TextView,
TextSwitcher,
TextSwitcher.
3.
? , java.io, java.math, java.net
java.util, Android SDK.

1.
. ViewSwitcher ;
TextSwitcher ( Textview)
ImageSwitcher ( ImageView).
2.
. TextView,
TextSwitcher,
ViewFactory.
3.
. Java
Android SDK. ,
Android, android.*.


Android SDK.

1.
, . ,
SharedPreferences,
.
2.
,
Hashtable ,
.

III.

ANDROID
13.
, :
;
ImageButton;
;
;
.
Been There, Done That!

. :
, , ,
.



,
.
.
. :
,
.

Been There, Done That!,
, ,
. . 13.1 ,
, ,
.
,
Been There, Done
That!. :
. Android
ImageButton .
,
/res/layout/settings.xml, , Nickname ().

. 13.1.

Been There, Done That!


Nickname (), ( ImageButton
, TextView Nickname ()
EditText Nickname ()) ,
LibearLayout ( ).
Nickname () - LinearLayout
, . . 13.2
-, .

. 13.2. -

! ,
( ), ,
. .

Button. ImageButton,
,
ImageButton, .

,
settings.xml,
.
, , Been
There, Done That!, ,
String, Dimension, Color Drawable. ,
,
.

settings.xml .

Eclipse :
TextView TextView_Nickname.
- LinearLayout
ScrollView, orientation -
LinearLayout horizontal. layout_width
layout_height fill_parent.
ImageButton ImageButton_Avatar
- LinearLayout.
layout_width layout_height wrap_content.


, adjustViewBounds true,
scaleType fitXY.
maxHeight minHeight ,

(, 75px).
- LinearLyaout

ImageButton. orientation vertical.

layout_width

layout_height

fill_parent.
Nickname () ( TextView TextView_Nickname
EditText EditText_Nickname)
- LinearLayout.
settings.xml.
, ,
. 13.3
IMAGEBUTTON
ImageButton ,
Drawable. . 13.3
ImageButton, ,
.
, ImageButton Button, View,
. Button
TextView ( Button ,
, ), ImageButton
ImageView.

. 13.3. ,

_____________________________________________________________
, ImageButton,
.
Uri , .

ImageButton
ImageView,
,
ImageButton, :
setImageBitmap(). ,
, ImageButton,
Bitmap.
setlImageDrawable(). ,
, ImageButton,
Drawable.
setImageResource(). ,
, ImageButton,
.
setlmageURI(). , ,
ImageButton, Uri.
! _________________________________________________________
ImageButton
, .
- setImageURI(null),
setImageURI()
Uri, ,
ImageButton.
, ,
Drawable, Uri .
setImageURI() ImageButton
, ,
. URI
/ . Uri :

android.resource://[]/[ ]

, Uri
Drawable avatar.png :
Uri /
:
android.resource://[]/[ ]/[ ]

, Uri
Drawable avatar.png /:
Uri path = Uri.parse(
android.resource://com.androidbook.triviaquiz13/drawable/avatar);

Uri Drawable,
setImageURI() ImageButton,
:
ImageButton avatarButton= (ImageButton)
findViewById(R.id.ImageButton_Avatar);
avatarButton.setImageURI(path);

ImageButton
ImageButton, , ,
, - View
. ImageButton,
,
.
SETONCUCKUSTENER
ImageButton,
,
View.OnClickListener:
avatarButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: Launch the Camera and Save the Photo as the Avatar
}
});

, Button.


SETONLONGCLICKLISTENER
, Android.
,
.
, .

View.OnLongClickListener

setOnLongClickListener()

ImageButton.

OnLongClickListener ,

onLongClick().

OnLongClickListener ImageButton,
:
avatarButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO: Launch Image Picker and Save Image as Avatar
return false;
}
});

onLongClick() onClick()
OnClickListener. onLongClick()
, true,
.

_______________________________________________
,
ImageButton:
1.
QuizSettingsActivity.java
ImageButton_
Avatar.
2.
onClick () OnLickListener
Short click.
3.
onLongClick () OnLongClickListener
Long click.
4.
.
ImageButton, , , .

, ImageButton , . ,
( )
.


.
sharePreferences .
QuizActivity, :
public static final String GAME_PREFERENCES_AVATAR = Avatar;


3, ,
, ,
startActivityForResult(),
,
onActivityResult() .
startActivityForResult() :
, .
OuizSettingsActivity.
. ImageButton,
, .
ImageButton,
. ,
QuizSettingsActivity:
static final int TAKE_AVATAR_CAMERA_REQUEST = 1;
static final int TAKE_AVATAR_GALLERY_REQUEST = 2;

startActivityForResult() ,
, QuizSettingsActivity
, . ,
, onActivityResult()
QuizSettingsActivity. ,
switch
, ,
:
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
switch(requestCode) {
case TAKE_AVATAR_CAMERA_REQUEST:
if (resultCode == Activity.RESULT_CANCELED) {
// Avatar camera mode was canceled.
} else if (resultCode == Activity.RESULT_OK) {
// TODO: HANDLE PHOTO TAKEN
}
break;
case TAKE_AVATAR_GALLERY_REQUEST:

if (resultCode == Activity.RESULT_CANCELED) {
// Avatar gallery request mode was canceled.
} else if (resultCode == Activity.RESULT_OK) {
// TODO: HANDLE IMAGE CHOSEN
}
break;
}

,
. resultCode onActivityResult()
Activity.RESULT_CANCELED. resultCode
Activity.RESULT_OK, .
,
,
saveAvatar().
. saveAvatar()
:

private void
{
// TODO:
// TODO:
// TODO:
// TODO:
}

saveAvatar(Bitmap avatar)
Save the Bitmap as a local file called avatar.jpg
Determine the Uri to the local avatar.jpg file
Save the Uri path as a String preference
Update the ImageButton with the new image



.
, .
, _____________________________________________________
android.hardware.Camera,
Camera ,
. android.permission.CAMERA.
ACTION_IMAGE_CAPTURE. ,
onClick() OnClickListener ImageButton, :
Intent pictureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(pictureIntent, TAKE_AVATAR_CAMERA_REQUEST);

Android .
, , ,

, ,
Android. ImageButton
, , .
13.4.

. 13.4.
Android

, _____________________________________________________
, .. ,
, : . ?. . Android .
,
,
ACTION_CHOOSER.
, (
, ?).
, , createChooser(), :
Intent.createChooser(innerIntent,
Choose which application to handle this);


, , , .
ACTION_IMAGE_CAPTURE ,
. , . case onActivityResult TAKE_AVATAR_CAMERA_REQUEST, -

, intent data,
:
Bitmap cameraPic = (Bitmap) data.getExtras().get(data);


saveAvatar().

Android ACTION_ PICK,
.
URI. .

MIME-, , .
, ,
onLongClick() , :
Intent pickPhoto = new Intent(Intent.ACTION_PICK);
pickPhoto.setType(image/*);
startActivityForResult(pickPhoto, TAKE_AVATAR_GALLERY_REQUEST);

ImageButton ,
, , (.
13.5).

. 13.5.
Android

ACTION_PICK ,
, ,
URI, . ,
case onActivityResult()
TAKE_AVATAR_GALLERY_REQUEST UR!
, Intent data, :
Uri photoUri = data.getData();

, Url Bitmap,
, :
Bitmap galleryPic = Media.getBitmap(getContentResolver(), photoUri);


saveAvatar().


,
.
Bitmap (android.graphics.Bitmap) ,
.

! _______________________________________________________________
Bitmap
, PNG JPG.
(image.bmp).
PNG JPG
Android Bitmap.

compress() Bitmap
. , JPG-,
, :
String strAvatarFilename = avatar.jpg;
avatar.compress(CompressFormat.JPEG,
100, openFileOutput(strAvatarFilename, MODE_PRIVATE));

URI , fromFile() Uri.


, ,
compress(), :
Uri imageUri = Uri.fromFile(new File(getFilesDir(),
strAvatarFilename));

URI,
ImageButton. ,
(
), ImageButton
, . 13.6.

. 13.6.
Been There, Done That!


createScaledBitmap() Bitmap
.
! _______________________________________________________________
, .
( ).

BitmapFactory (android.graphics.BitmapFactory),
, , , .


,
, Matrix(android.graphics.Matrix)


Been There. Done That!. ,
, . ,
startActivityForResult() onActivityResult().
Android SDK,
.

: ACTION_IMAGE_CAPTURE , . ,
, .
?
: , , ( ,
EXTRA_OUTPUT).
: , Bitmap?
: , ( ). ,
, ,
.


1.
, onActivityResult()?
A.
B.
C.

requestCode.
resultCode.
data.

2.
? ImageButton
Button.
3.
? Bitmap
.bmp.

1.
. roqestCode, ,
, (

startActivityForResult())

resultCode , ,
.
2.
. ImageButton
ImageView. , Button
ImageButton, View.
3.
. Bitmap
, PNG
() JPG ().

1.
Bitmap, , . ,
Matrix, (
).
2.
createScaledBitmap() Bitmap, () .

14.

, :
,
;
;
, ;
.
Been There. Done That!
.
: ,
(LBS locationbased services), ,
GPS- Android SDK.
,


, LBS- ,
. Android LBS . LBS-
, .
Been There, Done That! - ,
, LBS-.
,
.
LBS-,
.

,
, , , ,
(, New York City, Iceland,

Yellowstone National Park, 90210).


, ,
GPS-.
Been
There, Done That!, . . 14.1
, ,
.

. 14.1. ,


Favorite Place
, , ,
ScrollView.
. ,
, , Date of Birth
( ) Password ().

/res/layout/settings.xml
Favorite Place ( ),
, Spinner
Gender ().
TextView,
. - LinearLayout
Button, ,
TextView,
Favorite Place ( ).
. 14.2 .

. 14.2.
,



,
AlertDialog.
Favorite Place ( )
:

( String);
( float);

( float).

____________________________________________________________________
, .
,
; (
, GPS- )
EditText, GPS-
Android SDK.

. 14.3.

,
( ). . 14.3 .


,
.
/res/layout/fav_place_dialog.xml.
.
LinearLayout .

TextView,
. EditText,
,
Button, .
EditText Button , RelativeLayout. , TextView:
GPS, GPS- ( Been
There. Done That! , ).
. 14.4 -
Favorite Place ( ).

. 14.4. -
Favorite Place ( )

,

, LBS- Been
There. Done That!, ,
. ,
. , :
1. String, Dimension, Color Drawable,
.

2. /res/layout/settings.xml,
,
. 14.5.
3. /res/layout/fav_place_dialog.xml
TextView, EditText Button,
(. 14.6).

. 14.5. ,
Favorite Place
( )

. 14.6.

4.
String QuizActivity
.
SharedPreferences
( String), (
float) ( float).
5.
QuizSettingsActivity,
.
(, PLACE_DIALOG_ID). onCreateDialog()
onPrepareDialog() , ,
.
,
. ,
:
initFavoritePlacePicker()
( )
Button,

.
initPasswordChooser() initDatePicker().
,
, .
,
Button, .
Dialog
. ,
Dialog
.
. ,
View.OnClickListener Button, ,
,
, Button Clicked.
__________________________________________________________________________
, .
,
, ,
GPS.

, LBS-,
. :
, .
: LBS-
.
Android .
LBS- :
LBS- , ,
, .
,
.
.
,

. , ,
,
.

, , ,
, .
, ,
, , (
) , ,
.

! ______________________________________________________________
, Android,
,
, .
, LBS-, Android SDK,
Google API.
, , Google Maps
Android-.
, Google API, ,
Google, ,
API .
,
,
LBS-
Google API-. , API Google Maps , ,
Intent
, .
AVD- GOOGLE API

, , Android ( ,
AVD- )
. Google Maps
, AVD- Google API.
Been
There, Done That!, AVD-
.

, Android ,
.

. DDMS Eclipse. ,
.
, ... _____________________________________________________________
GPS- Google Maps. , maps.google.com
. ,
, ( ). URL
. 11 . , 11, , 51.845959, 104.908447. 44.427896.110.585632 11 44.427896 -110.585632. , Google Maps
, .

. 14.7. Maps Android


, :
1.
. - ,
Home.
2.
Maps.
3.
Menu.
4.
My Location ( ) (. 14.7).
5.
Eclipse DDMS.
6.
,
.

7.
Emulator Control ( )
, Location Controls (
).
8.
.
: 44.427896
110.585632 (. 14.8).
9.
Send ().

. 14.8.
DDMS
, , Google Maps
.
, . 14.9.

. 14.9.

, _____________________________________________________
, geo fix.


LBS- Android
. Android-
,
<uses-permission> Android.
, LBS-
android.permission.ACCESS_FINE_LOCATION

android.permission.
ACCESS_COARSE_LOCATION. GPS- android.permission.ACCESS_FINE_LOCATION.
,
LocationManager, getSystemService(),
:
LocationManager locMgr =
(LocationManager) getSystemService(LOCATION_SERVICE);

LocationManager LBS-,
.

LBS-.
, getProviders()
LocationManager.
(,
). getBestProvider(),
,
.

LBS-. LBS-
getProvider().
LocationProvider,
, , ,
, ,
.

(
) getLastKnownLocation()
LocationManager. ,
,

.
, ;

getLastKnownLocation() Location:
Location recentLoc =
locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);

Location . LBS-. , ,
.
Location getLatitude() getLongitude().


, ,
,
requestLocationUpdates() LocationManager.
,
(, ,
).
( ) ( )
.
, ,
,
LocationListener (android.location.LocationListener).
,
,
.
! ______________________________________________________________________

. LBS-
, , (
, ). .

- () GPS- (, ).
Google Maps
(maps.google.com), Eiffel Tower, .
,
( ).
,
,
. , ,
, :

, ;
( );
;
(, LAX, LHR, JFK, DME);
.

, , ,
( ).
.
, , ,
. ,
Springfield, ,
, 35 ,
.
East Springfield Springfield by the Sea,
. ,
(,
).
!
, , , . ,
, , , , .
, ,
Android-
Android SDK Geocoder (android.1tion.Geocoder),
,
( ).
Geocoder :
Geocoder coder = new Geocoder(getApplicationContext());

Geocoder,
,
.
:
getFromLocationName() Geooder,
.
: , ,
. , Springfield
, :
String strLocation = Springfield;
List<Address> geocodeResults =
coder.getFromLocationName(strLocation, 1);

, Geocoder,
:
Iterator<Address> locations = geocodeResults.iterator();
while (locations.hasNext()) {
Address loc = locations.next();
double lat = loc.getLatitude();
double lon = loc.getLongitude();
// TODO:
}

Address .
getLatitude() getLongitude()
Address .
getFromLocationName(),
.
:
get.FromLocation() Geocoder
.
, ,
.

Android Google API. , MapView
Google Maps
.

.


, , Maps,
ACTION_VIEW URI,
. URI .
,
Maps ( ,
), URI:
geo:,
, :
String geoURI = String.format(geo:%f,%f?z=10, lat, lon);

URI , 1 23, , 1, , 23-


(
).
, UR1 :
geo:, ?z=

, :
String geoURI = String.format(geo:%f,%f?z=10, lat, lon);

URI,
parse(), Uri
ACTION_VIEW, :
Uri geo = Uri.parse(geoURI);
Intent geoMap = new Intent(Intent.ACTION_VIEW, geo);
startActivity(geoMap);

,
URI geo-,
(, Maps) .
, ,
Back .
Geocoder Maps,

, Button it! ( !),
. 14.10.

. 14.10.
Maps

. 14.11. Maps


Menu,
, ,
, ,
, . 14.11.


Android SDK LBS-
android.location. LBS-,
,
Android SDK.
LBS- Android
Android SDK, Google API, Android SDK.
GOOGLE API


Google API. ,
com.google.android.map:
MapView,
;

MapActivity, MapView ;

GeoPoint, :
,
( );


.
Google API
, API-.
, ,
. LBS-
Android,
Android, Android Wireless Application Development
( Addison-Wesley),
Google API.
- Google API,
code.google.com/android/add-ons/googleapis/reference/index.html.

,
, Been There, Done That!.
,
,
. ,
. ,
LBS-,
Android SDK.


: MapView.
Google API?
: -, Google API.
: http://code.google.com/android/add-ons/googleapis/mapkey.html.
Google ( ).
: ,
, ,
?
: .
LocationManager,
,
. ,
LBS-, ,
,
; , AsyncTask
, . ,
, ,
,
.


1.
LBS?
A.
B.
C.
D.

.
.
.
, LBS-cep-
, .
E. .
F. .
2.
? Button,
AlertDialog, Button.
3.

Android SDK?

A.
B.
C.

.
, .
.

4.
? ,
LBS-.


1.
F. , LBS, .
2.
. Button,
.
View.OnClickListener.
,
DialogInterface.OnClickListener,
AlertDialog.
3.
A B. Android SDK LBS-.
, . ,
, .
, Google API,
Android SDK.
4.
. LBS- (
Google API), DDMS
.

1.

, GPS- .
2.

,
.
3.
Been There, Done That! ,

.

15.
, :
;
;
;
.

Been There, Done That!.
. , ,

.

,
, - .
,
, ,
. ,
. Android-
, 3G (
) Wi-Fi. Android -,
HTTP, HTTPS, TCP/IP .
Been There, Done That!
XML-. ,
.
, Android,
, ,
, .
Been There, Done That!
:
QuizScoresActivity ;
QuizGameActivity
.
Been There, Done That! ,
,
Android-.
___________________________________________________________________
,
, .


.
( )
. , ,
.
Been There, Done That! iPhone BlackBerry, .
.
.
Google
Engine
(

code.google.com/appengine/) Java . Google


Engine ,
, .

, , .
, :

.

HTTP.

, ,
.
,
.
Java Server Pages (JSP)
TT- XML-,
Android-.
___________________________________________________________________
, , //
.
. ,
SQL- MySQL
.



XML-,
. -
Been There, Done That!.
, ,
, ,
XML- ,
- , ,
ProgressBar (. 15.1).
- .

. 15.1.



,
, - ,

,
, .
( ,
),
.
Android ,

. ,
:

, .

,
.

,
. , ,

.
, , ,
. ,
.
, ,
. ,
, ,
( ) , -
(, ).
! _______________________________________________________________
Android- , . ,
Android ,
,
.
.



. -,
, . .
, ,

, .
, ,
Android.
! _______________________________________________________________
, , , ,
.

,
Android. Android
, .
Settings () .
Airplane Mode ( )
.

Wi-Fi Wi-Fi , .

Mobile Networks { ) .

, Settings
() , About phone ( ) (
About device ( )), Status ().
:

(, 888-555-1212);

(, Verizon, T-Mobile);

(, CDMA EVDO rev. EDGE);

(, -81 dBm 0 asu);

(, In Service ());

(, Roaming ( ) Mot roaming


( ));

(, Connected ());

, ... ______________________________________________________
, - , ,
.
. , . , .

Android . ,
Java,
java.net. , Android SDK
.
, , SSL HTTPS.

Android- ,
Android.
,
, ,
.

! _______________________________________________________________
. , , ,
, .
Thread Java
AsyncTask Android, .

Android . Android- ,
<uses-permission> Android
.

, :

android.permission.INTERNET;
android.permission.ACCESS_NETWORK_STATE.

, ,
, Wi-Fi

.
,

android.permission.WAKE_LOCK,
.

Android SDK
.
.
,
.

, Android

android.permission.ACCESS_NETWORK STATE.
,
android.permission. CHANG E_NETWORK_STATE.

ConnectivityManager(android.net
.ConnectivityManager)

.
ConnectivityManager
getSystemService() Context :
ConnectivityManager conMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);

ConnectivityManager, () getNetworkInfo():
Networklnfo(android.net.Networklnfo)
, ,
:
boolean isMobileAvail = netInfo.isAvailable();
boolean isMobileConn = netInfo.isConnected();
boolean isRoamingConn = netInfo.isRoaming();

NetworkInfo .
.

, ,
, .

ConnectivityManager

requestRouteToHost(),
IP- (,
Wi-Fi).
HTTP
- Hypertext Transfer Protocol
(HTTP). HTTP
.
,
. Android,
, .
XmlPullParser. setInput() XmlPullParser
InputStream. . ,
, , URL,
:

URL xmlUrl = new URL(xmlSource);


XmlPullParser questionBatch =
XmlPullParserFactory.newInstance().newPullParser();
questionBatch.setInput(xmlUrl.openStream(), null);

, XML-, , XML , XmlResourceParser


XmlPullParser.
,
.
, ,
XML- , .
, TO... ______________________________________________________
, -,
WebView, WebKit
HTML- . WebView
, .


, - 1
, .
, ,
.
.
Android SDK
ProgressBar
.

ProgressBar
(. 15.2).
, ,
. ,
.

. 15.2.



,
.
,
(. 15.3).
setProgress() ProgressBar.
. 15.3.
, _____________________________________________________
, , .
,

,
ProgressBar .
ProgressDialog,
Dialog. , ProgressDialog (.
15.4) Been There, Done That!,
, , .
.

. 15.4.
,

,
ProgressDialog :
ProgressDialog pleaseWaitDialog = ProgressDialog.show(
QuizGameActivity.this,
Trivia Quiz,
Downloading trivia questions,
true);

dismiss(),
pleaseWaitDialog, :
pleaseWaitDialog.dismiss();

___________________________________________________________________
pleaseWaitDialog , show() ,
true.
, ,
. ,
, , .
,
ProgressDialog. ,
, ,
.

, -
- .
Android- ,
.
Android .
AsyncTask
,
.
,
.
Thread Handler

, .
,

Been There, Done That!


AsyncTask, .
AsyncTask

Android
SDK

AsyncTask
(ancroid.os.AsyncTask), , ,
.
,
AsyncTask,
.
onPreExecute() ,
, , .
dolnBackground() ,
.
publishProgress()

dolnBackground(),

, .
.
onProgressUpdate() ,
, , dolnBackground()
publishProgress().
.
onPostExecute() ,
, .
execute() , AsyncTask,
, ,
.

,
Thread Handler.

: , .
. ,

, , XML-,
.


Thread, ,
XML-, ,
, Handler ,
TextView parsingStatus,
:
import android.os.Handler;
Handler mHandler = new Handler();
// ...
new Thread() {
public void run() {
// Instantiate XML parser
mHandler.post(new Runnable() {
public void run() {
parsingStatus.setText(Began Parsing...);
}
});
// XML Parsing loop here
// Update parsingStatus has needed
mHandler.post(new Runnable() {
public void run() {
parsingStatus.setText(Finished parsing...);
}
});

}
}.start();

Thread Handler mHandler


, .

AsyncTask
QuizSettingsActivity
XML.
ScoreDownloaderTask, AsyncTask
QuizSettingsActivity:
private class ScoreDownloaderTask extends AsyncTask<Object, String,
Boolean> {
// TODO: Implement AsyncTask callback methods
TableLayout table;
}

- TableLayout
,
ScoreDownloaderTask -.
, ScoreDownloaderTask

ScoreDownloaderTask,
onCreate() QuizScoresActivity, execute()
ScoreDownloaderTask .
execute() : URL ,
(- TableLayout):
public static final String TRIVIA_SERVER_BASE =
http://tqs.mamlambo.com/;
public static final String TRIVIA_SERVER_SCORES =
TRIVIA_SERVER_BASE + scores.jsp;
// ...
allScoresDownloader =
new ScoreDownloaderTask();
allScoresDownloader.execute(TRIVIA_SERVER_SCORES, allScoresTable);
SharedPreferences prefs =
getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE);
Integer playerId = prefs.getInt(GAME_PREFERENCES_PLAYER_ID, -1);
if (playerId != -1) {
friendScoresDownloader = new ScoreDownloaderTask();
friendScoresDownloader.execute(
TRIVIA_SERVER_SCORES + ?playerId=
+ playerId, friendScoresTable);
}

ScoreDownloaderTask, AsyncTask.
___________________________________________________________________
playerId. .

onPreExecute(),
, ,
.
:
@Override
protected void onPreExecute() {
mProgressCounter++;
QuizScoresActivity.this.setProgressBarIndeterminateVisibility(true);
}

.
, ,
, . ,
mProgressCounter
. , , .

onCreate() ,

:
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

setContentView().

, .
XML-.
doInBackground(),
. , doInBackground(),
, . doInBackground(),
:
@Override
protected Boolean doInBackground(Object... params) {
boolean result = false;
String pathToScores = (String) params[0];
table = (TableLayout) params[1];
XmlPullParser scores = null;
URL xmlUrl = new URL(pathToScores);
scores = XmlPullParserFactory.newInstance().newPullParser();
scores.setInput(xmlUrl.openStream(), null);
if (scores != null) {
processScores(scores);
}
return result;
}

URL. URL
, openStream()
.
setInput() XmlPullParser XmlPullParser ,
, .
processScores() ScoreDownloaderTask.
,
XmlPullParser XML-,
publishProgress():
private void processScores(XmlPullParser scores)
throws XmlPullParserException, IOException {
int eventType = -1;
boolean bFoundScores = false;

// score XML-
while (eventType != XmlResourceParser.END_DOCUMENT) {
if (eventType == XmlResourceParser.START_TAG) {
// (, scores score)
String strName = scores.getName();
if (strName.equals(score)) {
bFoundScores = true;
String scoreValue =
scores.getAttributeValue(null, score);
String scoreRank =
scores.getAttributeValue(null, rank);
String scoreUserName =
scores.getAttributeValue(null, username);
publishProgress(scoreValue, scoreRank, scoreUserName);
}

}
eventType = scores.next();
}
// ,
if (bFoundScores == false) {
publishProgress();
}
}

publishProgress()
dolnBackground(),
onProgressUpdate().
, .

,

onProgressUpdate(). ,
, -
TableLayout, .
@Override
protected void onProgressUpdate(String... values) {
if (values.length == 3) {
String scoreValue = values[0];
String scoreRank = values[1];
String scoreUserName = values[2];
insertScoreRow(table, scoreValue, scoreRank, scoreUserName);
} else {
final TableRow newRow =
new TableRow(QuizScoresActivity.this);
TextView noResults =
new TextView(QuizScoresActivity.this);
noResults.setText(
getResources().getString(R.string.no_scores));

newRow.addView(noResults);
table.addView(newRow);
}

insertScoreRow() TableRow
- TableLayout.
. AsyncTask
Java.

onPostExecute(), ,
, ,
. ,
, mProgressCounter,
.
@Override
protected void onPostExecute(Boolean result) {
Log.i(DEBUG_TAG, onPostExecute);
mProgressCounter;
if (mProgressCounter <= 0) {
mProgressCounter = 0;
QuizScoresActivity.this.
setProgressBarIndeterminateVisibility(false);
}
}


,
onCancelled(). onCancelled() .
, , ,
onPostExecute() . ,
onCancelled().
:
@Override
protected void onCancelled() {
Log.i(DEBUG_TAG, onCancelled);
mProgressCounter;
if (mProgressCounter <= 0) {
mProgressCounter = 0;
QuizScoresActivity.this.
setProgressBarIndeterminateVisibility(false);
}
}

onCancelled() ,
cancel() AsyncTask. cancel() . -

, .
,
- . ,
onPause() , :
@Override
protected void onPause() {
if (allScoresDownloader != null &&
allScoresDownloader.getStatus() !=
AsyncTask.Status.FINISHED) {
allScoresDownloader.cancel(true);
}
if (friendScoresDownloader != null &&
friendScoresDownloader.getStatus() !=
AsyncTask.Status.FINISHED) {
friendScoresDownloader.cancel(true);
}
super.onPause();
}


, , ,
AsyncTask
.
.
,
. .
QuizGameActivity
QuizTask, AsyncTask, :
private class QuizTask extends AsyncTask<Object, String, Boolean> {
// TODO: Implement AsyncTask callback methods
}

___________________________________________________________________
- DEBUG_TAG QuizTask.
, .
QuizTask -
ProgressDialog,
:
int startingNumber;
ProgressDialog pleaseWaitDialog;

QuizTask, onCreate()
QuizGameActivity ,

execute() QuizTask. execute() : URL


, ,
( integer) :
public static final String TRIVIA_SERVER_QUESTIONS =
TRIVIA_SERVER_BASE + questions.jsp;
// ...
QuizTask downloader = new QuizTask();
downloader.execute(TRIVIA_SERVER_QUESTIONS, startingQuestionNumber)

,
onPreExecute().
, ,
. ,
.
, , ,
, ,
:
@Override
protected void onPreExecute() {
pleaseWaitDialog = ProgressDialog.show(
QuizGameActivity.this, Trivia Quiz,
Downloading trivia questions, true, true);
pleaseWaitDialog.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface dialog) {
QuizTask.this.cancel(true);
}
});
}
,
,

,
Back ,
. cancel()
AsyncTask. ,
, ,
.

, .
, , . (
)
doInBackgroun():
@Override
protected Boolean doInBackground(String... params) {

boolean result = false;


startingNumber = (Integer)params[1];
String pathToQuestions = params[0] +
?max= + QUESTION_BATCH_SIZE + &start= + startingNumber;
result = loadQuestionBatch(startingNumber, pathToQuestions);
return result;
}


loadQuestionBatch(). QuizTask
.
, URL
,
setlnput() XmlPullParser.
___________________________________________________________________
, .

,

onPostExecute(). ,
, ,
. ,
:
@Override
protected void onPostExecute(Boolean result) {
Log.d(DEBUG_TAG, Download task complete.);
if (result) {
displayCurrentQuestion(startingNumber);
} else {
handleNoQuestions();
}
pleaseWaitDialog.dismiss();
}

_____________________________________________________________
, .

Been There, Done That! .


,
, . ,

AsyncTask
.
, , .

: ?
: . : ,
, ,
.
,
, , .
: ,
Android?
: Android SDK , : android.net, java.net org.apache.


1.

Android?
A.
.
B.
Settings () Android.
C.

getHandsetNetworkStatus()

NetStatus.
2.
? Android
, ,
Android.
3.
? Android-
Google Engine.
4.

Android?
A.
B.
C.
D.
E.

HTTP.
HTTPS.
TCP.
IP.
(RS Raw Sockets).


A B.
,
Settings ()
Android.
. Android
.
.
, Android-.
Google Engine .
.
Android-. HTTP HTTPS
, -. TCP IP -
, Android, , ,
API Java
.

1.
Been There, Done That!
, . ,
,
.
2.
,
AsyncTask Thread Handler.
3.
,
,
.

16.

, :
;
-;
GET- HTTP;
POST- HTTP;
JAR- ;
MIME-.
Been There, Done That!,
, , ,
. ,
. ,
Android- MIME-.

Been There, Done That! .
.
HTTP- Apache,
Android, , Apache
.
Been There,
Done That!:
QuizSettingsActivity
.
QuizGameActivity
.
QuizSettingsActivity
.
"Been There, Thai!
, .
Android -.
___________________________________________________________________
,
, , .

Been There, Done That!


. ,
( ) .

.
, ,
TelephonyManager.
, Android SDK , ,
TelephonyManager. TelephonyManager Android-
, ,
(SIM Subscriber Identity Module),
.
! _______________________________________________________________
Android .
, .


Android-
, Android. Android
, <uses-permission> Android
.
0 ,
:

android.permission.READ_PHONE_STATE
android.permission.MODIFY_PHONE_STATE


TelephonyManager(android.telephony.
TelephonyManager) . TelephonyManager
getSystemService() Context :
TelephonyManager telMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);

TelephonyManager,
.


TelephonyManager
getCallstate(). , ,
.

, listen(),
.

, .

TelephonyManager
getNetworkType().
, ,
, GPRS, EDGE EVDO.
TelephonyManager (, NETWORK_TYPE_GPRS).

listen().
,

.
: D
GSM
SIM-KAPTE
getPhoneType() TelephonyManager,
, : CDMA GSM.
,
getDeviceId(). IMEI- GSM MEID- CDMA-.
(, IMSI-
GSM-), getSubscriberId().
SIM-
TelephonyManager SIM . :
getSimState() SIM-, ,
SIM- , (. . PIN) .

getSimSerialNumber() SIM.
getSimOperatorName() SIM.
getSimOperator()
, SIM-.

TelephonyManager
. ,
getVoiceMailNumber().

15 , ,
. , ,
isNetworkRoaming() TelephonyManager.
,
, .
,
, ,
: . ?
,
, ... _____________________________________________________
SmsManager (android.telephony.SmsManager)

SmsMessage (android.telephony.SmsMessage) SMS-.



10
SharedPreferences.
,
( SharedPreferences).
.
.
;
.
___________________________________________________________________
,
.
, , AsyncTask.

HttpClient
(org.apache.http), Android SDK.

HTTP .
, HttpGet
, - GET- HTTP.
, HttpPost
, POST- HTTP.
- HTML.
, , Android-,
HTML-.
- , Android-,
,
. , ,
Android, GET- POST , .
Apache HTTP ,
org.apache.http.client.
___________________________________________________________________
, , //
.
! _______________________________________________________________
, ,
. , , , ,
. Thread Java AsyncTask Android. , , , ProgressBar. 15.
GET- HTTP
GET- HTTP
HttpGet.
, URLEncodedUtils
List, BasicNameValuePair,
URL . ,

AsyncTask,
, .
ASYNCTASK,

AccountTask, AsyncTask,
QuizSettingsActivity. AccountTask
execute()
(. 16.1).
,
,
.
( ).

. 16.1


( )
AccountTask
, AsyncTask,
. (
, .)
doInBackground(),
:
@Override
protected Boolean doInBackground(Object... params) {
Boolean succeeded = false;
Integer playerId =
mGameSettings.getInt(GAME_PREFERENCES_PLAYER_ID, -1);
String nickname =
mGameSettings.getString(GAME_PREFERENCES_NICKNAME, );
String email =
mGameSettings.getString(GAME_PREFERENCES_EMAIL, );
String password =

mGameSettings.getString(GAME_PREFERENCES_PASSWORD, );
Integer score =
mGameSettings.getInt(GAME_PREFERENCES_SCORE, -1);
Integer gender =
mGameSettings.getInt(GAME_PREFERENCES_GENDER, -1);
Long birthdate =
mGameSettings.getLong(GAME_PREFERENCES_DOB, 0);
String favePlaceName =
mGameSettings.getString(GAME_PREFERENCES_FAV_PLACE_NAME, );
Vector<NameValuePair> vars = new Vector<NameValuePair>();
if (playerId == -1) {
TelephonyManager telManager =
Using an indeterminate
progress indicator
in the top
right of the
menu bar (faint
circle).
(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String uniqueId = telManager.getDeviceId();
vars.add(new BasicNameValuePair(uniqueId, uniqueId));
} else {
vars.add(
new BasicNameValuePair(updateId, playerId.toString()));
vars.add(
new BasicNameValuePair(score, score.toString()));
}
vars.add(new
vars.add(new
vars.add(new
vars.add(new
vars.add(new
vars.add(new

BasicNameValuePair(nickname, nickname));
BasicNameValuePair(email, email));
BasicNameValuePair(password, password));
BasicNameValuePair(gender, gender.toString()));
BasicNameValuePair(faveplace, favePlaceName));
BasicNameValuePair(dob, birthdate.toString()));

String url =
TRIVIA_SERVER_ACCOUNT_EDIT+ ? + URLEncodedUtils.format(vars, null);
HttpGet request = new HttpGet(url);
ResponseHandler<String> responseHandler =
new BasicResponseHandler();
HttpClient client = new DefaultHttpClient();
String responseBody = client.execute(request, responseHandler);
if (responseBody != null && responseBody.length() > 0) {
Integer resultId = Integer.parseInt(responseBody);
Editor editor = mGameSettings.edit();
editor.putInt(GAME_PREFERENCES_PLAYER_ID, resultId);
editor.commit();
succeeded = true;
}
return succeeded;
}

.
: , /

SharedPreferences), .
,
.
: .
,
.
, , , .
,
.
,
. GET-
URLEncodedUtils.format(). , HttpGet, GET-
HttpClient.
HttpClient
ResponseHandler .
BasicResponseHandler String.
String ,
SharedPreferenees
.
_____________________________________________________________________

. , . , ,
, ,
-, , Secure
Hash Algorithm (SHA), . MessageDigest, java. security,
:

MessageDigest sha = MessageDigest.getInstance(SHA);


byte[] enc = sha.digest(uniqueId.getBytes());

, ,
, .


QuizGameActivity ,
AsyncTask. QuizTask,

, ?
.


, doInBackground() QuizTask.
:
SharedPreferences settings =
getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE);
Integer playerId = settings.getInt(GAME_PREFERENCES_PLAYER_ID, -1);
if (playerId != -1) {
Log.d(DEBUG_TAG, Updating score);
Integer score = settings.getInt(GAME_PREFERENCES_SCORE, -1);
if (score != -1) {
pathToQuestions +=
&updateScore=yes&updateId=+playerId+&score=+score;
}
}

,
pathToQuestions, URL, ,
, .
, ... ______________________________________________________
List,
BasicNameValuePair, URLEncodeUtils.format().
, , .

POST- HTTP
, , GET-
POST- HTTP.
,
- HTML. POST (. 16.2).

. 16.2.
! _______________________________________________________________

, . ,
SQL-
, ( BLOB).
JAR- ANDROID-

MIME-
HttpClient. ,
Apache HttpClient Android SDK .
Android SDK MIME-,
,
SDK. , MIME-,
Apache JAR- .
, JAR- :

Mime4j (james.apache.org/mime4j/index.html);
HttpMime 4.0 (hc.apache.org/httpcomponents-client-ga/httpmime/index. html);
Apache Commons 10 (commons.apache.org/io/).

___________________________________________________________________
-, , // 15 16/libs.
, ... ______________________________________________________
, MIME-?
Wikipedia: en.wikipedia.org/wiki/MIME#Multipart_messages. ,
MIME- ,
, . MIME HTML- enctype form
multipart/form-data. MIME-
HTTP. , MIME-
.

JAR- ANDROID- ____________________________
JAR- Android-, :
1.
JAR-(), .
2.
/libs .
, /src /res.
3.
JAR-) /libs.
4.
lipse Project=>Properties
( => ), ^ Android-

. Java Build Path ( Java)


Libraries ().
5.
Add JARs ( JAR-)
JAR Selection ( JAR-) JAP-,
. .
6.
.
!
/ MIME-
Been There, Done That!, JAR-,
. ,
, lOUtils,
.
ASYNCTASK


QuizSettingsActivity ImageUploadTask,
AsyncTask. imageUploadTask
execute()
.
ImageUploadTask ,
AsyncTask,
.
doInBackground(), (
):
@Override
protected Boolean doInBackground(Object... params) {
String avatar =
mGameSettings.getString(GAME_PREFERENCES_AVATAR, );
Integer playerId =
mGameSettings.getInt(GAME_PREFERENCES_PLAYER_ID, -1);
MultipartEntity entity =
new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File(avatar);
FileBody encFile = new FileBody(file);
entity.addPart(avatar, encFile);
entity.addPart(updateId, new StringBody(playerId.toString()));
HttpPost request = new HttpPost(TRIVIA_SERVER_ACCOUNT_EDIT);
request.setEntity(entity);
HttpClient client = new DefaultHttpClient();
ResponseHandler<String> responseHandler =
new BasicResponseHandler();
String responseBody = client.execute(request, responseHandler);
if (responseBody != null && responseBody.length() > 0) {
Log.w(DEBUG_TAG,
Unexpected response from avatar upload: + responseBody);
}

return null;

MIME-, MIME-,
, MultipartEntity, :
,
, .
HttpPost URL .
MIME- HttpPost, setEntity().
, , ,
HttpClient ResponseHandler.

Been There, Done That! .


, ,
, . ,
, , ,
,
TelephonyManager. , GET- POST-
HTTP HttpClient.

: ?
:
. , ,
, - SSL ( HTTPS
HTTP, ).
, MessageDigest.
: Android JSON (JavaScript Object
Notation)?
: , , JSON.
org. json, Android SDK,


1.

TelephonyManager?
A.
B.
C.

.
.
.

2.
? Android SDK MIME-.

3.
? ,
,
.
4.
?
A.
B.
C.
D.

BackgroundTask.
AsyncTask.
Thread.
AsyncActivity.

B. TelephonyManager
.
. MIME- .
,
, MIME-.
. , .
. .
',
.
A D. AsyncTask ,
Thread. .
SDK, .

1.

URL http://tqs.mamlambo.com/getplayer, GET-
.
, playerId,
playerId password (
).
2.
,
SMS-co-
.
3.
,

MIME- POST-
http://tqs.mamlambo.com/suggest. playerId
( ), question ( ) questionImage (,

),
,
( ).
17.
, :
;
;
, ;
.
Been There, Done That!,
. ,
, .

Android-,
.


Been There, Done That!
. .

. ,
,
,
.

: , ,
MySpace Facebook, ,
. Been There, Done That!
.

,
, . , ,
:
( ) ?

( , ,
, . .)?
( Facebook Twitter),
?


? ,
?
? SMS- ? ,
?
(, Facebook, Twitter . .)
,
- ?
(
)? ,
( )
?

, ,
.
.
(),
,
.
. ,


.

There Pone That!,
, .


,
.
, ,
:
1.
,
.
2.
,
, .
3.
, ,
Scores of Friends ( ) .

_________________________________________________________________
, , ,
.



Been There, Done That!,
QuizSettingsActivity,
. ,
:
,
.
QuizSettingsActivity
, .
,
.

Been There, Done
That!, . ,
.
,
, , .

. 17.1. ,


(. 17.1).
,
,
:
<TextView
android:id=@+id/TextView_Friend_Email
android:layout_width=wrap_content
android:layout_height=wrap_content
android:text=@string/settings_friend_email_label
android:textSize=@dimen/help_text_size
android:textStyle=bold></TextView>
<LinearLayout
android:id=@+id/LinearLayout_Friend_Email
android:orientation=horizontal
android:layout_height=wrap_content
android:layout_width=fill_parent>
<Button
android:id=@+id/Button_Friend_Email
android:layout_width=wrap_content
android:layout_height=wrap_content
android:text=@string/settings_button_friend_email></Button>
<TextView
android:layout_width=fill_parent
android:layout_height=fill_parent
android:textSize=@dimen/help_text_size
android:textStyle=bold
android:gravity=center
android:id=@+id/TextView_Friend_Email_Tip
android:text=@string/settings_friend_email_tip></TextView>
</LinearLayout>

,
TextView Button
Button_Friend_Email.
. , ,
(. 17.2).
XML-
/res/layout/friend_entry.xml:
<?xml version=1.0 encoding=utf-8?>
<LinearLayout
xmlns:android=http://schemas.android.com/apk/res/android
android:id=@+id/root
android:orientation=vertical
android:layout_width=wrap_content
android:layout_height=wrap_content
android:background=@drawable/bkgrnd>

<TextView
android:id=@+id/TextView_Friend_Email
android:layout_width=wrap_content
android:layout_height=wrap_content
android:textSize=@dimen/help_text_size
android:textStyle=bold
android:text=@string/settings_friend_email></TextView>
<EditText
android:id=@+id/EditText_Friend_Email
android:layout_height=wrap_content
android:maxLength=50
android:layout_width=fill_parent
android:maxLines=1
android:inputType=textEmailAddress></EditText>
</LinearLayout>

. 17.2. ,


. -
LinearLayout : TextView, ,
, EditText,
.
,

Button Button_Friend_Email
,
.
, :
Button addFriend = (Button) findViewById(R.id.Button_Friend_Email);
addFriend.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(FRIEND_EMAIL_DIALOG_ID);

}
});

onCreateDialog()

QuizSettingsActivity, case
:
case FRIEND_EMAIL_DIALOG_ID:
final View friendDialogLayout = layoutInflater.inflate(
R.layout.friend_entry, (ViewGroup)
findViewById(R.id.root));
AlertDialog.Builder friendDialogBuilder =
new AlertDialog.Builder(this);
friendDialogBuilder.setView(friendDialogLayout);
final TextView emailText = (TextView)
friendDialogLayout.findViewById(R.id.EditText_Friend_Email
);
friendDialogBuilder.setPositiveButton(
android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String friendEmail = emailText.getText().toString();
if (friendEmail != null && friendEmail.length() > 0) {
doFriendRequest(friendEmail);
}
}
});
return friendDialogBuilder.create();
.
, AlertDialog, .
0 (. 17.3),

FriendRequestTask. ( .)
,
.

. 17.3. ,

ASYNCTASK,


FriendRequestTask,

AsyncTask,

QuizSettingsActivity.

FriendRequestTask execute()
,
.
FriendRequestTask
, AsyncTask,
. (
. .)

doInBackground(), :
@Override
protected Boolean doInBackground(String... params) {
Boolean succeeded = false;
String friendEmail = params[0];
SharedPreferences prefs =
getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE);
Integer playerId = prefs.getInt(GAME_PREFERENCES_PLAYER_ID, -1);
Vector<NameValuePair> vars = new Vector<NameValuePair>();

vars.add(new BasicNameValuePair(command, add));


vars.add(new BasicNameValuePair(playerId, playerId.toString()));
vars.add(new BasicNameValuePair(friend, friendEmail));
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(TRIVIA_SERVER_FRIEND_EDIT);
request.setEntity(new UrlEncodedFormEntity(vars));
ResponseHandler<String> responseHandler = new
BasicResponseHandler();
String responseBody = client.execute(request, responseHandler);
if (responseBody != null) {
succeeded = true;
}
return succeeded;

POST- HTTP,
, HttpClient.
HttpPost
URL ;,
UrlEncodedFormEntity,
POST-.
, execute()
HttpClient, .
, ,
,
ResponseHandler.


, ,
QuizScoresActivity Scores of Friends (
) , . ,
,
.


,
, URL
.
, ,
.
,
ScoreDownloaderTask
Scores of Friends ( ) (. 17.4).
, ,
XML- .

.17.4. Scores of Friends


( )


,
.
, ,
.

, :

, .
Android.
(-, iPhone, BlackBertv . .). ,

.
,
( ,
).
,
,
.

, , ,
... . .

,
, !
, ,
, (, , )
.
.

,
, (
) .
API, -,
REST (Representational State Transfer).
, , Facebook, .
Android-
API,
.
, .
, Android-:

Twitter, ;
,
, Twitter Facebook;

Twitter-.

Twitter Android .
, .
Facebook
Facebook -,
, , .
developers.facebook.com ,
Facebook .
Facebook Platform for Mobile (Facebook Connect, Facebook SMS ..)
wiki.developers.facebook.com/index.php/Mobile.

Twitter
Twitter ,
, .
, 140 , Twitter

. Twitter ,
apiwiki.twitter.com, Twitter API.
OpenSocial
,
API OpenSocial,
wiki.opensocial.org. OpenSocial API ( API,
),
, (
):

friendster (- - );

hi5 ( );

Hyves ( );

Linkedln ( );

Mail.ru ( );

mixi ( );

MySpace ( );

NetLog ( );

orkut ( );

RenRen ( Xiaonei, );

Yahoo! ( );

XING ( ,
).

.

,
. ,
Been There,
Done That!, (

) . ,
, .

:
?
: ,
, .
/ ,
. , ,

. , ,
,
, /.
,
,
API .
: , , ?
: ,
, ,
. ,
: , ,
, , ,
, . ,
, ,
Been There, Done That!, :
, . ( ,
, ,
.) ,
, ,

: ,
, ?
: -
- (, Google,
Amazon, eBay), , API
, - .
,
, ,
. ,
,
, API- .



1.
? Android-
.
2.
Been There, Done That! ?
A.

.
B.

.
C.

.
D.
.
3.
? Android SDK
, Facebook, Twitter MySpace.

1.
.
,
.
,
.
,
, .
2.
B. Been There, Done That!
.
3.
. ,
Android SDK, API,
, Facebook, Twitter MySpace.

1.
Scores of Friends ( ) ,
. (: URL
XML- ,
.)
2.
, ,
,
( , ,
). .

URL,
followers true (, http://tqs.nnamlambo.com/scores.jsp7playerld
s##&followers=true).
3.
API
.
Been There, Done That!,
. ,
Twitter ,
(, X
!).
4.
,
, . .
.

18.

, :
;
, ;
.
Been There, Done That!. ,
,
,
,
.

Android SDK
,
, .
API
, .

.
, ,
,
. ,

. , , ,
,
.
, - ,
.
Been There, Done That!.
:
;
;
;
Been There, Done That!,
.


XML-,
Android. (
.)
, :

. ,
(dp dip),
,
.

, _____________________________________________________
Android ,
74x74 .
, . , .
. ( )
.
. ,
. .
.
.
,
XML- widget_info.xml /res/xml.
:
<?xml version=1.0 encoding=utf-8?>
<appwidget-provider
xmlns:android=http://schemas.android.com/apk/res/android
android:minWidth=146dp
android:minHeight=146dp
android:updatePeriodMillis=10800000
android:initialLayout=@layout/widget>
</appwidget-provider>

,
.
, , , , 146dp,
,
(74dp) . 74dp,
2dp .
74 2 148. 2
146, . ,
.

10.800.000 ,
. ,
, android:
initialLayout="@layout/widget".
.
Android
Android, ,
.
BroadcastReceiver.
, AndroidManifest.xml <receiver>,
,
.
, <receiver>
<application> AndroidManlfastxml:
<receiver
android:name=QuizWidgetProvider>
<intent-filter>
<action
android:name=android.appwidget.action.APPWIDGET_UPDATE />
</intent-filter>
<meta-data
android:name=android.appwidget.provider
android:resource=@xml/widget_info />
</receiver>

<receiver> Android
, . ,
.

. ,
RemoteViews,
,
. , ,
.
RemoteViews , . ,
: - ,
. RemoteViews - .
- -, :

LinearLayout;
FrameLayout;

RelativeLayout;
TextView;
ImageView;
Button;
ImageButton;
ProgressBar;
AnalogClock;
Chronometer.

, , . ,
, ,
,
,
.

. , ,
, ,
RemoteViews.
, widget.xml
:
<?xml version=1.0 encoding=utf-8?>
<RelativeLayout
xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=wrap_content
android:layout_height=wrap_content
android:id=@+id/widget_view>
<ImageView
android:layout_centerInParent=true
android:layout_height=fill_parent
android:layout_width=fill_parent
android:id=@+id/widget_image></ImageView>
<TextView
android:text=@+id/TextView01
android:layout_width=wrap_content
android:layout_height=wrap_content
android:layout_alignParentTop=true
android:layout_centerHorizontal=true
android:id=@+id/widget_nickname></TextView>
<TextView
android:text=@+id/TextView02
android:layout_width=wrap_content
android:layout_height=wrap_content
android:layout_centerHorizontal=true
android:layout_alignParentBottom=true
android:id=@+id/widget_score></TextView>
</RelativeLayout>

, , .
AppWidgetProvider, ,
:
onUpdate() ,
.
onDeleted() ,
.
onEnabied()
, .
onDisabied() ,
.
onReceive() ;

(
onUpdate(),
onDeleted(), onEnabled() onDisabied()).
, .
onUpdate()
RemoteViews.
, . ,
, AppWidgetProvider:
public class QuizWidgetProvider2 extends AppWidgetProvider {
@Override
public void onUpdate(Context context,
AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
WidgetData widgetData = new WidgetData(Unknown, NA, );
getWidgetData(widgetData);
String packageName = context.getPackageName();
RemoteViews remoteView =
new RemoteViews(context.getPackageName(), R.layout.widget);
remoteView.setTextViewText(
R.id.widget_nickname, widgetData.nickname);
remoteView.setTextViewText(
R.id.widget_score, Score: + widgetData.score);
if (widgetData.avatarUrl.length() > 0) {
URL image;
try {
image = new URL(widgetData.avatarUrl);
Bitmap bitmap =
BitmapFactory.decodeStream(image.openStream());
if (bitmap == null) {
Log.w(DEBUG_TAG, Failed to decode image);
remoteView.setImageViewResource(
R.id.widget_image, R.drawable.avatar);
} else {

remoteView.setImageViewBitmap(
R.id.widget_image, bitmap);
}
} catch (MalformedURLException e) {
Log.e(DEBUG_TAG, Bad url in image, e);
} catch (IOException e) {
Log.e(DEBUG_TAG, IO failure for image, e);
}
} else {
remoteView.setImageViewResource(
R.id.widget_image, R.drawable.avatar);
}
try {
ComponentName quizWidget =
new ComponentName(context, QuizWidgetProvider.class);
AppWidgetManager appWidgetManager =
AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(quizWidget, remoteView);
} catch (Exception e) {
Log.e(DEBUG_TAG, Failed to update widget, e);
}
}
private void getWidgetData(WidgetData widgetData) {
}
}

, .
getWidgetData(),
SharedPreferences .
,
SharedPreferences,
getWidgetData() .
RemoteViews.
, ,
. , RemoteViews
, ,
.
(
),
RemoteViews setTextViewText(),
,
.
RemoteViews ,
URL
. ,
RemoteViews setImageViewBitmap()
setImageViewResource().
.
updateAppWidget() AppWidgetManager.


RemoteViews

ComponentName

QuizWidgetProvider,
ComponentName.

_________________________________________________________________
, , ,
onUpdate() appWidgetManager appWidgetIds - . appWidgetManager ,
, AppWidgetManager
. appWidgetIds ,
, .
appWidgetIds, ,
, .

,
, .
,
. 18.1.

. 18.1.

______________________________________

Android , :
1.
.
2.
. (
Been There, Done That! - ).
3.
(
).
4.
Add to Home Screen ( ),
Widgets ().
5.
Choose widget ( ) ,
( ),
.
, 18.2.

. 18.2.
Been There, Done That!
,

,
. ,
, -
.
, .
RemoteView, ,
. -.
,

. ,
, , . ,
, , - .

__________________________________________________________________
, , , Facebook,
EditText. , ,
, .
, .
,
, .
Android SDK
PendingIntent. , , , .
PendingIntent, Intent.
PendingIntent

, ,
.
. PendingIntent ,
RemoteView, setonClickPendingIntent().
updateAppWidget().
Intent launchAppIntent =
new Intent(context, QuizMenuActivity.class);
PendingIntent launchAppPendingIntent =
PendingIntent.getActivity(context, 0, launchAppIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteView.setOnClickPendingIntent(
R.id.widget_view, launchAppPendingIntent);
-,
PendingIntent setClickpendingIntent(),
- RealativeLayout widget.xml. ,
, QuizmenuActivity;
Been There, Done That!.
( , , ,
, ,
QuizMenuActivity .)
, __________________________________________________
-
PendingIntent. updateAppWidget(),
.


, , , ,
,
. , , , .
BroadcastReceiver,
. .
, ,
, .
. ,
Service,
Android, .
Android :
,
,
.
_________________________________________________________________
Thread ? ,
,
: onUpdate(),
, .
, Service, Android, ,
.

. 18.3. Running Services


( )

,
Android , Settings ()
, ) Applications ()
Running Services ( ).
. . 18.3 Running Services
( ).

Service Service,

AndroidManifest.xml. Been There, Done That!-
Service OuizWidgetProvider.
onUpdate() onStartCommand()
Service Thread, ,
.

, __________________________________________________
Service Activity. ,
. , ,
onCreate() onStartCommand(),
onBind(), .
, , onDestroy().
QuizWidgetProvider:
public static class WidgetUpdateService extends Service {
Thread widgetUpdateThread = null;
private static final String DEBUG_TAG = WidgetUpdateService;
@Override
public int onStartCommand(Intent intent, int flags, final int
startId) {
widgetUpdateThread = new Thread() {
public void run() {
// code moved from onUpdate() method
}
};
widgetUpdateThread.start();
return START_REDELIVER_INTENT;
}
@Override
public void onDestroy() {
widgetUpdateThread.interrupt();

super.onDestroy();

@Override
public IBinder onBind(Intent intent) {
// no binding; cant from an App Widget
return null;
}
}

, onUpdate()
run() , onStartCommand().

_________________________________________________________________
, .
Android,
. <service> <application>
:
<service
android:name=QuizWidgetProvider$WidgetUpdateService />

, .

onUpdate() .
: Context.
startService(), Context.bindService().
startService() onUpdate()
:
@Override
public void onUpdate(Context context,
AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Intent serviceIntent = new Intent(context,
WidgetUpdateService.class);
context.startService(serviceIntent);
}

,
, .
. , , , , .
run()
Thread, :
if (!WidgetUpdateService.this.stopSelfResult(startId)) {

Log.e(DEBUG_TAG, Failed to stop service);

stopSelfResult()
, .
, stopSelf().
, ,
,
.
, .
, ,
, .
onDeleted()
AppWidgetProvider:
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
Intent serviceIntent = new Intent(context,
WidgetUpdateService.class);
context.stopService(serviceIntent);
super.onDeleted(context, appWidgetIds);
}

StopService onDestroy()
Service, , ,
.
_________________________________________________________________
, , . . , , , ,
.

Been There, Done That!,


, .
,
. ,
, Been There, Done That!.
,
.

: , ?

: . -.
, .
AppWidgetHost AppWidgetHostView.
: ,
, Button?
:
,

,
, PendingIntent
Intent.
, .
Handling User Interaction with Android App Widgets,
www.developer.com/ws/article.php/3837531/Handling-User-lnteraction-withAndroid-App-Widgets.htm.
: ?
: , Been There, Done That!,
.

.

, .
,
, , .
Handling User Interaction with
Android App Widgets (
).


1.

? .

2.
-, :
A.
B.
C.

Button.
WebView.
ProgressBar.

3.
? ,
,
.
4.

A.
B.
C.

.
. .
.

3.
. ,
AppWidgetHost.
4.
B. Button ProgressBar ,
- WebView .
5.
. 74
,
, 2 . ,
(72 2) 2, 146 .
a. ,
. Thread ,
, ,
. ,
.

3.
Been There, Done That!
, . (XML- ,
,
, , URL .)
4.
,
, .
5.
,
, , ,
.

IV. ANDROID
19.

, :
, Android;
;
;
, .

.

Android; ,
.
, Android, Android Market.
_________________________________________________________________
24 ,
Android Market.

,
,
. . ,
, , , , ,
. , ,
, ,
; , ,
, ;
, ,
, . ,
, (), ()
() (. 19.1).

. 19.1. , ,

().
.
. ,
.
, ,
. , ,

(, Name, , Nom, Nombre).

, ,
.
Android .
! ____________________________________________________________
(. . Java). .
ANDROID
, Android
SDK
. /
Android .
, Android-,
:
, Android (
);
, Android (
,
);

, , Android Market (
, Google ;
).
, Android (
), . 19.1.

19.1

(zh)
(cs)
(nl)
(en)

(fr)

(de)

(it)
(jp)
(ko)
(pl)
(ru)
(es)

(zh_CN)
(zn_TW)
(cs_CZ)
(nl_NL)
(nl_BE)
(en_US)
(en_GB)
(en_CA)
(en_AU)
(en_SG)
(en_SG)
(fr_FR)
(fr_BE)
(fr_CA)
(fr_CH)
(de_DE)
(de_AT)
(de_CH)
(de_LI)
(it-IT)
(it_CH)
(jp_JP)
(ko_KR)
(pl_PL)
(ru_RU)
(es_ES)

, __________________________________________________
. ,

Android ,
developer.android.com/sdk/sndroid-2.1.html#locs.
Android
, Android
. ,
. ,

Deutsch (Deutschland) ( ()).
, ,
English (American) ( ()),
Espacol (Estados Unidos) ( ()) ,
.
Settings
(). ,
.
______________________________________
, :
1.
,
Menu Settings ()
2.
Settings ()
Language & Keyboard ( ).
3.
Select Locale ( )
. Android
. ,
Espanol (), Android
.
,
.

, Android. Android- ,
Android
. . ,
, .
,
.
, , ,
, .
,

Been There, Done That! , . ,


.
, , ,
, (
). Been There, Done That! ,
, .


,
,
ISO 639-1 (
www.loc.gov/standards/iso639-2/php/code_list.php).
,
en, fr, de.
, .
, , Been There, Done That!
, .
:
1.
strings.xml .
,
. ,
, /res/values/strings.xml,
( ).
2.
strings.xml
/res/values-fr/.
3.
strings.xml
/res/values-de/.
Android
. , ,
, /res/values/. ,
( , ,
, ),
( ), .
,
, /res/drawable/,

/res/drawable-de/.

,
( , ,
). !
.
,
,
ISO 639-1 (
www.loc.gov/standanls/iso639-2/php/code_list.php),
, , , ISO 3166-1-alpha-2 (

www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_ele
ments.htm). ,
en-rUS, en-rGB,
en-rAU. ,
.
, Been There, Done That!*
, :
1.
strings.xml . ,
, /res/values/
strings.xml, ( ).
2.
strings.xml
/res/values-en-rGB/.
3.
strings.xml
/res/values-en-rAU/.
. , ,
,
.
, .
, ,
.
! ____________________________________________________________

Android -,
Android,

developer.android.com/guide/topics/resources/resourcesi18n.html#best-match.

Android Market
Android Market ,
Android. Android Market
Google Checkout,
, - .

, Android Market,
market.android.com/support/bin/answer.py?hl=en&answer= 138294.
, Android Market ,
, ,
.
! ____________________________________________________________
Android Merket ,
Android- ( ,

),

market.
android.com/support/bin/answer.py?hl=en&answer=136758.
ANDROID-
, .
. ,
.
, , .
Android- :
;
;
.
.


.
, ,
.

, (, , , .
.), ( ,
, ),
.
. ,
, , ,
, , ,
.
:
.
(
).
:

,
,
.
.

.

.
,
, , .
:
;
(
).
:
-
.
(, ). ,
, ,
,
.
,
, ,
, .
, , ,
,
. ,
,
.

.

, , ,
.

- ,
.
.
:

; .

,
. (
Google.)
:
.

, (
, -
).

! ____________________________________________________________
. .
,
,
. , ,
.

,

Android SDK
. , Locale (java.util.Locale) ,
.


,
Android. getConfiguration()
Context, :
Configuration sysConfig = getResources().getConfiguration();

, Configuration, :
Locale curLocale = sysConfig.locale;

,
.

.
, //
December 8, 1975,
// 8 December, 1975. Android SDK
.
,

DateFormat
(android.text.format.DateFormat) ,
,
, .
... ___________________________________________________
TimeUtils(android.util.TimeUtilsl),
.

,
. Java- Currency
(java.util.Currency) .
NumberFormat (java.text.NumberFormat)
.

,
.
, Android ,
. , Android-
,
.

: Android- ?
: .
; ,
. ,
,
, . , ,
, , ,
. ,
() ,
-.
: Android
, ?

: , Android
,
.
. , ,
,
.
: , ,
?
: , , /,
,
/ , .
, ,
,
. ,
- , , ,
/, ,
( ,
) , ,
, , ( , ).
: . - ?
: , ,
,
, .


1.
? Android-
-.
2.
? ,
Android Android Market, .
3.
,
?
A.
B.
C.
D.

.
.
.
.

1.
.
. Android ,
.
2.
. , Android,
. .
3.
. , ,
, , ,
. ,
,
.

1.
Been There Done
That! .
Android (
/).
2.
Been There, Done That! ,

. ,
, /.
Android
( /).

20.

, :
;
;
Android SDK.
Android .
Android SDK
. , Android .
Android
, Android SDK.
ANDROID


.
:
?
? ? ?
?
?
?

Android SDK ?


Android,
,
.

(. 20 1).
,
/res/values/. ,
, . 20.1.
, .
,
. .

20.1

ru, en, fr, es, zh, ja, ko,


de ..

rRu, rUS, rGB, rFR, rJP,


rDE ..

small, normal, large

port, land

ldpi, mdpi, hdpi, nodpi

noutouch, stylus, finger

keysexposed,
keyshidden, keyssoft

navexposed, navhidden
nonav, dpad, trackball,
whell



ISO 639-1
ISO 3166-1alpha-2,

,

, ,

,

,
,


(
)
,
,


SDK

v1, v2, v3, v4, v5, v6, v7


..

API Level
SDK (,
v1 Android SDK 1.0,
v7
Android SDK 2.1)


.
:
/res/values-en-rUS-port-finger
/res/drawables-en-rUS-land
/res/values-en-qwerty

/res/values-en-rUS-rGB
/res/values-en-rUS-port-FINGER
/res/values-en-rUS-port-finger-custom

,
( , , ),
-, Android,
developer.android.com/guide/topics/resources/providingresources.html#AlternativeResources.

Android- ,
, .
, ,
, .

Been There, Done That!
. ,
. .
, (
,
, ), ,
(. 20.1), .
,
.
:


.
,
.

/res/ layout-land/.

,
Been There, Done That!
( ) .

. 20.1.
( )
________________________________________________________________
, , , ,
// 20.
,
game.xml . 20.2.

. 20.2.
( , )
ImageView, TextView Button
.
, TextView
ImageView.
.
( );
(. 20.3).

. 20.3.
( )

. 20.4.
( , )

, . 20.4.

! _____________________________________________________________
, , ,
, ,
onRetainNonConfigurationInstance() Activity.
no -,

Android,

developer.
androjd.com/resources/articles/faster-screen-orientation-change.html.


. SensorManager
getSystemServi(). ,
getOrientation() SensorManager.

OrientationEventListener(android.view.OrientationEventListener)
onOrientationChanged(),
.

,
. , /res/layout-land/
/res/layout/
, , ,
, .

, ... ___________________________________________________
, Ctrl+F11
Ctrl+F12.


, ,
. ,
Been There, Done That! ,
-
LinearLayout, , ,
, .
, , , - ,
.

. ,
:
. .
- ,
ScroliView ListView.
(
), .
.
,
- RelativeLayout.
- AbsoluteLayout
.
, , NinePatch.
,
.
! ____________________________________________________________
-, Android, ,
developer.android.com/guide/practices/screens_support.html.

Android , ,

, . ,
, .

.
.
,
.
, ... ___________________________________________________
Android SDK and AVO Manager, AVD-,
.
. 20.5, - .
HVGA- ,
Motorola Droid, WVGA854-
.

. 20.5. -

. .
, ,
, :
(
).
,
.
dp sp
pt.
dp
.
(
).
There, Done
That!, ,
, Motorola Droid.

. 20.1, ,
.

,
.
,
,
.
, .

Android SDK

Android SDK: Android 1.1, Android 1.5, Android 1.6, Android 2.0, Android
2.0.1, Android 2.2 Android 2.3.
( Froyo Gingerbread). Google
Android
SDK :

4,7 % Android SDK 1.5.


7,9% Android SDK 1.6.
35,2% Android SDK 2.1.
51,8% Android SDK 2.2.
0,4% Android SDK 2.3.

-,
Android, 4 2011 .

developer.android.com/resources/dashboard/platform-versions.html.

SDK
,
(, 2.1 2.0).
,
,
. , ,
. ,
,

SDK. ,
.
,
, ,
.


,
Android-,
.
?
, , SDK
.
! ____________________________________________________________
SDK Android . ,
SDK .
, ,
Android SDK 1.5,
Android 1.6, Android 2.0. .
SDK
, SDK ,
SDK, ,
Android. ,
SDK,
,
. 20.1.


,
SDK. ,
SDK Android,
SDK. .
. Java
, ,
import. ,
SDK ,
.
-
.
,
, - ,
,
.
, __________________________________________________

Android
SDK

developer.android.com/resources/articles/backward-compatibility.html.
ANDROID SDK

Android ,
Build (android.os.Build). ,
SDK_INT Build.VERSION
android.os.Build.VERSION_CODES.

ANDROID SDK
,
, ,
Android SDK. . 20.1, ,
API Level Android SDK.

,
, .
,
. , ,
Android SDK.


:
. ,
?
: -, ,
Android.
,
.
,
Android SDK,
(Android Open Source Project), ,
( )
, .
,
- - , ,
, , .
. (),
Android SDK,
.
:
,
?
:
. , ,
(
port land). Android
.
, ,
(
,
).


1.
?
: /res/drawables-rUS-en.
2.

?
A.
/.
B.
, ,
.
C.
, .

D.
E.

.
.

. . ,
: /res/drawables-en-rUS.
E. ,
. ,
.
-,

Android,

developer.android.com/guide/topics/resources/providingresources.html#AlternativeResources.

1.
Been There, Done That!
. ,
.
( , -
).
2.
Been There, Done That!
SDK
AVD.

21.
ANDROID
, :
Android;
;
;
;
.

. -
Android SDK.
, ,
, ,
,
.

ANDROID
, , ,
Android
Android SDK.
Been There, Done That!.

Android.

,

,
Android.
<permission>.
Android
-- ,
. .


, , . , ,
, ,
, . 21.!.
.
,
, Android:

;
;

. 20.1.

! ____________________________________________________________
. ,

, .
,
NotificationManager (android.app.NotificationManager).
, Notification
( , ,
) notify() NotificationManager,
.
! ____________________________________________________________
,
. , , ,
Android .

Android
: , .
Android, ,
. Android SDK

, :

;

;

(text-to-speech TTS);

Android SDK
: .
,
.
View,
View. ,
, View ,

TextView EditText, , .
, ,
TextView EditText,
.
- , .
, , TextView,
View,
. View
;

View . ,
setTheme() Activity.
Android.
... ___________________________________________________
Android SDK ,
android.R.style. , android.R.style.Theme , . ,
, .
View ViewGroup
, ,
View, Android SDK.
.
View ( ViewGroup) android.view
,
.
View XML-
.

, TextView
Button.
View
developer.android.com/guide/topics/ul/custom-components.html.

Android (
. 21.2) , . ,
Android SDK
(input method
editors - IME).

. 21.2. Android

, .
, , ,
GestureDetector(android.view.GestureDetector).
GestureDetector onTouchEvent()
.
,
, :
onDown , .
onShowPress ,
, , .
onSignleTapUp , ,
.
onSignleTapConfirmed
.
onDoubleTap , .
onDoubleTapEvent ,
, , .
onLongPress onSingleTapUp, ,
,
, .

onScroll ,
, .
onFling ,
,
, , android.gesture,
, ,
. , ,
,
. SDK
GestureBuilder,
,
.
android.gesture 110
developer.android.com/resources/articles/gestures.html.

Android TTS- (android.speech.tts),

. TTS-
, .
, (Location-Based Services,
LBS) .
, .
TTS- Android , (
), , ,
.
,
.
! ____________________________________________________________
TTS- Android
TTS- ( Android SDK 1.6 ), . ( ,

) .
, Settings ()
Text-to-speech => Install Voice Data ( => ).
. , , .
android.speech.tts.


, (android.speech.
RecognizerIntent). ,
,
,
, .
! ____________________________________________________________
Android SDK 2.1 . ,
- .
.

-
. Android ,
,
. Android SDK
,
( ) .
API android.media.

! ____________________________________________________________
Android . - Android
, . , Android,
, ,
, .

Android SDK
. ,
URI,
. MediaPlayer (android.media.MediaPlayer)

MediaRecorder
(android.media.MediaRecorder) .
android.permission.RECORD_AUDIO.


VideoView
. MediaController
VideoView ,
, (. 21.3).
, MediaRecorder
. ,

android.permission.camera, ,

MediaRecorder,

android.permission.REC0RD_AUDIO, Android.
, ,
Android .

. 21.3.
VideoView MediaController

,
, Android
.

Android
Android SDK android.graphics,
(. 21.4).
graphics Android SDK

. 21.4.
,

Android

.21.5. ,
OpenGL ES
Android

OpenGL ES
Android
OpenGL ES ( 1.0),
OpenGL ES 1.1.
OpenGL ES Android , ,
, -
(. 21.5).

ANDROID
.
, ,
, . Android
.
, , . Android-
. , ,
,
, .

,
RingtoneManager. , Android

(android.permission.WRITE_SETTINGS). ,
, ACTION_RINGTONE_PICKER.


WallpaperManager.
, ,
.
, Android
. , ,
.
. , ,
, , , , - .
, .

Android SDK, android.service.wallpaper.

, Android
.
, JPEG-, ,

Android SDK ( ,
, ).
Android.
, -.
,
.
:

, ;

, - ,
- ,
;
,
;

, , .

_________________________________________________________________

,
, Android (bit.
ly/bngiaP), Cube Live Wallpaper, Android SDK.

,
.
,
SharedPreferences
.

,
:

.

SQLite.
,
-.
,
-.
, SharedPreferences
,


Android- .
Java- java.io,
/, .
Android-
Android. Android Android-
:
/data/data/<a >/

,
.
,
Context .
Context, :
openFileInput() , /files,
.
openFileOutput() , /files,
.
deleteFile () /files,
.
fileList() ,
/files.

getFilesDi() File /files.

getCacheDir() File /cache.

getDir() File
.
, ... ___________________________________________________
Android ( ), File Explorer ()
DDMS.
SQLIte
Android-
SQLite. SQLite
.
Android SDK SQLite. ,
SQLite Android,
android.database.sqlite. ,
:
, ;
SQL-;

Cursor;
;
,
.
... ___________________________________________________
Android SDK SQLite.

android.database
.
SQLite ,

sqlite3, ADB.

, Android- ,
, -.

, -.
-
Android ,
,
, - .
, -
. -
, ,
, , , .
-
URf, . ,
,
,
. , .
URI ,
. -{)
-
, ,
.

-
-, Android,
android.provider. -:
MediaStore (,
) ;
CallLog ,
;

Browser -, ;

Contacts ;

UserDictionary .
, ... ___________________________________________________
-
View, ListView. Adapter,
ArrayAdapter CursorAdapter, View, AdapterView, ListView
Spinner.
-

, -.
, -
- Android.

- ,
, -. ,

,
(
Folders
()). , Activity,
- ACTION_CREATE_LIVE_FOLDER,
ContentProvide,
.
android.provider.LiveFolders.

Android
.
Activity,
,
. ,
,
( Quick Search Box ( )).
,
, , ,

Android

.
, ,
, .
________________________________________________________________
, Android, SearchManager
(android.app.SearchManager),
Searchable Dictionary, Android SDK.



Android
. ,
LBS-, Android SDK API

, :
(,
);

Wi-Fi Bluetooth;

! ___________________________________________________________
Android.
, ,
.
,
, .

, Android SDK:
.
(
).


.
, __________________________________________________
Android ,
Openintents
(www.openintents.org/en/node/23). , , , . ,
, .
SensorManager.
SensorManager
getSystemService().

Wi-Fi
, (ACCESS_WIFI_STATE
CHANGE_WIFI_STATE) Wi-Fi ,
WifiManager.
WifiManager getSystemService().
Android SDK API
Wi-Fi,
Wi-Fi. ,

.
! ___________________________________________________________
Android Wi-Fi, API Wi-Fi .
Bluetooth
Android SDK Bluetooth,
android.bluetooth.
Bluetooth ,
.

.
, BATTERY_STATS,
Intent.ACTION_BATTERY_CHANGED
BroadcastIntent, BroadcastReceiver
- .
,
:

, ( ), ;

( ) ;


USB.
,
. , ,
,
, ,
.

Android.

Android-, ,
,
. ,
. ,
Android,
,
,
OpenGL ES. Android-
SQLite,
, , , -. ,

.

: Android?
: Android .
,
.

Android
http://devetoper.android.com/guide/appendlx/medla- formats.html.

:
, ?
: , ,
.
Android Android Wireless
Application Development.
Android SDK , Android,
https//developer.android.com.


1.
? - Android-
Android.
2.

Android?

A.
B.
C.
D.
E.

.
.
.
.
.

3.
? Android SDK
Android.
4.
? Android SDK,
.

1.
. -
. -.
-, ,
.
2.
E. android.media
. ' Android
,
, , ,
.
3.
. Notification Manager
Android.

4.
. Android SDK . , Android
. , ,
http://developer.android.com. ,
http://andraidbook.blogspot.com, ,
, .

1.

SQLite Been There, Done That!.
2.
,
getSystemService().
android.content.Context.
3.
Been There, Done That!
,
View.
?
4.
Android- ,
, .
Been There, Done That!.

Android, , .
22. ANDROID-
, :
;
;
, .

. , -
, . .
, .
, ,
, , -
!
,
, ,
, .
,
.


. , , ,
. ,
,
. ,
, , ,
. ,
, , .
, ,
24 7 (,
, ).
, ,
0,99 , , , -
? . ,
.
, , ,
,
.
,
:
;


;

).

, ... _________________________________________________

, ,
(
, ).

,
.

,
. , :

;

,
, , ;

. ,
.



Android.
,
Android SDK, .

:

.

.
( ), ,
.
,
, ,
.
, Perforce, Subversion CVS
Eclipse.
, ... ___________________________________________________
- ,
.

, .

, ,
.
,
.

,
. ,
. , . .
, ,
.

Android ,
, .

, ,

, .

. ,
,
, ,
, .
.
Android-
. ,

.
, :

, ,
.
,

. ,
,
(,
, , ).
/
, ,
, .
, ,
, .
Android
, . ,
,
, ,
.



, .
, , ,
, , ,
, .

, ,
.
,
. ,
,

.

, Android SDK,
; ,
,
, .

, ,
,
.
,

. ,
, ,
,
. . , .
Exerciser Monkey,
Android SDK, - .


100%- ,
. , ,
, ,

, ,

,
, .

, .


, .

; .
, ... ___________________________________________________

, .
, ( ) .

, . ,
, .
,
,
.

,
. ,
.
! ____________________________________________________________
,
, , .
,
.

,
, Android .
, ,
,
Android SDK
, (. 22.1).

. 22.1.

,
,
, - .
:
(, ,
Android SDK, , ,
);
( ,
, ..);

, .

*

. ,
Android 1
. :
,
( );
, (,
);

,
(, ,
-
)
!
, Android. AVD-, ,
.
,
. .
( , , LBS-, Bluetooth-), .

, ,
: ,
, .
.

: .
. . .
, ;
.

! ____________________________________________________________
, ,
().
. ,
, ,
, . .
, , .

,
, .
, , ,
, .
! ____________________________________________________________
, ,
, ,

. . , .


, . Android SDK
, .
:

;

JUnit.

,
Log
(android.util.Log), ,
.
Eclipse
Android SDK.
! ____________________________________________________________

, , , .
.

JUNIT ECLIPSE
Android SDK JUnit
Android-.
Java, ,
. .
,
, .

JUnit.
-, www.junit.org.
, ... ___________________________________________________
, , ,
. ,


.
Android-
:
1) ;
2) ;
3) .

. 22.2. ,
Eclipse


There,
Done That!.


, 1, pa Eclipse,
.
,
.
,
. Android Eclipse, :
1.
File => New => Project ( => =>
).
2.
New Project ( )
Android Android Test Project (
Android). Next ().
3.
Test Target ( )
An Existing Android Project (
Android) Browse ().
4.
Project Selection ( )
, , .
,
. 22.2.
5.
Finish ().
.

, ,
. ,
Nickname () ,
QuizSettingsActivity. ,
:
src
.
New => JUnlt Test Case ( =>
JUnit).

Name () New JUnlt Test Case (


JUnit) QuizSettingsActivityTests.

Superclass
()

androld.test.ActivityInstrumentationTestCase2<QuizSettingsActivity>.
(

Superclass does not exist* ( ).)


Class Under Test (
com.androidbook.triviaquiz22.QuizSettingsActivity.

Finish ().
import
QuizSettingsActivity ( import)
, :
public QuizSettingsActivityTests() {
super(com.androidbook.triviaquiz22, QuizSettingsActivity.class);
}

, , Nickname () ,

SharedPreferences, .
setup(),
. EditText Nickname (),
. :
@Override
protected void setUp() throws Exception {
super.setUp();
final QuizSettingsActivity settingsActivity = getActivity();
nickname =
(EditText) settingsActivity.findViewById(R.id.EditText_Nickname);
}

getActivity() ,

ActivitylnstrumentationTestCase2,
.
teardown().
, .
JUnit test. ,
,
test . ,
, Nickname (), ,
SharedPreferences.
QuizSettingsActivityTests, :

public void testNicknameFieldConsistency() {


SharedPreferences settings =
getActivity().getSharedPreferences(QuizActivity.GAME_PREFERENC
ES,
Context.MODE_PRIVATE);
String fromPrefs =
settings.getString(QuizActivity.GAME_PREFERENCES_NICKNAME,
);
String fromField = nickname.getText().toString();
assertTrue(Field should equal prefs value,
fromPrefs.equals(fromField));
}

Android- ,
. Android,
Android- .
. assertTrue() ,
true. ,
. .
.
,
SharedPreferences.
QuizSettingsActivity,
:
private static final String TESTNICK_KEY_PRESSES = T E S T N I C K
ENTER;
// ...
public void testUpdateNickname() {
Log.w(DEBUG_TAG, Warning: +
If nickname was previously testnick this test is
invalid.);
getActivity().runOnUiThread(new Runnable() {
public void run() {
nickname.setText();
nickname.requestFocus();
}
});
sendKeys(TESTNICK_KEY_PRESSES);
SharedPreferences settings =
getActivity().getSharedPreferences(QuizActivity.GAME_PREFER
ENCES,
Context.MODE_PRIVATE);
String fromPrefs =
settings.getString(QuizActivity.GAME_PREFERENCES_NICKNAME,
);
assertTrue(Prefs should be testnick, fromPrefs
.equalsIgnoreCase(testnick));
}

,
Android-, .

, ,
. ;
, ,
.

! ____________________________________________________________
,
, @UiThreadTest
. , , sendKeys()
. ( This method cannot be called from the
main application thread ( ).) , ,
, .

, , .
. .
Eclipse:
Run => Debug As => Android JUnit Test (
=> => JUnit Android). Console ()
Eclipse ,
(. 22.3).

. 22.3. Console () Eclipse


JUnit Android

! ____________________________________________________________
, Eclipse
JUnit,
. ,
Package Explorer
( ) Eclipse
Debug As => Android JUnit Test ( => JUnit Android).
Debug Configurations
( ) Android JUnit Test
( Android JUnit), ,
.
LogCat Android, .
,
, ,
.
JUnit.
, ,
. . 22.4 ,
Eclipse.

. 22.4. JUnit Eclipse


Android-
.
, Dev Tools
Instrumentation ( ).
, ,
android.test.InstrumentationTestRunner. .

( ,
, )
LogCat.
Instrumentation ( ) .

AndroidManifest.xml

<instrumentation> :
<instrumentation
android:targetPackage=com.androidbook.triviaquiz22
android:name=android.test.InstrumentationTestRunner
android:label=TriviaQuiz22 Tests />

, Dev Tools
Instrumentation ( ),
( label), .

,
Android SDK
,
, Android. :
ActivityUnitTestCase
, ,
.
, ,
onPause() onFinished(), . .
.
ApplicationTestCase ActivityUnitTestCase,
Application .
ProviderTestCase2 .

ServiceTestCase .

, ,
(. . , ,
, ),
, ,
.
android.test.

Android, ,
.
,
.
. ,
Android-
JUnit. ,
, ,
.


:
?

Android-

: Android- .
, .
,
.
:
JUnit?
: -, JUnit,
www.junit.org , .


1. ?
Android- .
2. ?
A.
.
B.
.
C.

.
D.

, .
E.

.
F.
.

3.
? 1(1-
.
4.
? JUnit,
Android SDK,
. ''
A.

B.
.
C.
,
GPS.
D.
/
.

1.
. Android SDK
.
2.
F. , , , ,
. () ,

, Android .
(B) ,
Android, ;
,
. (C)
, ,
. (D)
. (E) ,
. . - , ,
.
3.
.
Android, .
4.
C D. , JUnit
. ,
.

,
.

1.
Been
There, Done That!.

2.

.
3.
,
Android- (,
Android SDK Google Maps API).
,
.

V.
23.
, :
;
;
.
, ,
: ,
. ,
Android Market.


(. 23.1). : ,
, , ( ,
), .
, ,
. -
, .
- ,
.
__________________________________________________________________
. .
, .
-,
, ,

. 23.1.
1.
:
2.
.
3.
4.
5.
6.

Android-
- -.
.
.
.

.
-

. ,
, .
.
,

,
Android , .
,
Android-:
, ,
.

,
,
.
Android

, Android.
,
, ,
Android Market.
Android :
, (PNG- )
. ,
.
, .
, .
, .
, (
).
________________________________________________________________
Android SDK
android:versionName . Android Market .
. .
, .
, Android
. -,
.
, uses-sdk.
,
Android SDK, .

API level,
Android SDK. , Android SDK 2.1
API level 7.
... ______________________________________________________
Android Market , .
, ,
, uses-sdk.

debuggable.

, .
, ,
uses-permission, ,
,
.


Android- , ,
,
.
(
), .
,
.
-
,
, - .
, , Eclipse
Run () Debug
()
- . . , ,
,
(, Android Market),
.
- - ,
, ,
. ,
,
(. 23.2).

. 23.2 -

, - ,
,
.
Android ( .apk) .
.
Android Eclipse ,
, !

Android- ,
Android .
Eclipse .
,
. .

! _______________________________________________________________

.
.

Android-, .
, ( ) ,
.

___________________________________________________________________
, , Verisign,
Equifax , , ,
, .
Android- . ,
, , ,
( - ) ,
, ,
( Android) ,
.
)
. Android , ,
, . , ,
, ,
.
... _______________________________________________________
Android .
, ,
.

, Android. ,
Eclipse, :
1. Eclipse
Export ().

2. Export ()
Android Export Android Application ( Android).
3. Next ().
4. Next (). (
,
.)
5. Keystore selection ( )
Create New Keystore (
) (,
) Location (),
( Password ()
Confirm ( )). ( ,
Browse (),
, .)
!
_________________________________________________________________
. , . , ,
.
, ,
.

. 23.3. Android-

Eclipse
6.

Next ().

7.
Key Creation ( ) ,
, . 23.3.
! _______________________________________________________________
Android
( Validity ( )) , 25
. , Android Market ,
, 22 2033 ,
, 25 , .
8.

Next ().

9.
Destination and Key/Certificate checks (
/)
.
10.

Finish ().

, .

, ... ______________________________________________________
(.apk)
keytool jarsigner, JDK,
zipalign, Android SDK.
zipalign ,
Android. Eclipse
zipalign .

, , ,
, , .


Eclipse.
,
.

___________________________________________________________________
,
, .
( ), ,
Menu, Settings (),
Applications (),
Manage Applications ( ),
, Uninstall () , . ,
, , ,
. , ,
, .
( ) (.)

adb. adb:
adb install <___.apk>
, . , , ,
. devices
adb, , :
adb devices

, , , .
:
$ adb devices
List of devices attached
emulator-5554 device
HT9CSP801234 device

,
, -s. , BeenThereDoneThat.apk , :
adb -s emulator-5554 install BeenThereDoneThat.apk

adb
-, Android,
developer.android.com/guide/developing/tools/adb.html.


. , ,
:
, .
, .
, , .
, , ,
, ,
.
- , , ,
. ,
,
!

. ,
, ,
,
. , , .

:
, Android?
: , . ,
Android Market. , , .
Android ,
.
: 22 2033 ?
:
.
, ( ) .
: ?
: , getPackageInto()
PackageManager, . -

PackageInfo, , .


1.
?
.
2.
Android, .
?
A.
B.
C.
D.

inviteid:versionCode.
android:versionLabel.
android:versionName.
android:version.

E.

3.
? , ,
.

1.
. , ,
, ,
, , , ,
.
2.
A C. Android ,
.
3.
. Android ,
.

1.
Been There, Done That!,
, ( ). -
.
2.
Been There, Done That!
, adb.

24.
ANDROID MARKET
, :
Android- Android Market;
Android-;
.
! , ,
Android-.
. ,
Android Market, .
Android , .
, .
ANDROID MARKET
Android Market
Android-. . Android Market , Android. ,
,
Android Market.

Android Market
. Google
Google Checkout,
Android Market (
!? ! !) .
Android Market, :
1. http://market.android.com/publish/signup, .24.1.

. 24.1. Android Market


2. , Google. (
Google, , .)
3. , , . 24.2.
4. (25 ).
,
Google Checkout.

. 24.2. Android Market


5.
Google Checkout.
Android-
.
6.
Android Market
(Android Market Developer Distribution Agreement).
,
Android Market, Google Checkout.
Android Market
, Android
Market , . Android Market,
Upload Application ( ),
. 24.3.

381

. 24.3. - Android Market

. 24.4.

382

. 24.4. ,

, . 24.4
.
. 24.4 , ,
. ,
, .
, .

.
(), -
,
.
50 ,
, ,
,
.

383

All Locations ( ),
, .
,

Android,

http://market.android.com/support/bin/answer.py?hl=en&answer=138294.
,
, Android Market,
. .
Android Market : . Android
Market ,
. .
. ,
Android Market , 30%
. 0,99 200
, .
Copy Protection
( ) On (),

.
, .
.

.
,
( , )
Android-,
, .
!
_______________________________________________________________
(
Learn More ( )) .
Google
- , , , .
, ,
.

384

,
_____________________________________________________
, ,
. , , , ,
, , Android
Android Market
, Publish (),
Android Market .
, ,
, , , ,
All Android Market Listings ( Android Market)
. ,
, , , .
, .
,
. ,
, .
, . ,
. ( 99 200 .)

,
Android API
.
Android Market Google Checkout.
, .
(, , , ),
. , Android,
, , ,
- API , PayPal, Google Amazon. ,
.

385

!
_______________________________________________________________
, Android Market,
.
,
,
Android Market.
.

. .
Android .
, Google AdSense.
Android Market
, ,
Android Market 24- . , 24 , Android Market . , , ,
24
. 15
.
. ,

.

, , ,
, , ,

.
ANDROID MARKET
,
, Android Market.
,
.

Android- Android Market. ,
,
Android.

386

,
,
, .

ANDROID-
Android , , ,
. ,
Android Market,
. ,
,
, Android
Market, ,
, Android
Market,
.
,
_____________________________________________________
, Android, ARCHOS 5 Internet Tablet. , , ( )
, ,
Android.


Android- . ,
, - ,
Android - .

, ,
, ,
.
-
.
: ,
,
. Application Settings
( ) Settings ( ), . 24.5

387

. 24.5. Application Settings


( )
,


URL -
(
). ,
Android , , , , , , .
!
_______________________________________________________________

. , ARCHOS 5 Internet
Tablet
, ( ) ,
.
.

Android Market
Android-. Android ,
( ) - Android- Android, .

388

,
Android-:
PocketGear
,
(http://www.pocketgear.com).
SlideME ,
Android,
, (http://slideme.org).
AndAppStore Android-, (http://www.andappstore.com).
SHOP4APPS Motorola,

Android

(http://developer.motorola.com/shop4apps/).
MobiHand
(http://www.mobihand.com).
,
, ,
.
,
, ,
Google Experience (. ., , Google, Android
Market).
,
. , ? ,
.
, .
, ...
_____________________________________________________

Android- . .

, Android-,
. ,
,
-,
, (

389

, ). ,
Android Market
.
, .
Eclipse !
, . ( .)
!

: Android Market?
: Android Market , . , Android Market .

(en_US);
c / Francais (fr_FR) ;
/ Deutsch (de_DE);
/ Italiano (it_IT);
/ (es_ES);
/ Nederlands (nl_NL);
/ Polski (pl_PL);
/ Ceske (cs_CZ);
/ Portugues (pt_PT);
/ (zh_TW)
/ (ja_JP);
/ (ko_KR);
/ (ru_RU).

:
?
: , Android- ,

. Android Dalvik, Java-
. , Android , ProGuard (http://proguard.sourceforge.net/).
Android Market
() ,
.

390



1.
? Android
Market .

2.

A.
Android Market
.
B.
Android Market
.
C.
Android Market -
Android-.
D.
Android Market , 30$
.
E.
.
3.
? Android- -.
4.
, .Android Market,
?
A.

.
B.
,
,
.
C.
,
,
.Android Market.
D.
, .
E.
Word- .
F.
! ,
.
.

1.
.
Google . Android-
Android Market.
2.
A D. Android Market
Android-
, , 30% .

391

3.
. Androtd-
- , ,

.
4.
! , , /
, . ,

. Android Market
. Android Market
!

1.
Market.

Android

2.
, Android Market
( - Android Market).

Android Market.
3.
,
.

392

VI.
.
ANDROID-

, Android- :
Java Development Kit (JDK);
Eclipse (IDE);
Android Software Development Kit (SDK) ;
, Android.
-
. , , .

Android-
.
, . ,
, .

-, Android,
developer.android.com/sdk/requirements.html.
Android- :

Windows XP ;
Mac OS X 10.5.8 ( x86):
Linux.

2 , Android-.
JDK.
Eclipse, Android SDK .
JAVA DEVELOPMENT KIT

393

Android- JDK 5 JDK


6 Oracle. Java Standard Edition JDK -
Oracle www.oracle.conn/technetwork/java/javase/downloads/index.html
( Java Standard Edition JDK , ).
, .
ECLIPSE
Eclipse Android-; Windows, Mac Linux. Android- Eclipse 3.4
(Ganymede), Eclipse 3.5 (Galileo).
, ... ___________________________________________________
Eclipse , ,
Eclipse IDE for Java EE Developers.
Eclipse Eclipse Java Development Tools
(JDT) Web Tools Platform IWTP).
Eclipse IDE
for Java EE Developers www.eclipse.org/downloads/ ( Eclipse IDE for
Java EE Developers (Galileo SR2 v.3.5.2) ,
)
Eclipse .
Eclipse .

.
Windows
,
Eclipse.exe .

(Target), .
Mac OS X
Eclipse Mac OS X.
README.html,
Eclipse. ,
Eclipse eclipse.ini
, .

394

, ... ___________________________________________________
Eclipse, Android- -,
Android, developer.
android.com/guide/developing/other-ide.html.
ANDROID SDK
Android-
Android SDK. Android SDK JAR- Android
( Android-),
Android, .
Android SDK - ,
Android, developed
android.com/sdk/index.html ( Android SD and AVD
Manager, , , Android
SDK, , ). Android .
Android SDK . , .
Android SDK and AVD Manager 25
, 40 .
SDK . ,

Android,
Android SDK, ,
Android SDK and AVD Manager.
Android ( ). , Android,
, , USB- Windows.

Android SDK and AVD Manager.
ANDROID SDK
_________________________________
, Android SDK Android SDK and AVD Manager ,

395

Android SDK .
, , Android SDK,
(Windows, Linux Mac OS),
Android SDK and AVD Manager Android SDK 2.1 ( ). Android
SDK , 1 Windows):
1.
/Windows/Android SDK/androidsdk_r08- windows.zip, , ,
(, C:\Program
Files\AndroidSDK). Android SDK and AVD
Manager.
2.
/Windows/Android SDK/androidsdk_r08- windows.zip platforms,
android-sdk-windows ( , C:\Program
Files\AndroidSDK\ android-sdk-windows\platforms).
Android SDK 2.1.
3.
android-sdk-windows
platform-tools. platform-tools_r01windows
/Windows/Android SDK/platform-tools_r01-windows.zip
.
4.
Google API Android
SDK 2.1, /Common/Android SDK/google_apis7_r01.zip add-ons, android-sdk-windows.
5.
Android SDK
2.1, android-sdk-windows
samples. /Common/Android SDK/samples2.1_r01-linux.zip (
).
, Android SDK and AVD Manager, ,
Android SDK 2.1, Google API
.
Windows
PATH, tools Android SDK, (My
Computer)
(Properties). Vista
(Advanced System Settings).
(Advanced)
(System Properties)
(Environment Variables).

396

(System Variables) (Environment Variables) PATH , tools Android


SDK.
Mac OS X
PATH, tools
Android SDK, .bash_profile,
m.
Linux
PATH, tools
Android SDK,
-/.bash_profile, -/.bashrc -/.profile.
ANDROID
ECUPW (ADT)
Android Eclipse Android. Eclipse, ,
.
Android- .
Android Eclipse (ADT),
Eclipse .
Eclipse. -, Android, developer.android.com/sdk/eclipseadt.html.
Android Eclipse 3.5 (Galileo),
:
1.
Eclipse.
2.
Help => Install New Software ( - ).
3.
Install ()
Add (). Add Site ( ).
4.

URL
https://dlssl.google.com/android/eclipse/. ,
URL http://dl-ssl.google.com/android/eclipse/.
5.
Install ()
Developer Tools. ( ,

397

, Android DDMS,
Android Development Tools Android Hierarchy Viewer.)
6.
Next () ,
.
Finish ().
7.
, Eclipse.
ANDROID
ECLIPSE _____________
Android Eclipse
. (
Eclipse 3.5 (Galileo)):
1.
Eclipse.
2.
Help=>Install New Software ( =>
).
3.
Install ()
Add (). Add Site ( ).
4.
Archive ().
5.
/Common/ADT Plugin for Eclipse/ADT-8.0.1.zip ,
, .
Name ().
6.
.
7.
Install ()
Developer Tools. ( ,
, Android DDMS,
Android Development Tools Android Hierarchy Viewer.)
8.
Next () ,
.
Finish ().
9.
,
Eclipse.
, Android Eclipse,
Eclipse, , Android SDK.
Eclipse Window => Preferences ( =>
) ( Eclipse => Preferences (Eclipse => ) OS X). Preferences ()
Android , Android SDK. , ,
SDK Location ( SDK) Preferences
(), SDK (Android 1.0, 1.5, 1.6, 2.0, 2.01,
2.1 . .).

ANDROID SDK

398

Android SDK ,
, SDK, . Android SDK , ,
.
SDK Google
:
-
SDK.
API -
SDK.
,
SDK.
Android SDK,
Android SDK and AVD Manager ( Eclipse
Window =>
Android SDK and AVD Manager ( => Android SDK and AVD Manager))
, . Android SDK Android (
AVD- ) Eclipse, .
, Android. ,
Android- SDK.


Android . Android
USB.
Android
USB
USB.
Android, Menu,
(Settings),
(Applications), - (Development),
o USB (USB Debugging).
, ,
- USB Archos 5 Internet Tablet,
Device Storage & USB connection (
USB), USB Connection Mode ( USB) ,
, Debug Bridge (ADB) ( (ADB))

399

-/ .. .),! SDK.
API - -
SDK.
,
SDK.
, ... ___________________________________________________

. , ,
.
, ,
(Stay awake)
- .


Android- , , , -Mobile Gl, Motorola Droid Nexus One,

USB.
Windows. Android SDK , USB (Device Manager)
, Android SDK, Eclipse.
WINDOWS
USB- Android. Android SDK and AVD Manager USB Driver Package.
developer.android.com/sdk/win-usb.html. ,
, USB-
, .
MAC OS X
ac , , ac USB- . .
LINUX
Ubuntu Linux rules-,
:

400

1.

(root).

2.
3.

/etc/udev/rules.d/50-android.rules.
:
1.

Gutsy (7.10) Hardy (8.04):

SUBSYSTEM==usb, SYSFS{idVendor}==0bb4, MODE=0666

2.

Dapper (6.06):

SUBSYSTEM==usb_device, SYSFS{idVendor}==0bb4, MODE=0666

4.

a+rx /etc/udev/rules.d/50-android.rules.

401

.
ECLIPSE -
,
Eclipse.
Android, Java-
Eclipse.
_________________________________________________________________
, Android- Eclipse? ,
androidwirelessdev@gmail.com ( !
endroidbook.blogspot.com .
!

,
,
, New => Class (
=> ). New Java Class ( Java)
, , ,
-
.
-, - >

Source => Override/Implement
Methods ( => / ).
Override/Implement Methods (/ )
, ,
-, , .
IMPORT
,
Import _ ( )
(, _' ( )), Eclipse
import.

402

, Organize imports ( ) (
Ctrl+Shift+O Windows Cmd+Shift+O
) Eclipse import. Eclipse import ,
.

, , Log Android, Eclipse
.
, Eclipse ,
import .
,
.
r , .
. ,
:
1.

Properties ().
2.
Java Editor
( Java) Save Actions ( ).
3.
Enable Project Specific Settings (
), Perform the Selected Actions on Save
( ), . , Organize
Imports ( ).

, , ( ,
). Javadoc
,
. Javadoc
, Ctrl+Shift+J (Windows)
Cmd+Alt+J (Mac).

Source=>Generate Element
Comment
(
=> ), Javadoc-, , ,


,
. -

403

, ,
Ctrl+.
,

.
Javadoc- .
, .
, (, , Ctrl+)
.
, .

,
, , . : , . !
Eclipse! , .
? ,
! , ,
. ,
, . , ,
.

? !
New Editor ( ),
. , ,
.
, , ? !
. -,
Close Others ( ),
, . -,
,
. ( , , , , .) ,
Eclipse, :
1.
Preferences ()
Eclipse.
2.
Preferences ()
General (), Editors ()
Close Editors Automatically ( ).

404

3.
Number of Opened Editors Before Closing
( ).
, 8 Number of
Opened Editors Before Closing ( ),
.
, , When All Editors Are
Dirty or Pinned ( )
Open New Editor ( ),
, ,
, Number of Opened
Editors Before Closing ( ).
, ,
,
.

Rename () Eclipse .
, ,
.
, ,
Refactor=>Rename (=>). . w ,
ALT+Shift+R, . ,
,
Eclipse ,
.
Eclipse ,
, ,
. ,
. !

Eclipse Java-.

,

(, ).
,
Ctrl+Shift+F (Windows) Cmd+Shift+F (Mac).
.
, . , ,
.

405

Eclipse
Properties (), Formatter () Java Code Style (
Java).
. ,
.


,
.
, .
Eclipse: , ,
Outline ().
Outline (),
. , ,
. .
,
Outline (), .

,
, , , ?
TextView nameCol = new TextView(this);
nameCol.setTextColor(getResources().getColor(R.color.title_color));
nameCol.setTextSize(getResources().
getDimension(R.dimen.help_text_size));
nameCol.setText(scoreUserName);
table.addView(nameCol);

, .
, . Eclipse
Extract Local Variable ( ) Extract Method
( ) .
Extract Local Variable ( ), :
getResources().getColcr(R.color.titie_ color).

Refactor=>Extract Local Variable (=> ) ( Ctrl+Alt+L).
,
Replace All Occurrenes ( ), , .

406

1-3 , .
:
int textColor = getResources().getColor(R.color.title_color);
float textSize =
getResources().getDimension(R.dimen.help_text_size);
TextView nameCol = new TextView(this);
nameCol.setTextSize(textSize);
nameCol.setText(scoreUserName);
nameCol.setTextColor(textColor);
table.addView(nameCol);

. ,
?
.
, Extract Method ( ):
1.

2.

Refactor=>Extract Method ( => ) (
Ctrl+Alt+M).
3.

. (
.)
. ,
.
, ,
. ., !

Extract Method ( ). ,
, .
:

addTextToRowWithValues(newRow, scoreUserName, textColor, textSize);

,
!
,
Eclipse .

, Eclipse
, .

407

Eclipse.
-, :
Refresh (), F5.
, R.java,
gen . ( :
.) Compile
Automatically ( ) ,
. ,
.
, .
Project => Clean ( => ) Clean () , . Eclipse
().


Androici- .
, LogCat.
, LogCat.
, ,
. LogCat ,
, . ,
,
.
, Android- ,
.
, . ,
, . :
public static final String DEBUG_TAG = MyClassName;

, .
, ,
,
.

Eclipse ,
.
, Android-
.

408

, Properties () .
,
. Android .
, Eclipse :
, ( ), , , ,
Eclipse. ,
.
,
. , . , Window => Reset Perspective ( => ).

Eclipse
. Eclipse
, ,
, , , ,
.
CVS, Subversion, Perforce, git .
, .
Android- bin gen,
.

Eclipse, Preferences (), Team ( ; Ignored Resources
( ). Ignore Patterns ( ) *., *. *.dex, Add Pattern ( ).

409

Вам также может понравиться