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

Android

D_Android_title.indd 1

01.11.2011 13:12:37

Android

Application Development

by Donn Felker
with Joshua Dobbs

D_Android_title.indd 2

01.11.2011 13:12:38

Android

D_Android_title.indd 3


2012

01.11.2011 13:12:38

32.973.26-018.2.75
38
681.3.07

..
. ..
. . ..
:
info@dialektika.com, http://www.dialektika.com
, .
38
Android: . : . . . :
.. , 2012. 336 . : . . . .
ISBN 978-5-8459-1748-5 (.)
32.973.26-018.2.75
.

, ,
, Wiley Publishing, Inc.
Copyright 2012 by Dialektika Computer Publishing.
Original English language edition Copyright 2011 by Wiley Publishing, Inc.
All rights reserved including the right of reproduction in whole or in part in any form. This translation is published
by arrangement with Wiley Publishing, Inc.

Android:

..
..
..
..

08.11.2011. 70x100/16
Times.
. . . 27,09. .-. . 18,73.
1500 . 0000


199034, -, 9- . ., 12.
. . , 127055, . , . , . 43, . 1
ISBN 978-5-8459-1748-5 (.)
ISBN 978-0-470-77018-4 (.)

Android Application Development For Dum.indb 4

- , 2012,
, ,
Wiley Publishing, Inc., 2011

08.11.2011 12:23:45

16

I. Android
1. Android
2.

21
23
38

II. Android
3. Android
4.
5.
6. Android
7.
8. Android Market

65
67
101
123
160
168
190

III.
9. ,
10.
11.
12.
13.
14.
15.

211
213
230
238
256
283
294
301

IV.
16.
17. ,

315
317
321
325

Android Application Development For Dum.indb 5

08.11.2011 12:23:49

15
15


,


I. Android
II. Android
III.
IV.
,

!

16
16
17
18
18
18
18
19
19
19
19
20

I. Android

21

1. Android
Android



Android
Java Android







GPS

SD

23
23
23
24
24
25
25
26
27
27
28
29
29
30
31
31
33
33
33
34
34
34
35

Android Application Development For Dum.indb 6

08.11.2011 12:23:49

Google API

35
35
35

2.
Android

Android
Linux 2.6
Android

OHA
Java


JDK
JDK
JDK
Android SDK
Android SDK

Eclipse
Eclipse
Eclipse
Eclipse
Android
Android SDK
Android
SDK

38
38
39
39
39
40
41
42
43
44
44
44
45
46
46
47
48
48
50
53
53
54
56
59
60
60
61

II. Android

65

3. Android
Eclipse


Build Target Min SDK Version

67
67
73
73
74
76
79
79
79

Android Application Development For Dum.indb 7

08.11.2011 12:23:49








default.properties

82
83
83
88
89
89
97
99

4.
Silent Mode Toggle

XML








XML





101
102
103
105
107
108
108
110
111
111
112
112
114
117
117
118
119
120
121

5.

,


onCreate
Bundle



Android

AudioManager

123
123
124
125
128
129
129
129
130
131
133
134
135

Android Application Development For Dum.indb 8

08.11.2011 12:23:49



Android


DDMS
Eclipse


139
139
141
143
143
143
144
144
148
156
157
157

6. Android

160
160
160
161
162
162
162
162
163
163
164
165

7.
Android

AppWidgetProvider

Android




AppWidgetProvider


AppWidgetProvider


168
169
170
171
172
172
173
174
175
176
176
178
179
180
185
186
188

Android Application Development For Dum.indb 9

08.11.2011 12:23:49

8. Android Market




APK
Android Market




Android Market

190
190
191
192
192
193
196
202
203
203
203
205
209

III.

211

9. ,















213
213
214
214
214
215
215
216
217
218
222
223
223
226
226
227
228

10.


XML




XML

230
231
231
231
232
234
234
235
235

10

Android Application Development For Dum.indb 10

08.11.2011 12:23:49

236
236

11.

EditText









238
238
238
240
240
240
241
246
248
249
250
251
253
254
255

12.






SQLite
SQLite
Java

SQLite


SQLite

RemindersDbAdapter

SimpleCursorAdapter

256
256
256
258
258
259
259
260
260
261
261
262
263
265
265
266
268
274
277
277
278

13.
AlarmManager

283
283

Android Application Development For Dum.indb 11

11

08.11.2011 12:23:49

AlarmManager
ReminderManager
OnAlarmReceiver
WakeReminderIntentService
ReminderService


284
285
286
287
290
291
291
293

14.








294
294
294
294
296
297
299
299
300
300

15.







PreferenceActivity
PreferenceActivity



301
301
302
302
303
304
304
306
307
309
309
310
310
313

IV.

315

16.
Foursquare
LOLCat
Amazed
API-
MultiResolution

317
318
318
318
319
319

12

Android Application Development For Dum.indb 12

08.11.2011 12:23:49

Last.fm
Hubroid
Facebook SDK Android
Replica Island
SQLite

319
319
320
320
320

17. ,
droid-fu
RoboGuice
DroidDraw
Draw 9-patch
Hierarchy Viewer
Application Exerciser Monkey
zipalign
layoutopt
Git
Paint.NET GIMP

321
321
321
321
322
322
322
322
323
323
323

325

Android Application Development For Dum.indb 13

13

08.11.2011 12:23:49

Android Application Development For Dum.indb 14

08.11.2011 12:23:49



, . Agilevent,
Microsoft ASP Insider, MCTS (.NET Framework 2.0/3.5 Web Applications) ScrumMaster.
TekPub.com Introduction to Android. blog.donnfelker.com,
@donnfelker.


- , .
-.
Android ,
. Android
6 . Google Android Device Seeding Program. - www.joshdobbs.com.

Android Application Development For Dum.indb 15

08.11.2011 12:23:49


... , Android! , Android, . :
? , ! ,
, Android.
, Android
, , .
Google Android 2005 ( ), , ,
. , Google , ,
, , ,
G1
Android. , ,
, .
Android , G1.
Android, .
, iPhone,
Android. Donut (Android 1.6), , Google
. 1.6 2.0, , .
2.2, 3.0 1. , . , Android. ,
, .


. ,
Android - Android. , .
Android. -
. :
Eclipse
. , Java. , , Java
. .
1 , ,
2.3, 3.0. . .

Android Application Development For Dum.indb 16

08.11.2011 12:23:50

, , .
, Android XML,
, Eclipse XML,
. , Eclipse , ,
. gray () red ()
green (), , Android.
Android
, , , , ,
GPS (Global Positioning System ) . , ?! Google
Android.
, , Android, .
,
. Android. ,
, .
. ,
Android .

,

Android Java XML.
,
. .
public class MainActivity

Java , ,
. , Java,
, Android
SDK (Software Development Kit ).
, PascalCase.
m.
URL-, :
http://d.android.com

- , http://github.com/donnfelker.
, .
http://www.
dummies.com/go/androidappdevfd.

Android Application Development For Dum.indb 17

17

08.11.2011 12:23:50


Android , :
 Windows XP (32-), Vista (32- 64-)
Windows 7 (32- 64-);
 Mac OS X (Intel) 10.5.8 ( x86);
 Linux (i386).
, Windows,
, :
.
Android SDK JDK (
). 2.
Android Java,
Java. , Android
XML,
XML. , XML Java.
(, C#), .
Android, Android .
, ,
, .

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

II. Android
Android
.
Android
. , Android Market (, , , , ).

18

Android Application Development For Dum.indb 18

08.11.2011 12:23:50

III.
III , , ,
( , ). SQLite . ,
Android ,
.

IV.
,
Android. , , , . Android,
.

,
,
.

, ,
.

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


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

Android Application Development For Dum.indb 19

19

08.11.2011 12:23:50

!
, , . , ,
.
.
. . , , , ,
.
, , .
.
:
info@dialektika.com
E-mail:
http://www.dialektika.com
WWW:
:
:
127055, , . 45, . 1
:
03150, , / 152

20

Android Application Development For Dum.indb 20

08.11.2011 12:23:50


Android
The 5th Wave

,
.

Android Application Development For Dum.indb 21

08.11.2011 12:23:50


I Android
. ,
Android SDK Android,
Android SDK.

Android Application Development For Dum.indb 22

08.11.2011 12:23:50

Android
...





Android
Android

2005 Google Android,


.
, ? Google, ,
. Google
Android, . 2010 160 Android, ,
, Android 2008 .
Google !

. Android ,
Google .
Android Market, Google, . ,
.


Android
? , ?
, ? ? ?
, , Android. , ,
, .


,
, , . , Android

Android Application Development For Dum.indb 23

08.11.2011 12:23:50

.
, . Android Market .
, . Android Market
Android ( , ),
, Android Market. Android Market ,
.

Android API
(Application Programming Interface ).
. Android Market,
. , Android Market
. .

, Google
. , Android
, , .


Android . ,
.
Android . Android. Android http://source.android.com
.
, , .

. .
Android

, Android Google. .
Android
Android Inc.,
. 2005 Android
Google.

24

Android Application Development For Dum.indb 24

Android Inc.
, Danger,
Wildfire Communications, T-Mobile WebTV.
Google , ,

.

I. Android

08.11.2011 12:23:50


Android
. ,
, Android , , .
Google : , . , , Android Market
, . .
Android Market 8.
Android , (
):
 ;
 ;
 GPS- (Global Positioning System );
 Bluetooth.
htp://source.android.com/compatibility/
overview.html. ,
, .

(mashup) . , , , .
, .
API, Android, . ,
Google Maps API
.
, . , , , .
 .
. , ,
. .
Android

1. Android

Android Application Development For Dum.indb 25

25

08.11.2011 12:23:50

Twitter API
(, iTwitter).
 .
. ,
, . ,
.
,
.
Android
GPS .
 . Android API,
. , , .

.
.

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

, , .
!


, ,
.

Android
Android, .
, . Android
, Java. Java Android. ,
Java, .

26

03ch01.indd 26

I. Android

08.11.2011 12:36:40

, , Java
- , Java , 5- . Java,
: , ,
, .
, Java ( , ) .
,
.
Android Java,
XML
Apache Ant. , ,
XML. XML,
, .
XML. Ant ; . Android.
XML Java, . Android.

Java Android
Android Java,
Java, J2EE,
Java, Dalvik. , Dalvik, ,
.
- Java,
, Google. , Google Java. Java ,
.
Dalvik .
Android, , . Android.

Android (activities).
.
, , . Windows
.
3 5.

1. Android

Android Application Development For Dum.indb 27

27

08.11.2011 12:23:50


(intents) Android.
, (, , ..)
. (action) ,
, ,
. , .
Android. ,
.


, Android
-. ,
Android
.

,
- . ,

Android, .
,
.
(image
gallery).
.
. ,
. 1.1.
, . , (. 1.1), , .
!


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

28

Android Application Development For Dum.indb 28

I. Android

08.11.2011 12:23:50


,
, Android .
?
Android ( ). ,
.
, . ,
. , ,

, , .
, .


,
, . ,
, , , .
:
 ContextMenu ( );
 Menu ();
 View ();
 SurfaceView ( ).
, . , , ,
.
, . :
 Button ();
 CheckBox ();
 DatePicker ( );
 DigitalClock ( );
 Gallery ( );
 FrameLayout ( );
 ImageView ( );
 RelativeLayout ( );
 PopupWindow ( ).

1. Android

Android Application Development For Dum.indb 29

29

08.11.2011 12:23:50

Android . android.widget Android :


http://developer.android.com/reference/android/widget/
package-summary.html

,
.


AsyncTask Android , .
AsyncTask , ,
. .
(thread) , . , , , ,
, .
,
. , , ..
.

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

, ,
. , ,
, , .
, ,
. , ,
, .
, Android
ANR (Application Not Responding

30

Android Application Development For Dum.indb 30

I. Android

08.11.2011 12:23:50

), . 1.2. (
Android), ANR.
, , : Force
close ( ) Wait (). ,
ANR .

, .

. 1.2. ANR

, .
, . , ,
Android :
http://developer.android.com/guide/practices/design/
responsiveness.html


Windows, , , , .
,
. , ,
- -.
, , .
, Android Market, .
.


Google ,
. ,
Google .
Google .

1. Android

Android Application Development For Dum.indb 31

31

08.11.2011 12:23:50

Android, . .
,
, .
. 1.1 , .
1.1. Android

GPS

Bluetooth?

Bluetooth

Android , . : Android

. ,
.
, , Android. : ,
, , Android. , Evo 4G
Sprint Android,
. , Sprint
SDK (Software Development Kit ), .
, Sprint ,
.
Android , : , , ..
Android, Google TV, ,
. Android ,
.
Android . 4.

32

Android Application Development For Dum.indb 32

I. Android

08.11.2011 12:23:50


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

GPS
Android GPS (Global Positioning
System ), ,
. , , . Foursquare, , GPS ,
, (, , , )
.
Google Map ,
, . Android GPS , , .
Android, .

. Android . , , ?
, , .
Android
. , . . : , .
. - :
, .

1. Android

Android Application Development For Dum.indb 33

33

08.11.2011 12:23:50

SD
Android
SD, .
Android 2.2 SD ,
. . ,
- , ,
.
(, , , .), ,
.
SD. , .
SD .
,
SD . , .


Android, .
,
Android .

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

-
, . ,
, .

Android. - .
Adnroid,
.

34

Android Application Development For Dum.indb 34

I. Android

08.11.2011 12:23:50

.
- , Google Maps GPS.


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


, . , . Contacts, ,
. ,
GPS, ,
, .
, . (.
).

, . , - ,
,
. ,
, . , SD.
. , SD. , .
,
.
, (. 3).

Google API
, Android , .

1. Android

Android Application Development For Dum.indb 35

35

08.11.2011 12:23:51

. ,
, . Google , .


, ,
. , , . Google Android Maps API, .
. Maps API
, . :
, , , , .

Android
, Google :
Google Navigation API.
, .


,
, , .
.

, , ,
. , ,

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

36

Android Application Development For Dum.indb 36

.

,

.
, , , ,

.

.
,
,
-, ,
.

Android, ,

. .
, -

I. Android

08.11.2011 12:23:51

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

.

.
Android
( ),
,
. ,
, ,
.


, ,
.
, . , Android - . ,
. ,
, ?
, ,
. , . . , - , .
- , . ,
.
, Android
. : .

1. Android

Android Application Development For Dum.indb 37

37

08.11.2011 12:23:51


...









Android



JDK
Android SDK
Eclipse
Android

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


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

Android Application Development For Dum.indb 38

08.11.2011 12:23:51

Android Market,
.
. 25 .
, ,
,
. , ,
Android Market, , , .
, .


, Android, .
Android, ,
.

Android
, Android (Open Source). , ,
. , , , , , Android ,
,
. , .
Android Git. ,
http://source.android.com.

Linux 2.6
Android Linux 2.6.
Android , , ,
Android. Linux 2.6.
 . Linux
.
 . , .
 . .
 . Linux .

2.

Android Application Development For Dum.indb 39

39

08.11.2011 12:23:51

 .
Linux,
.
Linux 2.6 . 2.1.
Linux

IPC

Wi=Fi

. 2.1. Linux

Android
Linux 2.6 Android,
. . Android.
 Android. Java Dalvik.
 . - API (Application Program Interface
).
 WebKit. -,
- .
 SQLite. , .
 . , .
 SSL (Secure Sockets Layer ). , -.
. 2.2 Android.

Android

Surface
Manager

Media
Framework

SQLite

OpenGL | ES

FreeType

WebKit

Dalvik

SGL

SSL

libc

. 2.2. Android

40

Android Application Development For Dum.indb 40

I. Android

08.11.2011 12:23:51



Android. , Android SQLite
. Android

. Android.
Android, .
 Activity manager ( ).
.
 Telephony manager ( ).
,
.
 View system ( ).
.
 Location manager ( ).
.
. 2.3 , .

. 2.3. Android

Android, API, ,
, . Android . 2.4.
Android ,
. Android , ,
.
Android, . ,
,
. ,
Android, ,
- ,
.

2.

Android Application Development For Dum.indb 41

41

08.11.2011 12:23:51

Home

...

Android

Surface
Manager

Media
Framework

SQLite

OpenGL | ES

FreeType

WebKit

Dalvik

SGL

SSL

libc

Linux

IPC

Wi=Fi

. 2.4. Android

OHA
OHA (Open Handset
Alliance )?
? , ,
. , ,
.
OHA 2007 .
34 . OHA Google.
OHA 71 , .
OHA
. OHA www.openhandsetalliance.com.
OHA , , T-Mobile,
Spring, LG, Motorola, HTC, NVidia Texas Instruments.
( ) OHA , Android . OHA Android.
Android,

42

Android Application Development For Dum.indb 42

I. Android

08.11.2011 12:23:51

,
, ..
OHA Android.
OHA Android.
OHA Android,
,
.
. ,
GPS, Bluetooth, Wi-Fi .. . , Android. OHA
, ,
. , ( ) , .
,
, , , .
Barnes & Noble Nook
Android. Forward () Back (), Android. ,
Barnes & Noble Nook,
, ,
Android.

Java
Java , Android .
, Java. ,
, , .
, , .
Java . JVM (Java Virtual Machine
Java) Android Java. .
,
.
Java .
Java, , . Android Java

2.

Android Application Development For Dum.indb 43

43

08.11.2011 12:23:51

. , ,
,
- Java, Java , 5- .


Android , Windows, Linux Mac OS X. , Windows 7.
,
, .


Android :
 Windows XP (32-), Vista (32- 64-) Windows 7
(32- 64-);
 Mac OS X 10.5.8 (
x86);
 Linux ( Linux Ubuntu Hardy
Heron).
, 64- 32- .

64- Windows 7.
Windows,
, . Linux Mac, ,
: C:\path\to\file.txt, : /path/to/file.txt.

, Android, ,
. , . , ,
. ,
, . . Eclipse ,
. Pentium D, 1,6
1 . Windows XP Windows 7.
Eclipse .

44

Android Application Development For Dum.indb 44

I. Android

08.11.2011 12:23:51

Android
, .
Android:
http://developer.android.com/sdk/requirements.html

,
Android. ,
3 .
, ,
.



, Android.
 Java JDK. .
 Android SDK. Android
Android.
 Eclipse. , Java,
Android SDK Android ADT (Android Development Tools Android). Android.
 Android ADT. Eclipse, , , Android.
, .

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

2.

Android Application Development For Dum.indb 45

45

08.11.2011 12:23:51

JDK
Java SDK (Software Development Kit
), Google , , JDK (Java Development Kit
Java).
JDK ,
.

JDK
.
1. http://java.sun.com/javase/
downloads/index.jsp.
Java SE.
2. JDK Java Platform (JDK), . 2.5.
JDK ( 6u26).
Mac, JDK
.

JDK

. 2.5.

46

Android Application Development For Dum.indb 46

I. Android

08.11.2011 12:23:51

3.
4.
5.

6.


Java SE, (Windows, Linux Mac),
.

Download ().
. Skip
This Step ( ), .
JDK-6u26windows-i586.exe ( Windows).
, . (. 2.6).
.
.

. 2.6. JDK

, , -,
. 2.5, . , ,
http://developer.android.com/sdk/requirements.html.
. , ...
JDK. JRE (Java Runtime
Environment Java) , JDK.
!

, JDK .
Android 2.2. Android 2.2
JDK 5 6. Java ( Android),
. , :
5 . 6,
.

JDK
,
, JDK. , ,
. (

2.

Android Application Development For Dum.indb 47

47

08.11.2011 12:23:51

, ).
.
. ! JDK .

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

Android SDK
Android SDK, .
1. http://developer.android.com/
sdk/index.html.
2. SDK .
3. SDK .
SDK .
, E:\android.
4. android Windows SDK
Manager.exe (. 2.7).

. 2.7.

5. , Yes ().
Android SDK and AVD Manager (
Android SDK AVD).
6. , . 2.8.
Choose Packages to Install ( ). . -. Cancel (). Android SDK and AVD Manager.
Available packages ( ) .

48

Android Application Development For Dum.indb 48

I. Android

08.11.2011 12:23:51

. 2.8.

2.2 (. . 2.8).
.
, .
Android 2.2 API 8, Samples for SDK
API 8, revision 1, . 2.8.
Android Google
SDK, ,
Android. ,
. , Bluetooth, Android SDK 2.0
, Bluetooth.
7. Install ().
Installing Archives ( ),
(. 2.9).

. 2.9.

2.

Android Application Development For Dum.indb 49

49

08.11.2011 12:23:51

8. , Close ().
android-sdk-windows (. 2.10). ,
.

. 2.10. Android SDK

Android SDK , Failure to fetch


URL ( URL). :
.
. ,
Android SDK and AVD Manager (. ), Settings ()
Force https://... sources to be fetched using http:// ( https:// http://).
.
Android NDK
Android NDK (Native Development
Kit ) ,

Android, .. , , C C++.
NDK, SDK. NDK
SDK, .


,
Android Windows.
ADB (Android Debug Bridge Android) .

50

Android Application Development For Dum.indb 50

I. Android

08.11.2011 12:23:51

ADB Android

. ADB;
.
Android.
Android Windows.
.
1. Windows .
2. (. 2.11). .

. 2.11.

3. (. 2.12). (. 2.13).
4.
.
.

. 2.12.

2.

Android Application Development For Dum.indb 51

51

08.11.2011 12:23:52

. 2.13.

5. ANDROID (. 2.14).
6. , Android. ,
E:\android\android-sdk-windows\tools.

. 2.14. ANDROID

7. OK, .
8. Android Path.
(. 2.15)
.
9. Path ;%ANDROID% ( , ).

52

Android Application Development For Dum.indb 52

I. Android

08.11.2011 12:23:52

. 2.15. Path

Android , ANDROID.


, . .
. Path ,
.

Eclipse
, SDK, Eclipse.
.

Eclipse
!

Eclipse.
Android Eclipse Helios
( 3.6). - http://developer.
android.com/sdk/requirements.html ,
Eclipse Helios Android ,
.

2.

Android Application Development For Dum.indb 53

53

08.11.2011 12:23:52

http://eclipse.org/downloads. Eclipse (. 2.16). Eclipse IDE for Java


Developers ( Java). Windows 32 Bit ...64 Bit eclipse-javahelios-SR2-win32.zip. .
, Eclipse IDE for Java EE Developers. , . .
Eclipse Galileo Older Versions ( ),
. 2.16, Eclipse Galileo.

. 2.16. Eclipse

Eclipse
Eclipse .
, eclipse-javahelios-SR2-win32.zip . , E:\Program Files\Eclipse. Eclipse
, eclipse.exe.
, .
.
1. Eclipse, .
Eclipse Windows ,

54

Android Application Development For Dum.indb 54

I. Android

08.11.2011 12:23:52

.
Eclipse. .
2. Eclipse.
Eclipse Workspace
Launcher ( ), . 2.17.
Browse (), ,
,
E:\Eclipse_Workspace. ( . 2.17) OK.

. 2.17.

, .

, , . ,
,
.
OK Workspace Launcher Eclipse (. 2.18).

. 2.18. Eclipse

2.

Android Application Development For Dum.indb 55

55

08.11.2011 12:23:52

3. .
Workbench ( )
Welcome ( ).
Eclipse .
Eclipse ADT, Android.

Eclipse
ADT (Android Development Tools Android)
Eclipse , Android. , Android,
.
,
. , ADT Eclipse , Android SDK. ,
Eclipse APK (Android Package Android),
. APK , ,
.
APK , Eclipse.
APK 8.

ADT
Eclipse ADT, .
1. Eclipse.
2. HelpInstall New Software (
).
Install (. 2.19).
3. , .
Add ().
Add Repository ( ), . 2.20.
,
. Eclipse ADT ,
.
4. Name () . , Android ADT, .
5. Location () https://dl-ssl.google.
com/android/eclipse/.
6. OK.
7. Android ADT Work with ( ), . 2.19. , , Name () Version ().

56

Android Application Development For Dum.indb 56

I. Android

08.11.2011 12:23:52

. 2.19. ADT

. 2.20.

8. Install ,
(. 2.21), Next ().
9. , Android DDMS
Android Development Tools, .
.
(. 2.22) . Next, , Back
(), - .
10.
Next.
11. Finish ().

2.

Android Application Development For Dum.indb 57

57

08.11.2011 12:23:52

12. Eclipse, Restart


Now ( ).
ADT .

. 2.21.

. 2.22.

58

Android Application Development For Dum.indb 58

I. Android

08.11.2011 12:23:52

Android SDK
Android SDK Eclipse. . Eclipse
. .
1. Eclipse WindowPreferences ().
Preferences (. 2.23).
2. Android.
3. SDK Location ( SDK) E:\android\android-sdkwindows.
4. OK.

. 2.23. Android SDK

, Eclipse , Android.
https://dl-ssl.google.com/android/eclipse (
), , s. :
http://dl-ssl.google.com/android/eclipse.


Android
,
Android. ,
.

2.

Android Application Development For Dum.indb 59

59

08.11.2011 12:23:52

Android SDK
Android SDK (E:\android\android-sdk-windows), , . Android SDK,
. . 2.1
.
2.1. Android SDK

usb_driver

Android. Android
, , , ADT. usb-driver
, USB

tools

, , , , ..

temp

SDK. SDK
.
temp

samples

, .

platforms

Android, . . Android 2.2 android-8,


Android 1.6 android-4 ..

docs

Android SDK

add-ons

API , .
Google, .
, add-ons

Android
Android Android.
, 1.1 2.2. Eclipse .
, ,
. , , . ,
. ,
Bluetooth, 2.0,
2.0 Bluetooth .

60

Android Application Development For Dum.indb 60

I. Android

08.11.2011 12:23:52

. 2.24 Android 1 2010


. :
http://developer.android.com/resources/dashboard/platform-versions.html
Android 2.1

Android 2.2

Android 1.5
Android 1.6

. 2.24. Android 1 2010

SDK
, , Android. , .
.


. Google
, ,
. , . , ,
( , , ).
,
.
, . , , Bluetooth,
Bluetooth ,
Bluetooth. ,
, . , , , . ,

2.

Android Application Development For Dum.indb 61

61

08.11.2011 12:23:52

,
.
. Eclipse .
,
.
,
.

Android
,
. ,
, DDMS (Dalvik
Debug Monitor Server Dalvik),
ADT (. . 2.22). , , , . ,
, . ,
, .
.
Windows, USB. Mac Linux ,
.
Windows USB Android, .
1. Eclipse WindowAndroid SDK and AVD
Manager ( ).
Android SDK and AVD Manager (. 2.25).
2. Available Packages ( ).
3. Google USB Driver package (
USB Google), . 2.25. .
4. Install Selected ( ).
Choose Packages ( ).
5. Accept (), , Install.
.
6. , Close ().
7. Android SDK and AVD Manager Installed Packages ( ).
USB (. 2.26).
Android SDK and AVD Manager.

62

Android Application Development For Dum.indb 62

I. Android

08.11.2011 12:23:52

. 2.25. ,

. 2.26. USB


USB DDMS , . , . , DDMS ,
, , SMS
GPS. DDMS :
http://developer.android.com/guide/developing/tools/ddms.html

2.

Android Application Development For Dum.indb 63

63

08.11.2011 12:23:52

SDK API
, API
SDK, Android , :
http://developer.android.com/resources/samples/index.html

- , , . , , Bluetooth,
, ..
SDK. samples,
, . .
Android Eclipse.
samples , API. , , , , , .
:
http://developer.android.com/resources/samples/ApiDemos/index.html

64

Android Application Development For Dum.indb 64

I. Android

08.11.2011 12:23:52

II


Android
The 5th Wave

, ,
.

05ch03.indd 65

08.11.2011 12:40:54


II,
Android. , , . , ,

Android Market . , .

05ch03.indd 66

08.11.2011 12:40:56

Android
...

Android, ,
,
? ! , ,
. ,
Android
.
, !, . ? ? . Eclipse
. , .

Eclipse
Eclipse.
(. 3.1). .
Android.
2 Eclipse ADT (Android
Development Tools Android). ,
Eclipse Android , , . ,
Android Eclipse. .
Android, .
1. Eclipse FileNewProject ().
New Project ( ),
. 3.2 .
, .
2. Android.

05ch03.indd 67

08.11.2011 12:40:56

. 3.1. Eclipse

. 3.2.

3. Android Project, , Next ().


New Android Project ( Android),
. 3.3.

68

05ch03.indd 68

II. Android

08.11.2011 12:40:56

. 3.3.

4. Project Name ( ) Hello Android.


. Eclipse. Eclipse .
5. Contents () ( )
Create new project in workspace (
). Use default location ( , ).
,
, 2 Eclipse. Contents ,
. ,

Android.
Eclipse

(. 3.4). .

,
, Use default location ( , ).
Location (). Browse ()
, .

3. Android

05ch03.indd 69

69

08.11.2011 12:40:56

. 3.4.

6. Build Target ( )
Android 2.2.
(..
Java), .
2 Android 2.2,
. , . , Android 2.2
,
.
7. Application Name ( ) Properties () Hello Android.
Android.
. .
- .
8. Package Name ( ) com.dummies.android.helloandroid.
Java.
9. Create Activity ( ) MainActivity.

70

05ch03.indd 70

II. Android

08.11.2011 12:40:56

Create Activity , . . Android , .


MainActivity.java.
10. Min SDK Version ( SDK) 8.
New Android Project ,
. 3.5.

. 3.5.

Min SDK Version Android,


. , ,
.
11. Finish ().
Package Explorer ( ) Eclipse (. 3.6).

3. Android

05ch03.indd 71

71

08.11.2011 12:40:56

. 3.6. Hello Android

Java

Java,

.

,
.
, Java.

. .
Java

. .
.
,

. ,
,
com.dummies.android.
helloandroid.
, , .
,
-
. , , . , -,
-, -,
,
. .


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

72

05ch03.indd 72

II. Android

08.11.2011 12:40:56

Android
Android , , Android. API
Android ( ; . ).
Android ,
.
Android
1.5
1.6
2.0
2.0.1
2.1
2.2
2.3.1
2.3.3
3.0


3
4
5
6
7
8
9
10
11

New Android Project (


Android) .


Finish () Hello Android Package
Explorer (. 3.7),
( ). Eclipse , .

. 3.7.

Eclipse , , .
, New
Android Project? Eclipse ADT .
 . Eclipse
, . (. . 3.7).

3. Android

05ch03.indd 73

73

08.11.2011 12:40:56

, .
 . Eclipse
.
,
, Eclipse ProjectBuild Automatically ( ).
, .
<Ctrl+B>.
Finish
? . , Eclipse ADT ,
. gen,
, . .
, gen
Java, . ,
, (. 3.8). gen
.

. 3.8.
,

Build Target Min SDK Version


New Android Project Build Target ( ) Min SDK
Version ( SDK).
Android, . 2.2, 2.2.
1.6, , 1.6. , 1.6 Bluetooth, 2.0.
2.2 Bluetooh API.

Android. , Android , , . Bluetooth, ,

74

05ch03.indd 74

II. Android

08.11.2011 12:40:56

2.0. ,
, , , ,
SDK http://d.android.com. , Android 2.2 http://d.android.com/sdk/android2.2.html.

Min SDK Version ( SDK)
Android Market (. 8) Android.
3 (Android 1.6), , .
Android Market ,
,
Min SDK Version.
,

,
http://developer.android.
com/resources/dashboard/
platform-versions.html.


,

.

,
.

Android . , 1.6,
Android 1.6, 2.0, 2.1, 2.2 (
, , , , ).
. .
, . , 1.6 Bluetooth API.
Min SDK Version Android, .
, ,
. Min SDK Version ,
1. ,
Android.
!

Android (,
5, .. Android 2.0) Min SDK Version ,
, 2.0,
. , Min
SDK Version.

3. Android

05ch03.indd 75

75

08.11.2011 12:40:56


, Android,
. Eclipse
AVD (Android Virtual Device Android).
, , . ,
Android. , SDK
.
, .
1. Android SDK and AVD Manager (
Android).
Eclipse , . 3.9.
,
(. 3.10).

. 3.9.
Android SDK and AVD Manager

. 3.10. Android SDK and AVD Manager

76

05ch03.indd 76

II. Android

08.11.2011 12:40:56

2. New ().
Create new Android Virtual Device (
Android), . 3.11.

. 3.11.

3. Name () 2_2_Default_HVGA.
Android
.
4. Target ( )
Android 2.2 API Level 8.
5. SD Card ( ) .
SD . SD, () .
SD.
6. Built-in () Skin () HVGA.
7. Hardware () , .
, ,
GSM-, GPS-, - .. .
8. Create AVD ( Android).
Android SDK and AVD
Manager (. 3.12).

3. Android

05ch03.indd 77

77

08.11.2011 12:40:56

9. Android SDK and AVD Manager.

. 3.12.

:
Android requires .class compatibility set to 5.0. Please fix project
properties ( 5.0.
.). Eclipse Android ToolsFix Project Properties (
Android ). Eclipse
, .


.
Android ,
, ,
.. ,
,


.
:
{_}_{}_{}
[{_}]

78

05ch03.indd 78


2_2_Default_HVGA. Android 2.2. 2.2
2_2. ,
.

.
, .
Default ( )
Android SDK.
( )
. ,
WVVGA800.

II. Android

08.11.2011 12:40:56



, -
. Eclipse,
. ,
, , , . , ADT
.
ADT .
 .
. Android
.
 . , .
Android,
RunRun (), ADT , New_
configuration ( ). , Eclipse , .
,
RunRun. ( ), ,
.


,
, RunDebug
Configurations ( ) .


, ,
, , ,
. .
1. RunRun Configurations ( ).
Run Configurations (. 3.13). .
:
Android Application ( Android);
Android JUnit Test ( Android JUnit).

3. Android

05ch03.indd 79

79

08.11.2011 12:40:56

. 3.13. Run Configurations

2. Android Application, , New


Launch Configuration ( ), . 3.14.
Android Application
New ().
, .

New Launch Configuration

. 3.14.

3. Android. Name () ExampleConfiguration. .


4. Browse ().
Project Selection ( ).

80

05ch03.indd 80

II. Android

08.11.2011 12:40:56

5. Hello Android (. 3.15) OK.


Project Selection , Android .

. 3.15.

6. Launch Default Activity ( , ).


, ,
MainActivity. (. ).
7. Target ( ), . 3.16.
Automatic.
8. , AVD Name ( ) 2_2_Default_HVGA.

. 3.16. Target

3. Android

05ch03.indd 81

81

08.11.2011 12:40:57

9. 2_2_Default_HVGA.
, RunRun. Manual
(), ,
. Automatic, , ( 2_2_
Default_HVGA).
10.
Apply () Close ().
ExampleConfiguration Run Configurations.


, .
,
,
. , ADT ,
.
,
.
1. .
RunRun Configurations ( ).
2. ExampleConfiguration, . Duplicate ().
ExampleConfiguration (1).
3. . Name ()
DuplicateTest.
, (
).
, . . .
4. DuplicateTest,
Delete (), .
, , Delete.
5. Close, Run Configurations.

82

05ch03.indd 82

II. Android

08.11.2011 12:40:57



. .
-!


.
RunRun () <Ctrl+F11>. ADT , , .
(, ),
Run As ( ), . 3.17.
Android Application ( Android),
.

. 3.17. Run As

ExampleConfiguration, , (. 3.18).
ANDROID
! , , ,
! .
,
Linux. .
, Linux
.

3. Android

05ch03.indd 83

83

08.11.2011 12:40:57

. 3.18.

.
. 3.18. , , .
.
Android (. 3.19). , . ,
.
, (. 3.20)
.
, .
. ,
,
RunRun. ADT
.

84

05ch03.indd 84

II. Android

08.11.2011 12:40:57

. 3.19. Android

. 3.20. 2_2_Default_HVGA

3. Android

05ch03.indd 85

85

08.11.2011 12:40:57

, .
, ? , , .
,
, . , Lock (), .
. ,
Unlock (), . 3.21.

. 3.21.

. 3.22.
Hello Android, . , . 3.22.
Hello Android. , . 3.23. (, )
strings.xml Hello, Android.
, , ,
RunRun . , ,
Hello Android.

86

05ch03.indd 86

II. Android

08.11.2011 12:40:57

. 3.22.

. 3.23. Hello Android , ...

3. Android

05ch03.indd 87

87

08.11.2011 12:40:57

, ADT
. , Eclipse
( ) RunRun.
. Console ()
Eclipse (. 3.24).
Console

. 3.24. ,


Console
.
Console Hello Android.
[2011-07-05
[2011-07-05
[2011-07-05
[2011-07-05

[2011-07-05

[2011-07-05
[2011-07-05

[2011-07-05

88

05ch03.indd 88

13:13:46 - Hello Android] --------------------13:13:46 - Hello Android] Android Launch!


13:13:46 - Hello Android] adb running normally.
13:13:46 - Hello Android] Performing
com.dummies.android.helloandroid.MainActivity
activity launch
13:13:46 - Hello Android] Automatic Target
Mode: using existing emulator 'emulator-5554'
running compatible AVD '2_2_Default_HVGA'
13:13:48 - Hello Android] Application already
deployed. No need to reinstall.
13:13:48 - Hello Android] Starting activity
com.dummies.android.helloandroid.MainActivity
on device
13:13:49 - Hello Android] ActivityManager:
Starting: Intent {act=android.intent.action.

II. Android

08.11.2011 12:40:57

MAIN cat=[android.intent.category.LAUNCHER]
cmp=com.dummies.android.helloandroid/
.MainActivity }
[2011-07-05 13:13:49 - Hello Android] ActivityManager:
Warning: Activity not started, its current
task has been brought to the front

Console . , ,
,
.
.
[2011-07-05 13:13:49 - Hello Android] ActivityManager:
Warning: Activity not started, its current
task has been brought to the front

ADT , MainActivity ,
. , Android, .

, . ,
. ADT , , Java, .

, New Android Project (
Android). .
!

,
Android
, .
.


. 3.25 Hello Android Package Explorer (
).
, :
 src;
 gen;
 Android 2.2;
 assets;
 res.

3. Android

05ch03.indd 89

89

08.11.2011 12:40:57

. 3.25. Hello Android

, bin,
resources, libs, Referenced Libraries .. . 3.25 , New Android Project ( Android).
bin , ADT (, ADT
). libs Referenced Libraries ,
(. ).
AndroidManifest.xml default.properties.
AndroidManifest.xml ,
, default.properties
Android, (, Android).
.

src
Android,
MainActivity.java, New Android Project (
Android) . src, .
.
1. src.
src , com.
dummies.android.helloandroid.
2. , .
MainActivity.java (. 3.26).

90

05ch03.indd 90

II. Android

08.11.2011 12:40:57

. 3.26. src

, .
p1, p2 .. ,
, ,
.
, , .
.
. , - XML
HTTP ,
.
: HTTP:
 com.dummies.android.helloandroid.models;
 com.dummies.android.helloandroid.http.
Java.
Android , . 3.27.
com.dummies.
android.helloandroid,
. ,
,

.
. 3.27.
com.dummies.
android.helloandroid.

3. Android

05ch03.indd 91

91

08.11.2011 12:40:57

Android 2.2
! gen! , ,
res. Android. , ( Android 2.2 ),
, , Eclipse Java ADT.
Android 2.2 android.jar,
Android, New Android Project .
android.jar Android SDK.
Android 2.2 android.jar, , (. 3.28).
ADT Eclipse.

. 3.28. android.jar

, android
ADT. ,
Android SDK ( Eclipse) .

assets
assets () . , .
,
. .
, assets,
AssetManager. , . , (, ).
assets ( XML SQLite).
, res ,

92

05ch03.indd 92

II. Android

08.11.2011 12:40:57

. ,
- .
ID ( ).
( , ) , .

res
, .
(
). .
.
. , .
res.
.
Android , , ( ).
, ,

.
,
,
ADT R (. ).
,
res. . 3.1 .
3.1.

anim/

XML,

color/

XML,

drawable/

(.png, .jpg, .gif) XML,

drawable-hdpi/

.
hdpi .
, drawable/. ,

drawable-ldpi/

. ldpi . ,
drawable/. ,

3. Android

05ch03.indd 93

93

08.11.2011 12:40:57

. 3.1

drawable-mdpi/

.
mdpi .
, drawable/. ,

layout/

XML,

menu/

XML,

raw/

, .

values/

XML, , , , .. XML res


, values/
.
:
arrays.xml ( );
colors.xml , (
R.colors);
dimens.xml , (, 20px 20 ); R.dimens;
strings.xml (
R.strings);
styles.xml , , CSS
HTML; , ( R.styles)

res, .

, , . 3.1, , . , Android.
Android .
Android .
, , ,
Android. , ..
. ,
,
. Android:
http://d.android.com/guide/topics/resources/providing-resources.html

94

05ch03.indd 94

II. Android

08.11.2011 12:40:57

bin, libs Referenced Libraries


( Hello Android) ,
, . libs
(.. , ).
libs .
, . Package
Explorer ( )
NewFolder (). . Hello Android,
(.. libs) Finish ().

, . , jTwitter Java . jTwitter Android, jtwitter.jar libs.
libs Refresh (), jtwitter.jar
.
libs
Java, .. , . , Eclipse ,
. , libs jtwitter.jar. , ,
jtwitter.jar
Build PathAdd to Build Path (  ).
Referenced
Libraries (. 3.29). . , ,
Eclipse.

. 3.29. Referenced Libraries

3. Android

05ch03.indd 95

95

08.11.2011 12:40:57

jTwitter :
http://www.winterwell.com/software/jtwitter.php

libs . ,
.

gen
Android gen . ADT gen
.
ADT R.java ( ).
res. gen ,
res. gen
, res.
Java Android
, .. res.
R.java. R.java , res.
,
. R
, Eclipse. R
.
Hello Android gen , gen.
R.java, . Java. ,
, res. , res ( ).
R.java.
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
. .

aapt .
.
*/
package com.dummies.android.helloandroid;
public final class R {
public static final class attr {
}
public static final class drawable {

96

05ch03.indd 96

II. Android

08.11.2011 12:40:57

public static final int


}
public static final class
public static final int
}
public static final class
public static final int
public static final int
}

icon=0x7f020000;
layout {
main=0x7f030000;
string {
app_name=0x7f040001;
hello=0x7f040000;

, 0x? ,
. ADT ,
, .
, ADT R.java.
, .
!

R.java . ,
. R.java
, gen
. ADT R.java.


, , , ?
Android . ,
, .
Android
AndroidManifest.xml. Android
.
Android ,
. , .
 Java, .
, (.. ) , , Android Market.
 ,
.
 , .
 Android API, .
. , .
(version name) (version
code). .

3. Android

05ch03.indd 97

97

08.11.2011 12:40:57

( )
, . . , Android Market
. Android, (. ).
. :
(, , ). Android ; .
1. ,
(major) (minor) .
, , . .
!

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


(version name) , .
, , :
<_>.<_>.<_>

, , , 2.1.4 2.1.
Android
, .

98

05ch03.indd 98

II. Android

08.11.2011 12:40:57

( ) . . ,
Foursquare . 2010-06-08 ,
8 2010 . , , , , ,
.

,
. Android . , .
, . . 3.2 .
3.2.

Internet
Write External Storage
Camera
Access Fine Location
Read Phone State

default.properties

ADT Eclipse.
(, , )
.
!

default.properties . ,
Eclipse (. 3.30). ,
Package Explorer ( )
Properties ().

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

3. Android

05ch03.indd 99

99

08.11.2011 12:40:57

. 3.30.

100

05ch03.indd 100

II. Android

08.11.2011 12:40:57

...








Silent Mode Toggle







, , Android, . , , Android Market.


, ,
. , . ,
,
. , .

.
,
. ,
( ). ,

.
, . ,
. . ,
.
,
, , ,
, . , , . .
. ,
.

Android Application Development For Dum.indb 101

08.11.2011 12:23:55

Silent Mode Toggle


Silent Mode Toggle ( ). , Android Eclipse, . , ,
3.
,
Eclipse. Eclipse . FileClose All
( ).
, Hello Android, , . Package Explorer ( ) Eclipse Hello Android
Close Project ( ).
Eclipse, . Eclipse , , , Eclipse .
Silent Mode Toggle.
. FileNew Project ( ). Android Project Next (). . 4.1.
4.1. Silent Mode Toggle

Application Name ( )

Silent Mode Toggle

Project Name ( )

Silent Mode Toggle

Contents ()

Build Target ( )
Package Name ( )

Android 2.2
com.dummies.android.silentmodetoggle

Create Activity ( )

MainActivity

Min SDK Version ( SDK)

Finish (). Silent Mode Toggle (. 4.1).

Eclipse , : The project cannot be built


until build path errors are resolved ( ,
).
Android ToolsFix Project Properties ( Android
). Eclipse
.

102

Android Application Development For Dum.indb 102

II. Android

08.11.2011 12:23:55

. 4.1. Silent Mode Toggle Eclipse

Build Target, ,
. Min SDK Version,
, Android .
, , , . ,
?
, .


, Silent Mode Toggle Eclipse, . ,
. . ,
.
Silent Mode Toggle ,
. , , : . , , . 4.2
4.3, , .
, ,
.
. , , . ,
. ,
.

4.

Android Application Development For Dum.indb 103

103

08.11.2011 12:23:55

. 4.2.

. 4.3.

104

Android Application Development For Dum.indb 104

II. Android

08.11.2011 12:23:55

XML
Eclipse
res/layouts Android. Silent Mode Toggle, ADT main.xml
res/layouts. , . ADT
. main.xml,
Eclipse (. 4.4). .

. 4.4. main.xml

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

4.

Android Application Development For Dum.indb 105

105

08.11.2011 12:23:55

XML
. .

XML
XML
Eclipse Android, XML, XML .
<?xml version="1.0" encoding="utf-8"?>


(LinearLayout) . LinearLayout
, ,
, . LinearLayout . LinearLayout.
</LinearLayout> ,
LinearLayout , , ,
, .
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

Android (view) . ,
. ,
, (, TextView), , , . XML
. TextView,
, @string/hello ( 3).
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>

106

Android Application Development For Dum.indb 106

II. Android

08.11.2011 12:23:55


.
, , . View () Android, ,
View.
LinearLayout, , .
</LinearLayout>


- . . ( ). ,
( ).
, Android ,
. Java, XML main.xml. . 4.2
, Android SDK.
4.2. , Android SDK

LinearLayout

RelativeLayout

FrameLayout


. FrameLayout , .
, ,

TableLayout

, TabHost, ,
SlidingDrawer, , ( , ).
. , . 4.2.

4.

Android Application Development For Dum.indb 107

107

08.11.2011 12:23:55


: . ,
Eclipse ( ).
,
, , . . , , ,
XML , .


. 4.5 main.xml .
, Graphical Layout ( ) .

. 4.5. main.xml

Graphical Layout (. 4.6).


, , , .

TextView, , main.xml.
.

108

Android Application Development For Dum.indb 108

II. Android

08.11.2011 12:23:55

. 4.6. main.xml



. , . Properties ().
Properties , , .
1. , .
2.
Show InProperties ( ).
(. 4.7). , . : (
),
, .
Properties .
. Properties
, Android, Properties
. Properties , WindowsShow ViewOtherGeneralProperties (
).

4.

Android Application Development For Dum.indb 109

109

08.11.2011 12:23:55

. 4.7. ()

. , LinearLayout TextView ,
RelativeLayout.
,
. , - , ,
. ,
,
. . , TextView
. main.xml , Java.


main.xml, XML. main.xml, Graphical Layout ( ),
. 4.7. TextView. .

110

Android Application Development For Dum.indb 110

II. Android

08.11.2011 12:23:56

<?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"
>
</LinearLayout>


XML LinearLayout, LinearLayout.
XML. . 4.3
, .
4.3.

xmlns:android="..."

XML, Android SDK

android:orientation="vertical"

android:layout_width="fill_parent" ,

android:layout_height="fill_parent" ,

, LinearLayout . .
.


, , .
, .
Java .

layout_width layout_height
, , Android , . , .
layout_width ( ) layout_height ( ) . Android SDK LayoutParams.
layout_width , layout_height
.

4.

Android Application Development For Dum.indb 111

111

08.11.2011 12:23:56

fill_parent wrap_contents

layout_width layout_height
. fill_parent ( ) wrap_
content ( ).
fill_parent Android ,
,
. wrap_content Android , .
, (, ,
TextView), ,
. wrap_content Autosize
( ) Windows.
, XML. (..
), Java, ,
, .
.
. , Android SDK.
!

layout_width layout_
height,
. , .
Android 2.2 fill_parent match_
parent. fill_parent , .
2.2 ,
match_parent.


, , . ,
.


Silent Toggle Mode (. . 4.2 4.3),
( ). , . , , ,
.
, .

112

Android Application Development For Dum.indb 112

II. Android

08.11.2011 12:23:56


Android
. (drawableldpi, drawable-mdpi drawablehdpi), , .
,

. ? ,
, . ,

.
, .
.
Android

:
http://developer.android.com/guide/
practices/screens-support.html

,
. .
1. Windows Eclipse.
( phone_silent.png) res/drawable-mdpi Eclipse (. 4.8).

. 4.8.

4.

Android Application Development For Dum.indb 113

113

08.11.2011 12:23:56

2. phone_on.png.
drawable-mdpi :
phone_on.png, ;
phone_silent.png, .
, drawable-mdpi. , . 4.9. icon.png Android, ADT .

. 4.9. Silent Toggle Mode

Eclipse ADT , , , Eclipse ProjectBuild Automatically ( ),


. , gen, R.java.
R.java . Java XML.
XML.

XML
phone_on.png phone_silent.png , .
, ImageView, , .
main.xml .
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:orientation="vertical"

114

Android Application Development For Dum.indb 114

II. Android

08.11.2011 12:23:56

android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/phone_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/phone_on" />
</LinearLayout>

ImageView LinearLayout.
ImageView .


ImageView ,
.
 android:id="@+id/phone_icon". id Android.
, :
http//developer.android.com/guide/topics/ui/declaring-layout.html

 android:layout_gravity. ,

.
center_horizontal.
, , .

(.. ). , center_vertical, left_horizontal ..
linearLayout.
LayoutParams.
 android:src="@drawable/phone_on". ImageView. .
@drawable/phone_on src
R.java. XML, @ .


(, src) @
drawable, res drawable-mdpi.
mdpi? ? , . Android drawable.

4.

Android Application Development For Dum.indb 115

115

08.11.2011 12:23:56

ADT , . Android
, ldpi, mdpi hdpi.
,
drawable-hdpi, Android . , Android drawable-mdpi,
, drawable-ldpi,
. , .
phone_on ,
. phone_on.png. Java .png
R.java.
ADT , , . Eclipse main.xml src
ImageView. <Ctrl+>. (. 4.10). ,
, . , icon, .
<Ctrl+> , .

. 4.10.

116

Android Application Development For Dum.indb 116

II. Android

08.11.2011 12:23:56


, ,
.
( ) , . . Silent
Mode Toggle, ADT
Android (. 4.11).
. ?
, ? !
. , ,
. Silent Toggle Mode , (. 4.12). ,
. ,
, ,
, Android.

. 4.11.
Android 2.2

. 4.12.
Silent Mode Toggle


? , . ,
, :
http://d.android.com/guide/practices/ui_guidelines/icon_design.html

, , .
, .


2 Android SDK, . Android SDK (. 2) docs/shareables.
.zip, . , ,
.

4.

Android Application Development For Dum.indb 117

117

08.11.2011 12:23:56


. , . , .
.
. 4.4 .
4.4.

, ldpi

3636 px

, mdpi

4848 px

, hdpi

7272 px


, .
1. . icon.png.
.
2. Windows mdpi
Eclipse.
Eclipse , icon.png (. 4.13). Yes.

. 4.13. ,

,
.
. : ,
, .
Android:
http://d.android.com/guide/practices/ui_guidelines/icon_design.html

118

Android Application Development For Dum.indb 118

II. Android

08.11.2011 12:23:56

mdpi, ldpi hdpi.


(.. .png) . , hdpi ldpi
, mdpi.
,
Android, . 4.11,
.
, . ,
, , . . hdpi ldpi icon.png, ,
, mdpi.


, , . ,
. ,
View. ,
.. , , . Android
, , , ,
, ..,
. ,
, , , .
, ,
, .
Silent Toggle Mode ,
. ,
main.xml ImageView.
<Button
android:layout_height="wrap_content"
android:text=" "
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:id="@+id/toggleButton" >
</Button>

toggleButton,
Java .
wrap_
content, Android ,
, .
,
. layout_gravity . , , text.

4.

Android Application Development For Dum.indb 119

119

08.11.2011 12:23:56

main.xml . ,
, : ImageView
Button.
<?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"
>
<ImageView
android:id="@+id/phone_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/phone_on" />
<Button
android:layout_height="wrap_content"
android:text=" "
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:id="@+id/toggleButton" >
</Button>
</LinearLayout>


, . Eclipse main.xml
Graphical Layout ( ). Eclipse (. 4.14).
, ADT
ADT, Eclipse.

. ADP1 (Android
Development Phone 1 Android, 1).
, Google.
ADP1,

.
Create (),
. ,
Querty .

120

Android Application Development For Dum.indb 120

(. 4.14),
,
, . ,
ADP1
.
 Landscape, closed (, ).
.
.
 Potrait ().
.
 Landscape, open (, ). .
.

II. Android

08.11.2011 12:23:56

. 4.14. Silent Mode Toggle


, . - . , ,
. , .
1. main.xml,
.
2. LinearLayout background .
android:background="#ffffff"

3. , .
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"

4.

Android Application Development For Dum.indb 121

121

08.11.2011 12:23:56

android:layout_height="fill_parent"
android:background="#ffffff"
>

4. main.xml.
5. Graphical Layout,
.
Eclipse , . 4.15.
#ffffff, . . , #ff0000
. (.. res) , . , .
, ,
, .

. 4.15. ,

, Silent Mode Toggle.


, .
, .. , .

122

Android Application Development For Dum.indb 122

II. Android

08.11.2011 12:23:56


...










Android

, , ! , .
, :
, - .


Android (activity) , , .
, , , .
Android ,
. ,
, .
Android
, .
Activity.
,
Activity , .
, (.. ).

Android Application Development For Dum.indb 123

08.11.2011 12:23:57

,
.
 onCreate(). ,
, , .
 onPause(). ,
, . , ,
(.. -
) .
. ,
(.. ).
. , .
, .
Android, , , .
, . 5.1.
5.1.

(
)

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

,
Android . , . ,

.
, .
,

124

Android Application Development For Dum.indb 124

II. Android

08.11.2011 12:23:57


, , (. 5.1) ,
.

onCreate()

onStart()

onRestart()

onResume()

onPause()

onStop()

onDestroy()

. 5.1.

, .
, .

5.

Android Application Development For Dum.indb 125

125

08.11.2011 12:23:57


.

onCreate() onDestroy(). onCreate()
, onDestroy() . ,

, onCreate() ,
onDestroy() .
 onStart()
onStop().
,
(, ).
, .
, . ,

. onStart(), onStop() , ( ).
onStart() onStop() , .
 . onResume()
onPause().

.
onResume() onPause() , ,
. ,
.


.
( ) , .
onCreate()
onPause() .
.
public class Activity extends ApplicationContext {
protected void onCreate(Bundle savedInstanceState);
protected void onStart();
protected void onRestart();

126

Android Application Development For Dum.indb 126

II. Android

08.11.2011 12:23:57

protected
protected
protected
protected

void
void
void
void

onResume();
onPause();
onStop();
onDestroy();


.
 onCreate(). . . onCreate() onStart(). onCreate() .
onStart().
 onRestart(). . onRestart() onStart().
. onStart().
 onStart(). ,
. onResume(),
, onStop(), . .
onResume() onStop().
 onResume(). ,
. . . onPause().
 onPause(). ,
, .
,
.
, onResume(), onStop().
. onResume() onStop().
 onStop(). ,
, ,
. , . .
, onRestart(),
, onDestroy(). . onRestart() onDestroy().

5.

Android Application Development For Dum.indb 127

127

08.11.2011 12:23:57

 onDestroy(). ,
.
: finish(),
. ,
, isFinished(). onPause() , : .
. .
,
.
, , ,
. . , ,
,
Android (, -
, ). (, , )
onPause().


, , ,
.
, . , , ,
. : onPause()onStop()onDestroy().
onDestroy() .
, , , . ,
,
.
.
, . , .


, 3 Silent Mode Toggle. MainActivity
MainActivtity.java. Eclipse.

128

Android Application Development For Dum.indb 128

II. Android

08.11.2011 12:23:57

onCreate
, onCreate().
MainActivity.java .
. .
public class MainActivity extends Activity {
/** */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}


setContentView().
!

:
super.onCreate(savedInstanceState);

. super
onCreate(),
MainActivity. , .
onCreate() onCreate() .

Bundle
:
Bundle savedInstanceState

Bundle (, ) , . .
Bundle, . III,
, , .


, . ,
, , . , , .
,
.
:
setContentView(R.laout.main)

5.

Android Application Development For Dum.indb 129

129

08.11.2011 12:23:57

R.layout.main main.xml,
res/layouts .


Silent Mode Toggle . .
, .
, .
. Android
.
.


.
. , <Alt+E> .
, . , , .
,
onKeyDown(), .
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// ,
return super.onKeyDown(keyCode, event);
}


,
. ,
.
, :
 Button;
 ImageButton;
 EditText;
 Spinner;
 ListItemRow;
 MenuItem.

130

Android Application Development For Dum.indb 130

II. Android

08.11.2011 12:23:57

,
Clickable ( )
false. (
clickable) (
setClickable()). .


Silent Mode Toggle , .


, 5.1. toggleButton.
, onCreate().

5.1.
,
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button toggleButton =
(Button)findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
}
});
}

findViewById(), Android. -. View (), . View


Button (), View.
Button toggleButton =
(Button)findViewById(R.id.toggleButton);

.
Button . View.
OnClickListener, onClick().
onClick(),
. onClick() , .

5.

Android Application Development For Dum.indb 131

131

08.11.2011 12:23:57

, (,
ImageView,
ImageButton), .
, .

onCreate() Button ,
(. 5.2). Eclipse , , Button.
,
Button. ,
, , .

. 5.2. Eclipse , Button

, , .
.
import android.widget.Button

Eclipse , Button , android.widget.


, .
, , import
. , ,
.
.
:
import android.widget.*

132

Android Application Development For Dum.indb 132

II. Android

08.11.2011 12:23:57

Eclipse
android.widget.



. , , onCreate(). setButtonClickListener(), . onCreate(). 5.2.

5.2.

public class MainActivity extends Activity {
/** */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setButtonClickListener();
16
}
private void setButtonClickListener() {
Button toggleButton =
(Button)findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
//
}
});
}

19

 16. , .
 19. , .
,
onClick(), .

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

5.

Android Application Development For Dum.indb 133

133

08.11.2011 12:23:57

, ,
. .



AudioManager , . AudioManager
,
onCreate(). , onCreate().
AudioManager
mAudioManager. ,
( 5.3).

5.3. mAudioManager
package com.dummies.android.silentmodetoggle;
import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private AudioManager mAudioManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setButtonClickListener();
mAudioManager =
(AudioManager)getSystemService(AUDIO_SERVICE);
}

11

20

private void setButtonClickListener() {


Button toggleButton =
(Button)findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
//
}
});
}
}

134

Android Application Development For Dum.indb 134

II. Android

08.11.2011 12:23:57

.
 4. import .
AudioManager.
 11. mAudioManager.
,
.
 20. mAudioManager
getSystemService(),
Activity.
Activity, .
getSystemService(), Object. , Object .
getSystemService() , . Context, Java
http://d.android.com/reference/android/content/Context.html. :
 ;
 ;
 .


AudioManager
AudioManager ,
. ,
5.4. .

5.4.
package com.dummies.android.silentmodetoggle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private AudioManager mAudioManager;
private boolean mPhoneIsSilent;

14

@Override
public void onCreate(Bundle savedInstanceState) {

5.

Android Application Development For Dum.indb 135

135

08.11.2011 12:23:57

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAudioManager =
(AudioManager)getSystemService(AUDIO_SERVICE);
checkIfPhoneIsSilent();
23
setButtonClickListener();
25
}
private void setButtonClickListener() {
Button toggleButton =
(Button)findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
if (mPhoneIsSilent) {
32
//
mAudioManager.setRingerMode(
AudioManager.RINGER_MODE_NORMAL);
mPhoneIsSilent = false;
} else {
//
mAudioManager
.setRingerMode(AudioManager.RINGER_MODE_SILENT);
mPhoneIsSilent = true;
}
//
toggleUi();
44
}
});
}
/**
*
*/
private void checkIfPhoneIsSilent() {
53
int ringerMode = mAudioManager.getRingerMode();
if (ringerMode ==
AudioManager.RINGER_MODE_SILENT) {
mPhoneIsSilent = true;
} else {
mPhoneIsSilent = false;
}
}
/**
*
*/
private void toggleUi() {
66
ImageView imageView =
(ImageView) findViewById(R.id.phone_icon);
Drawable newPhoneImage;

136

Android Application Development For Dum.indb 136

II. Android

08.11.2011 12:23:57

if (mPhoneIsSilent) {
newPhoneImage =getResources().
getDrawable(R.drawable.phone_silent);
} else {
newPhoneImage = getResources().
getDrawable(R.drawable.phone_on);
}
imageView.setImageDrawable(newPhoneImage);
}
@Override
protected void onResume() {
super.onResume();
checkIfPhoneIsSilent();
toggleUi();
}

84

. .
 14. mPhoneIsSilent ,
.
 23. checkIfPhoneIsSilent() mPhoneIsSilent. false,
, .
, , ( ), mPhoneIsSilent.
 25. onCreate(),
mPhoneIsSilent.
, , ,
mPhoneIsSilent . , ,
.
 32. 32 44 . mPhoneIsSilent
, . ,
if, RINGER_MODE_NORMAL,
. mPhoneIsSilent false . ,
else. else
RINGER_MODE_SILENT,
. , else
mPhoneIsSilent true,
.

5.

Android Application Development For Dum.indb 137

137

08.11.2011 12:23:57

 44. toggleUi() ,
.
toggleUi().
 53. checkIfPhoneIsSilent()
,
mPhoneIsSilent onCreate().
,
AudioManager.
, mPhoneIsSilent true, false.
 66. toggleUi() ImageView, , .
, , . 4.4,

. 4.5. 4 . ImageView .
getResources().getDrawable()
setImageDrawable() ImageView.
, ImageView.
 84. .
. onResume() , .
?
,
mPhoneIsSilent? . ,
. onResume()
toggleUi(), . onResume() onCreate(), toggleUi() ,
mPhoneIsSilent
.
toggleUi() onResume()
, Silent Mode Toggle, . ,
. onResume(),
. ,
, ,
.

138

Android Application Development For Dum.indb 138

II. Android

08.11.2011 12:23:57


, Android. .


( , ).
Hello Android. Silent Mode
Toggle . ADT . .
.
1. Eclipse RunRun ()
<Ctrl+F11>, .
Run As ( ),
. 5.3. Android Application ( Android)
OK. .

. 5.3. ,

2. , ( )
.
, , 3. , . , RunRun
<Ctrl+F11>. (. 5.4).

5.

Android Application Development For Dum.indb 139

139

08.11.2011 12:23:57

. 5.4. Silent Mode Toggle

3. .
(. 5.5), .
(. . 5.5), Android .

. 5.5. ,

140

Android Application Development For Dum.indb 140

II. Android

08.11.2011 12:23:57

4. ,
( ).
Silent Mode Toggle, .
. ,
. Silent Mode Toggle, .

Android
Eclipse
, . , . 2 USB,
( Nexus One, ).
1. , Android Market.
.
2. MenuSettings
( ), Settings.
Applications ().
3. Unknown sources ( ),
. 5.6.

. 5.6.

, ,
.

5.

Android Application Development For Dum.indb 141

141

08.11.2011 12:23:58

4. Application settings ( )
Development (). Development (. 5.7)
USB debugging ( USB).
( ).

. 5.7. USB

5. USB.
6. , Eclipse. Run Run
() <Ctrl+F11>.
ADT
Android Device Chooser (
Android), . . 5.8 Android Device Chooser,
Nexus One. ,
. ,
, .
7. OK.
,
. ( , ).

142

Android Application Development For Dum.indb 142

II. Android

08.11.2011 12:23:58

. 5.8.


,
. Unknown sources USB debugging
, . .
, - ;
. .


Eclipse ,
Eclipse. Eclipse , ,
Android.
Eclipse ADB (Android
Debugging Bridge Android), ADT.


, Eclipse
RunRun () <Ctrl+F11>. ! ADT , , ,
.

5.

Android Application Development For Dum.indb 143

143

08.11.2011 12:23:58


, ,
. , , . - . . , , ,
. , , , . . , . , ADT
,
.

DDMS
DDMS (Dalvik Debug Monitor Server
Dalvik)
( ):
 ;
 ;
 ;
 ;
 ;
 SMS;
 .
DDMS , . tools Android SDK. 1
tools ,
DDMS .

DDMS
. , DDMS ,
.
DDMS LogCat, Android (. 5.9).
, , . Application Not Responding ( ) Force
Close ( ), , .
DDMS LogCat,
, , . DDMS ,
.

144

Android Application Development For Dum.indb 144

II. Android

08.11.2011 12:23:58

. 5.9.

DDMS ,
, . , GPS Google MapView . ,
. , , , , .
, . ,
DDMS, . , GPS GPX KML, . , ,
90 /.

DDMS
Java , DDMS,
. MainActivity.java onCreate()
, 5.5.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAudioManager =
(AudioManager)getSystemService(AUDIO_SERVICE);
checkIfPhoneIsSilent();
setButtonClickListener();
Log.d("SilentModeApp", " ");
12
}

, 12,
. SilentModeApp , TAG.
, . , DDMS. TAG
DDMS.
Java TAG
.
private static final String TAG = "SilentModeApp"

5.

Android Application Development For Dum.indb 145

145

08.11.2011 12:23:58

Log.d 5.5. d
. :
 e ;
 I ;
 wtf What The... ( ?!);
 v Verbose ( ).
, ,
.
, android.util.Log.

DDMS
DDMS, DDMS
, DDMS Eclipse.
 . , Android SDK. tools ddms.bat. DDMS Eclipse
(. 5.10).

. 5.10. DDMS, Eclipse

146

Android Application Development For Dum.indb 146

II. Android

08.11.2011 12:23:58

 Eclipse. ADT DDMS Eclipse. DDMS,


Eclipse Open Perspective ( ),
. 5.11, DDMS.
DDMS, Open Perspective
Other () DDMS.
Eclipse
DDMS,
. , DDMS . DDMS LogCat.
.
(. 5.12).
LogCat
, .
. 5.11.
DDMS

. 5.12. LogCat Eclipse

Eclipse , RunRun
<Ctrl+F11>. ,
DDMS. LogCat Log.d
(. ), (. 5.13). , , Log.d.

5.

Android Application Development For Dum.indb 147

147

08.11.2011 12:23:58

LogCat

. 5.13. , Log.d

Java.
Java Perspective ( Java), . 5.14.
Java Perspective

. 5.14. Java

Eclipse
DDMS , ,
Eclipse. , ,
LogCat .

148

Android Application Development For Dum.indb 148

II. Android

08.11.2011 12:23:58

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


. , ,
. Android , , .
(, , ) , - , .
. , AudioManager onCreate() , , .
mAudioManager, , .
. ,
onCreate()
. , mAudioManager ,
, . 5.6 . mAudioManager ,
.

5.6.
mAudioManager
private AudioManager mAudioManager;
private boolean mPhoneIsSilent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//mAudioManager =
// (AudioManager)getSystemService(AUDIO_SERVICE);
checkIfPhoneIsSilent();
setButtonClickListener();
Log.d(SilentModeApp, );
}

/**
*
*/
private void checkIfPhoneIsSilent() {

5.

Android Application Development For Dum.indb 149

149

08.11.2011 12:23:58

int ringerMode = mAudioManager.getRingerMode();


if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
mPhoneIsSilent = true;
} else {
mPhoneIsSilent = false;
}

22

.
 1. mAudioManager .
 9. ,
, .
 22. onCreate() checkIfPhoneIsSilent(),
,
mAudioManager null.
( ).
, onCreate() .


.
 Java , .
RunToggle Breakpoint (
), . 5.15.
 , , <Ctrl+Shift+B>.
. 5.15.
 Eclipse
, .

. 5.15.

150

Android Application Development For Dum.indb 150

II. Android

08.11.2011 12:23:58

(. 5.16),
.

. 5.16. Java

. 3
onCreate(), 5.7.

5.7.
setContentView(R.layout.main);
//mAudioManager =
//
(AudioManager)getSystemService(AUDIO_SERVICE);

checkIfPhoneIsSilent();

 3. mAudioManager.
 5. .
5.


,
, .. .
AndroidManifest.xml, Package Explorer
( ), Eclipse.
Application (). Debuggable ( ) true (. 5.17). AndroidManifest.xml.
, , .
Debuggable true.
, Eclipse ,
. true.

5.

Android Application Development For Dum.indb 151

151

08.11.2011 12:23:58

Application

. 5.17.

, (. 5.7) .
, RunDebug ()
<F11>. ADT
Eclipse
.
, .
, , , ADT
(. 5.18).
. Force Close ( ). ,
. , (. 5.19), ,
Eclipse Debug (). Yes.
(. 5.20), .
: , ! , ,
.

152

Android Application Development For Dum.indb 152

II. Android

08.11.2011 12:23:58

. 5.18.

. 5.19. Debug

mAudioManager, ,
null, .. . ,
.

5.

Android Application Development For Dum.indb 153

153

08.11.2011 12:23:58

mAudioManager

. 5.20.


, , .
Resume () <F8>,
source not found ( ).
, , (. 5.21). Force close ( ).
, , .

.
, Disconnect (), . 5.20. Java
3 (. 5.7) MainActivity.java.
, .

154

Android Application Development For Dum.indb 154

II. Android

08.11.2011 12:23:58

. 5.21.


, . Android. , , ; , . , . ,
, , ( ), . 5.8.

5.8.
/**
*
*
*/
private void toggleUi() {
ImageView imageView =
(ImageView) findViewById(R.id.phone_icon);
Drawable newPhoneImage;
if (mPhoneIsSilent) {
11
newPhoneImage =
getResources().getDrawable(R.drawable.phone_silent);

5.

Android Application Development For Dum.indb 155

155

08.11.2011 12:23:58

} else {
newPhoneImage =
getResources().getDrawable(R.drawable.phone_on);
}
imageView.setImageDrawable(newPhoneImage);
}
@Override
protected void onResume() {
super.onResume();
//checkIfPhoneIsSilent();
toggleUi();
};

26

 11. if , .
mPhoneIsSilent.
true, ,
false .
 26. toggleUi() , . 26 checkIfPhoneIsSilent(),
mPhoneIsSilent . Resume(), .
Silent Mode Toggle,
Silent Mode Toggle. Resume(),
mPhoneIsSilent , .. .
toggleUi() .
, mPhoneIsSilent
null, , .


, , . ,

-. , ? . :
- , ?
? .
. , .

156

Android Application Development For Dum.indb 156

II. Android

08.11.2011 12:23:58

Android ,
, . ,
,
, .

. : ( , ,
..) , .
Android ,
, SMS, , .
, Android
. , .
, Android .
, ,
. ,
. , . . ?
.
, ,
Android Market.


, , . , , . .
, .
-? . ,
, . , ,
. onPause() onResume()?
Android , .


Silent Mode Toggle,
. : , .
, , ,
(. 5.22).

5.

Android Application Development For Dum.indb 157

157

08.11.2011 12:23:58

. 5.22. ,

. ?
, , ,
. - , , .


. Android
, . Android SDK

, Java, Android, .
Android http://d.
android.com/guide/topics/
testing/testing_android.html.

158

Android Application Development For Dum.indb 158


Android , ,

.
 jUnit. SDK
jUnit ADT. jUnit
,
Java. jUnit
, Android.
jUnit

II. Android

08.11.2011 12:23:59

www.junit.
org. Eclipse
, jUnit.
 Monkey. ,

5.

Android Application Development For Dum.indb 159

. , , , ,
.
Monkey . Android SDK.

159

08.11.2011 12:23:59

Android
...




Android.
? , 3-4,

.
Android , , .


Android :
 ;
 ;
 ;
 ;
 ;
 ;
 ;
 ;
 .
, , Silent Mode Toggle,
. , Android. .

Android , , 10px, 2in, 5sp. Android

Android Application Development For Dum.indb 160

08.11.2011 12:23:59

, . , (padding)
10px, .. . Android
.
 dp (density-independent pixels , ).
160dpi
(dots per inch ). 1dp
160dpi.
dp , . . dp
, ,
. :
http://developer.android.com/guide/practices/screens_support.html

 sp (scale-independent pixels , ).
dp, , . sp ,
.
 pt (points ). 1/72 .
 px (pixels ). .
, , . , px, ,
,
, , .
 mm (). .
 in (inches ). 2,54 .

, ,
. Android CSS (Cascading Style Sheets ), -. ,
( ),
( ). Android .
, , . ,
, ..

6. Android

Android Application Development For Dum.indb 161

161

08.11.2011 12:23:59


, , . , . , TextView
.
.

, .
 . XML /
res/values/<_>.xml. ,
bools.xml.
 . XML
/res/values/<_>.xml. , integers.xml.
 . ,
XML /res/values/<_>.xml.
, integers.xml.
, .
 . , (drawables).
.
.
,
. /
res/values/<_>.xml. , types.xml.

, XML.
. . ,
. , XML,
menus/. XML.

(, colors.xml) /res/
values. Java , , login_screen_font_color (_
___).
.

162

Android Application Development For Dum.indb 162

II. Android

08.11.2011 12:23:59


.
, R. , 3.


, Java,
String.
. ,
,
. , ,
,
, . Java Eclipse.


, .
1. .
2. .
3. () .
, .
3045 .
.


15 . , 30
. , 15
. . .
1. Eclipse main.xml,
layouts.
2. , ( ).
<Button
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text=" "
/>

3. , .

6. Android

Android Application Development For Dum.indb 163

163

08.11.2011 12:23:59

4. <Shift+Alt+A>.
.
5. Extract Android String ( Android).
Extract Android String (. 6.1).
. , OK.
main.xml strings.xml .
main.xml @string/switch_silent_mode. strings.xml, res/values,

switch_silent_mode.

. 6.1.

XML. Java
. 15 . , .
: , ,
,
.


,
.
, .

164

Android Application Development For Dum.indb 164

II. Android

08.11.2011 12:23:59

, Android
( , 3).


,
. , 300dpi, . 250250px. hdpi
7272px ( ),
, Android Google TV
. , . . ,
, .
, , , , , .
, , .
, . ,
, .


, ,
. . .
 . - , , .
,
, .
, .
 . . .
. , , , , : res/drawable-ru res/drawableja. Android ( ) .


Android .
, , iPhone. Android
, , , .

6. Android

Android Application Development For Dum.indb 165

165

08.11.2011 12:23:59

Android? ,
Android , , . ,
. , . , , , ? ,
.
, ? , , ,
, .
.
,
.
(
) , . ,
. , (, ).
, , , .
strigns.xml, - ,
- . ,
strings.xml, . ,
, Android values-cn,
strings.xml. Android
, values, strings.xml. Java,
, .
,
. Android,
? , , .


. SDK:
http://developer.android.com/guide/topics/resources/
localization.html

166

Android Application Development For Dum.indb 166

II. Android

08.11.2011 12:23:59


, ,
Android Market
. , , .
Android Market Google
8.

6. Android

Android Application Development For Dum.indb 167

167

08.11.2011 12:23:59



...





Android


, ,
. , .
Silent Mode Toggle, . Android Market,
, .
, Silent Mode
Toggle, . ,
: ,
, Silent Mode Toggle,
, , . ,
. Silent Mode Toggle
? ! .
.
, . (.. ). , , .
:
 Intent;
 BroadcastReceiver;
 AppWidgetProvider;
 IntentService;
 AppWidgetProviderInfo.

Android Application Development For Dum.indb 168

08.11.2011 12:23:59


Android, .

Android
Android , , (
Android). .

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

. 7.1. ,

Silent Mode Toggle ,


, .
,
, Silent Mode Toggle. ,
,
(. 7.2).

7.

Android Application Development For Dum.indb 169

169

08.11.2011 12:23:59

. 7.2.

Android , Linux 2.6. Linux , Android Linux. Android


, .

Android ( ,
) .
.
, , .

,
Android. , Android. , , , , .
, , ?
.
Android , Android, .
RemoteViews ( ),
.
, . , ,
.
, ,
.
(.. ). .
Android, ( ) .
Android .

170

Android Application Development For Dum.indb 170

II. Android

08.11.2011 12:23:59

, . ,
, Android .
,
. RemoteViews .
( ), AppWidgetProvider
( ). Android
.
,
AppWidgetProvider.

AppWidgetProvider
AppWidgetProvider ,
.
, . , , .

. ,
,
RemoteViews. AppWidgetProvider.
. ,
, , .
? , . ,
, .
: .
, . , - ( ),
, - (, ),
( Android)
, . ,
, (, ), , - (
). ,
.
,
. : -

7.

Android Application Development For Dum.indb 171

171

08.11.2011 12:23:59

(). , , ,
.


, Android.

. , ,
.
,
. PendingIntent ( ) Android. PendingIntent Intent.

Android
Android. ,
, ,
. , .
Android Intent () Action
(), , .
Intent turnLightOn = new Intent("_");

Android,
( , .. Action) Intent
( , Android ). , Android
, , . Android , . ,
.
BroadcastReceiver ( ). AppWidgetProvider
BroadcastReceiver , .
BroadcastReceiver .
AppWidgetProvider . , AppWidgetProvider , , ,
AppWidgetProvider. AppWidgetProvider . , PendingIntent.
PendingIntent Intent. , . ,

172

Android Application Development For Dum.indb 172

II. Android

08.11.2011 12:23:59

,
Intent. , ,
. BroadcastReceiver,
.. AppWidgetProvider. Intent, Activity
BroadcastReceiver , ,
, , .
, .
.
startActivity(),
Intent .
BroadcastReceiver
sendBroadcast(), .
(. )
startService() bindService(),
.
,
, ,
.


.
 . ,
. ACTION_VIEW, ACTION_
EDIT ACTION_MAIN. ,
.
 . , , , URL- ,
, ..
. 7.1 Intent.
7.1.

ACTION_VIEW

tel:123

123

ACTION_DIAL

content://contacts/people/1 , 1
content://contacts/people/1 1

ACTION_EDIT

7.

Android Application Development For Dum.indb 173

173

08.11.2011 12:23:59

. 7.1

ACTION_VIEW

http://www.example.org
-
content://contacts/people

ACTION_VIEW

, .
 category:. . ,
CATEGORY_LAUNCHER , .
CATEGORY_ALTERNATIVE
.
 type:. MIME . , audio/mpeg Android
MP3.
. MIME, ,
. , .
 component:. ,
.
, (,
, .).
. , ,
. ,
, Android .
 extras:. . . , ,
extras: , .


Android
.
 . , .
.
,
.
.

174

Android Application Development For Dum.indb 174

II. Android

08.11.2011 12:23:59

 . , ,
, ,
.
, , , , MIME.
, . Gmail, Exchange POP,
. , .
.


PendingIntent ( ) , , , . . , ,
. ,
, . ,
, Android .

RemoteViews .
,
. ,
Android
RemoteViews .
RemoteViews

.
, Android ANR
(Application Not Responding ). ,
Android .
- -

, , ANR. , -
, Twitter.
,
Android ANR ,
, :

.

ANR
AppWidgetProvider.

IntentService,
ANR
.

7.

Android Application Development For Dum.indb 175

175

08.11.2011 12:23:59


PendingIntent.getBroadcast(). PendingIntent,
.
.
 Context , PendingIntent .
 RequestCode . , .
 Intent .
 Flags ,
. , .
- . PendingIntent,
Intent? ! Intent
PendingIntent (
). PendingIntent
Intent.
.
Silent Mode Toggle .




Android. Android
. PendingIntent
AppWidgetProvider. , ,
.

AppWidgetProvider
AppWidgetProvider, Eclipse,
Silent Mode Toggle.
com.dummies.android.silentmodetoggle . , AppWidget.java. src/com.dummies.android.silentmodetoggle Silent
Mode Toggle NewClass ().
New Java Class ( Java).
AppWidget android.appwidget.AppWidgetProvider
(. 7.3). Finish ().
, Eclipse AppWidget.java.

176

Android Application Development For Dum.indb 176

II. Android

08.11.2011 12:24:00

. 7.3. AppWidget

AppWidgetProvider ,
RemoteViews. ?
AppWidgetProvider, ,
BroadcastReceiver ( ). BroadcastReceiver , Android.
RemoteViews (, ), Android ,
, . Android
. , AppWidgetProvider
.
, , ..
. ,
BroadcastReceiver.
AppWidgetProvider . ,
, , , . , .
, .
, (, ).
.

7.

Android Application Development For Dum.indb 177

177

08.11.2011 12:24:00


AppWidget , .
, , , AppWidgetProvider. AppWidget.java ,
7.1. , , ; .

7.1.
public class AppWidget extends AppWidgetProvider {
@Override
public void onReceive(Context ctxt, Intent intent) {
if (intent.getAction()==null) {
// -
} else {
super.onReceive(ctxt, intent);
}
}

1
4
5
8
10

@Override
public void onUpdate(Context context, AppWidgetManager
appWidgetManager, int[] appWidgetIds) {
15
// -
}
}

.
 1. ,
AppWidget AppWidgetProvider.
 4. onReceive(),
, RemoteViews.
, , . Intent
PendingIntent, .
 5. , Intent
. . , . , ,
.
 8. . , .
 10. . -
, . , ,
(. ).
, , , , (. 15) .

178

Android Application Development For Dum.indb 178

II. Android

08.11.2011 12:24:00

 15. onUpdate() Android , . , . ,


30 ,
. , .


Android ,
, . ,
. (. . 7.2)
, .
. , . 7.2
. , (,
-), (. 7.4).

. 7.4.

,
. - . 7.4 , .
. (. . 7.4)
.

7.

Android Application Development For Dum.indb 179

179

08.11.2011 12:24:00

, XML res/layouts.
widget.xml. widget.xml , 7.2.

7.2. widget.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/phoneState"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/icon"
android:clickable="true" />
</RelativeLayout>

, .
RelativeLayout ImageView, . true
clickable 9 7.2.
src ImageView, . , ( , ), ,
. , , , . , . ,
. , , ,
. , ToggleService src (. ).
. phone_state_normal , , phone_state_silent
. , .

AppWidgetProvider
AppWidgetProvider, (
). , .
, ,
AppWidgetProvider. ,
, -, , -, . .

180

Android Application Development For Dum.indb 180

II. Android

08.11.2011 12:24:00

IntentService
,
? .
, , Android,
ANR
(Application Not Responding ). ANR,
. , ,
, , . . Android ,
,
, .
Android , , ,
.
, , ANR. , , , , . ,
, , Android ANR.
, . ,
,
IntentService, ,
, .
, ,
IntentService , . , .
, .

AppWidgetProvider IntentService
AppWidget , 7.3.

7.3. AppWidget
public class AppWidget extends AppWidgetProvider {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction()==null) {
context.startService(new Intent(context,
ToggleService.class));
} else {
super.onReceive(context, intent);
}
}

@Override

7.

Android Application Development For Dum.indb 181

181

08.11.2011 12:24:00

public void onUpdate(Context context, AppWidgetManager


appWidgetManager, int[] appWidgetIds) {
context.startService(new Intent(context,
ToggleService.class));
}

16

public static class ToggleService extends IntentService { 19


public ToggleService() {
super("AppWidget$ToggleService");
22
}
@Override
protected void onHandleIntent(Intent intent) {
26
ComponentName me=new ComponentName(this,AppWidget.class);27
AppWidgetManager mgr=AppWidgetManager.getInstance(this);28
mgr.updateAppWidget(me, buildUpdate(this));
29
}
private RemoteViews buildUpdate(Context context) {
30
RemoteViews updateViews=new
RemoteViews(context.getPackageName(),R.layout.widget); 32
AudioManager audioManager =
(AudioManager)context.getSystemService(
Activity.AUDIO_SERVICE);
34
if(audioManager.getRingerMode() ==
AudioManager.RINGER_MODE_SILENT) {
updateViews.setImageViewResource(R.id.phoneState,
R.drawable.phone_state_normal);
40
audioManager.setRingerMode(
AudioManager.RINGER_MODE_NORMAL);
} else {
updateViews.setImageViewResource(R.id.phoneState,
R.drawable.phone_state_silent);
45
audioManager.setRingerMode(
AudioManager.RINGER_MODE_SILENT);
}
Intent i=new Intent(this, AppWidget.class);
49
PendingIntent pi
= PendingIntent.getBroadcast(context, 0, i,0);
52
updateViews.setOnClickPendingIntent(
R.id.phoneState,pi);
54
return updateViews;
56
}
}
}

, .
 6. ToggleService. context
Context
Android, . onReceive() onUpdate(). , Android , .

182

Android Application Development For Dum.indb 182

II. Android

08.11.2011 12:24:00




onReceive()
.
16. , 6, .
19. IntentService,
, MainActivity (.. ),
. .
22. , . , , , . - ,
.
26. onHandleIntent() ,
. 616.
(.. ),
. ,
26 .
Intent, .
Intent , ToggleService
.
27. ComponentName,
AppWidgetManager (. ) , RemoteViews.
28. AppWidgetManager
AppWidgetmanager.getInstance(). AppWidgetManager
.
.
29. updateAppWidget(). ComponentName
RemoteViews. Android
ComponentName, 27, . RemoteViews
.
30. buildUpdate().
RemoteViews, 29. ,
.

 32. RemoteViews , buildUpdate().


R.layout.widget 7.3.

7.

Android Application Development For Dum.indb 183

183

08.11.2011 12:24:00

 34. AudioManager
. , , ,
,
.
 40. RemoteViews, .
, MainActivity .
RemoteViews R.id.phoneState ImageView
R.drawable.phone_state_normal ( . 7.2).
 45. else, , ImageView, R.drawable.phone_state_silent,
( ).
 49. Intent, AppWidget.
 52. . . ,
, , PendingIntent. PendingIntent, , 49.
 54. RemoteViews, . RemoteViews , . , RemoteViews,
.
PendingIntent, 52, , .
setOnClickPendingIntent().
:
( , )
pi, , 52. , ImageView.
 56. RemoteViews, updateAppWidget()
29 .
drawable-mdpi phone_state_
normal.png phone_state_silent.png. Eclipse
drawable-mdpi
Refresh (). phone_
state_normal phone_state_silent.
.png.

184

Android Application Development For Dum.indb 184

II. Android

08.11.2011 12:24:00


, . ,
Silent Mode Toggle , .
XML. ,
Android ,
. .
1. Eclipse res
NewNew Folder ( ).
2. xml Finish ().
3. res/xml
NewAndroid XML File ( Android XML).
4. widget_provider.xml.
5. AppWidget Provider ( ), . Finish.
6. XML, widget_provider.
xml, .
widget_provider.xml.
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android=
"http://schemas.android.com/apk/res/android"
android:minWidth="79px"
android:minHeight="79px"
android:updatePeriodMillis="1800000"
android:initialLayout="@layout/widget"
/>

minWidth minHeight , . ,
.
updatePeriodMillis , . Silent Mode Toggle
. , 30 . , 30
, , AppWidgetProvider
onUpdate().
initialLayout , . ,
RemoteViews onReceive() .
,
. , initialLayout ,
.

7.

Android Application Development For Dum.indb 185

185

08.11.2011 12:24:00

initialLayout , . , , , , . , initialLayout
TextView ,
AppWidgetProvider.
Silent Mode Toggle,
Widgets. Silent Mode Toggle. , .
,
ApplicationManifest.xml IntentService
BroadcastReceiver. , .


, Activity, Service
BroadcastReceiver ( ),
. Android .
Activity, Service BroadcastReceiver, , , , .
, ,
AppWidgetProvider BroadcastReceiver, , .
AppWidgetProvider IntentService , Eclipse AndroidManifest.xml
, 7.4. , .

7.4. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=
"http://schemas.android.com/apk/res/android"
package="com.dummies.android.silentmodetoggle"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:debuggable="true">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name=
"android.intent.category.LAUNCHER" />
</intent-filter>

186

Android Application Development For Dum.indb 186

II. Android

08.11.2011 12:24:00

</activity>
<receiver android:name=".AppWidget"
android:label="@string/app_name"
android:icon="@drawable/icon">
<intent-filter>
<action
android:name=
"android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
</receiver>
<service android:name=".AppWidget$ToggleService" />
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>

18

21

25

, .
 18. ,
BroadcastReceiver . name .
AppWidget, AppWidget.java.
.
 21. (
), , .

Android ,
.
APPWIDGET_UPDATE . updatePeriodMillis,
widget_provider.xml. , , , ..
 25. , . Android
, ,
.
.
RunRun ()
<Ctrl+F11>. .
, Home (). .

7.

Android Application Development For Dum.indb 187

187

08.11.2011 12:24:00


Android ,
.
Android .
(, , , , ). .
1. . , .
2. Add to Home screen ( ), . 7.5. Widgets.
3. Choose widget ( ) Silent Mode Toggle (. 7.6).

. 7.5.

. 7.6.

! Silent Mode Toggle


(. 7.7). ( ), , : (. . 7.2) .

188

Android Application Development For Dum.indb 188

II. Android

08.11.2011 12:24:00

. 7.7. Silent Mode Toggle


7.

Android Application Development For Dum.indb 189

189

08.11.2011 12:24:00


Android Market
...








Android Market


Android Market

Android Market Android, Google.


Android Market,
, . , ,
, , .

.
Android Market (. ), .
Android Market.
, . , .


,
Android . .
? , ,
. APK (Android Package File Android).
APK.

Android Application Development For Dum.indb 190

08.11.2011 12:24:00


APK , .
uses-sdk AndroidManifest.xml. Silent Mode Toggle uses-sdk
minSdkVersion, 4.
<uses-sdk android:minSdkVersion="4" />

minSdkVersion Android,
. 4. Silent Mode Toggle
Eclipse 8. ? ?
Android ( )
. , 3
4. . , Android,
, . ,
,
. ,
,
, . ,
Android , .
, , ,
. , 4,
, , Android 4 .
minSdkVersion Android Market , , Android, . minSdkVersion=4,
, 3 (.. Android 1.5) ,
. Android Market
3 . 4 ,
.
!

uses-sdk
minSdkVersion, Android Market 0. , Android. ,
(, Bluetooth,
Android 2.0), , ,
. ,
. ,
, , .

8. Android Market

Android Application Development For Dum.indb 191

191

08.11.2011 12:24:00


Android APK :
 ADT, Eclipse;
 ,
, , , Hudson Continuous Integration Server;
 Ant;
 Maven.
APK ADT Eclipse. , ,
( ) Android
APK.
APK , ADT
. Ant
APK Android:
http://d.android.com/guide/publishing/app-signing.html


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

,
.
 ,
. , APK
.
 , . , .
 ADT
- , APK ,
Keytool Jarsigner.

192

Android Application Development For Dum.indb 192

II. Android

08.11.2011 12:24:00

, .
. .
. ,
. , ,
.
Android.
,
. APK
:
http://d.android.com/guide/publishing/app-signing.html


Android ( Java) , .
.
 ADT. ADT APK,
.
 Keytool.
. tools Android SDK.
.
APK ADT.


, Android Android
Market. .
,
. , ,
Android Market , . , Android. , .
Android Market (. ).

APK
APK ADT, .
1. Eclipse.
2. Silent Mode Toggle
Android ToolsExport Signed Application Pacage
( Android ).

8. Android Market

Android Application Development For Dum.indb 193

193

08.11.2011 12:24:00

(. 8.1) .

. 8.1.

3. Next ().
Keystore Selection ( ), . 8.2.

. 8.2.

4. , Create new keystore ( ).


5. Location () .
Android E:\android.
.keystore. :
E:\android\chainik.keystore

194

Android Application Development For Dum.indb 194

II. Android

08.11.2011 12:24:01

6. , . Confirm ().
7. Next.
Key Creation ( ).
8. .
Alias (). .
Password () Confirm (). .
Validity ( ). , , , .
.
9. ( , ):
First and Last Name ( );
Organization Unit ();
Organization ();
City or Locality ( );
State or Province ( );
Country Code ( ).
, . 8.3.

. 8.3.

10. Next.
(. 8.4).

8. Android Market

Android Application Development For Dum.indb 195

195

08.11.2011 12:24:01

. 8.4. APK

11. .apk.
12. Finish ().
.apk .keystore (. 8.5).

. 8.5. .apk .keystore

Android Market
, APK, Android Market. Android
Market. , Google.
Google, Gmail. Google, http://www.google.com/accounts. Android Market
. ,
25 . , .

196

Android Application Development For Dum.indb 196

II. Android

08.11.2011 12:24:01

1. - http://market.android.com/
publish.
2. Google (. 8.6).

. 8.6.

3. .
Developer Name ( ). ( ),
. . Developer Name
.
Email Address ( ). , . .
Website URL (URL -). -. , Blogger, . Android Market
-. Blogger
www.blogger.com.
Phone Number ( ). , .
, . 8.7.
4. Continue ().

25 (. 8.8).
5. Continue (), Google Checkout.

8. Android Market

Android Application Development For Dum.indb 197

197

08.11.2011 12:24:01

. 8.7.

. 8.8.

6. , . 8.9, . Agree and Contingue


( ).
Google,
.
Continue.
7. (. 8.10) Sign in
and continue ( ).

198

Android Application Development For Dum.indb 198

II. Android

08.11.2011 12:24:01

. 8.9.

. 8.10.

8. Android Market

Android Application Development For Dum.indb 199

199

08.11.2011 12:24:01

8. (. 8.11)
Place your order now ( ).
, .
, , ,
Android (. 8.12).

. 8.11.

. 8.12. ,

200

Android Application Development For Dum.indb 200

II. Android

08.11.2011 12:24:01

9. Android Market Developer Site ( Android Market).


(. 8.13).

. 8.13.

10. Android Market , Google.


11. , I agree... ( ...) I agree,
Continue ( , ).
Android
(. 8.14).
Google Checkout
,
Android Market, Google Checkout.
Setup
Merchant Account ( )
:
 ;

 ( );
 .
Google Checkout
.
. 11 , .
.

8. Android Market

Android Application Development For Dum.indb 201

201

08.11.2011 12:24:01

. 8.14.


APK,
Android. .
, . : ?
,
. ,
. ( ) ,
, .
Android Market. ,
, . Android
Market ,
0,99 9,99 . 10 .
, . , ,
, . , ,
.
.
,
. , , . ,
.

202

Android Application Development For Dum.indb 202

II. Android

08.11.2011 12:24:01


,
( 24 , ;
). , , .
. .
.
,
. ,
.
Android Market 24
. , , .
Google Checkout
24 . Google Checkout.
24 .
, .
24 ,
, .
, , , .


, , . , 5080% , ,
, .
? ?
: . ,
. . , , Google AdSense, AdMob ( Google) Quattro Wireless
( Apple).
. Android.
( 60 ), ,
, .



Android Market,

8. Android Market

Android Application Development For Dum.indb 203

203

08.11.2011 12:24:01

.
, , .
, , . , , , .
.
, Android. .
1. .
2. Eclipse DDMS.
3. ( ) Devices (),
. 8.15.
4. Screen Shot ( ), .
5. Save ()
.

. 8.15. DDMS

204

Android Application Development For Dum.indb 204

II. Android

08.11.2011 12:24:01

Android Market
, Android
. , .
1. Android (. . 8.14) Upload
Application ( ).
(. 8.16).

. 8.16. Android Market

2. Application .apk file ( .apk ) APK,


, Upload ().
Android Market Java .
. Silent Mode Toggle
com.dummies.android.silentmodetoggle. , Android Market, ,

8. Android Market

Android Application Development For Dum.indb 205

205

08.11.2011 12:24:02

3.

4.

5.

6.

7.

8.
9.

10.

Silent Mode Toggle, , , ,


.
, .
Screenshots ( ) .
320480 480854 .

. ,
, , , , , . .
.
180120 .
,
. Android Market
.
.
.
Silent
Mode Toggle Widget.
Android Market. , ,
, , .
.
, Android
Market, , , .
Android Market.
.
Android Market.
, , . ,
,
Android Market.
, .
.
Application ().
.
Silent Mode Toggle Productivity (), .
(copy
protection).

206

Android Application Development For Dum.indb 206

II. Android

08.11.2011 12:24:02

Off (). On ()
.
, 2 , 4 .
,
,
.
( Android 2.2) SD. , ,
. APK ,
.

11. , .
, , All locations ( ) Italy (). Android Market . All locations,
.
12. Web Site (-) E-mail ( ). Phone ().
Phone, - .
. - ,
,
, . Web Site E-mail

.
13. , Android Market . ,
.
14. .
Publish ().
Android Market.
Save (). , .
Delete (). .
Save. , Android.
Saved Draft ( ), ,
(. 8.17). (,
),
.

8. Android Market

Android Application Development For Dum.indb 207

207

08.11.2011 12:24:02

. 8.17.

15. ,
Android.
(. . 8.16).
16. , Publish (),
.
Android Market.
. 8.18 Silent
Mode Toggle Widget,
Android Market
Nexus One. , Android Market,
Apps Productivity ( ),
, Just in
( ), .

,
,
. . .
, .
. 8.18. , -
.
Android Market

208

Android Application Development For Dum.indb 208

II. Android

08.11.2011 12:24:02

Silent Mode Toggle Widget Android


Market, , .
,
. ,
Android, Unpublish ( ).



, . , ,
, . ,
, ,
, Android ,
, ,
. , .
, Android Market.
 . .
. ,
, .
 . ! , .
, ,
.
,
, , .
 . , , , , ,
. , , .
, . ,
Android 2.2 .
 . , , ,
. , .
. , .

8. Android Market

Android Application Development For Dum.indb 209

209

08.11.2011 12:24:02

 . ,
Android Market, -.
,
.
, Android
Market. . , . ,
24 , . , 4 . ,
, ,
, , , ,
.
,
,
,
.
2011 Amazon Google App Store,
Android Market.
, , .
App Store,
, Amazon, .
, Amazon .
http://developer.amazon.com.

210

Android Application Development For Dum.indb 210

II. Android

08.11.2011 12:24:02

III

The 5th Wave

! !
,

Android Application Development For Dum.indb 211

08.11.2011 12:24:02


III Android,
II, . , II,
, Android. ,
, .
, ,
. ,
.

Android Application Development For Dum.indb 212

08.11.2011 12:24:02

,

...





Android ,
Android . Task Reminder ( ). .
Task Reminder ,
.


,
, .
 . - , , , .
 , .
 .
 , .
 .
 ,
.
, Android. Task Reminder ,
.

Android Application Development For Dum.indb 213

08.11.2011 12:24:02


, - ,
Windows cron Linux. Windows
, . Unix Linux cron ( / )
.
Android Linux 2.6, , crontab,
cron. , , Android cron, crontab.
Android AlarmManager, .
AlarmManager , . . Task Reminder AlarmManager

-.


. , , , , .
, , Task Reminder.
 Java.
 SQLite.
 SQLite
AlarmManager
.


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

214

Android Application Development For Dum.indb 214

III.

08.11.2011 12:24:02

. ,
.
 , .
, .
. Task Reminder
, . ,
, - .
 NotificationManager ( )
, .
. ,
. ,
.
 .
, .
, .
, .
Task Reminder
NotificationManager.


Task Reminder , , ,
.
.
. , . , .


Eclipse Android Android 2.2 SDK 4. ,
. . 9.1. Android 9 ,
. Eclipse , . 9.1.

9. ,

Android Application Development For Dum.indb 215

215

08.11.2011 12:24:02

9.1.

Project Name ( )

Task Reminder

Build Target ( )
Application Name ( )

Android 2.2 (API Level 8)


Task Reminder

Package Name ( )

com.dummies.android.taskreminder

Create Activity ( )

ReminderListActivity

Min SDK Version ( SDK)

ReminderListActivity.
MainActivity,
, , .
ListActivity,
ReminderListActivity.


ListActivity , list (), res/layout.
main.xml, res/layout,
reminder_list.xml.
Eclipse
RefactorRename ().
<Shift+Alt+R>.
, setContentView()
ReminderListActivity.java. ReminderListActivity.
java .
ReminderListActivity ListActivity
Activity. .
ReminderListActivity , 9.1.

9.1. ReminderListActivity
public class ReminderListActivity extends ListActivity {
/** */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_list);
}
}

ReminderListActivity reminder_list,
, . ListActivity,
, 9.2.

216

Android Application Development For Dum.indb 216

III.

08.11.2011 12:24:02

9.2. reminder_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_reminders"/>
</LinearLayout>

11

.
 5. ListView Android,
,
. id ListView @
id/android:list @+id/android:list.
 8. . , , 8.
, ListView , . id
@id/android:empty @+id/
android:empty.
 11. no_reminders ( ) , (.. ) .
res/values/strings.xml no_
reminders .


Task Reminder ,
. , , .
Eclipse ,
. ReminderEditActivity.
src
NewClass (). <Shift+Alt+N>
Class. Superclass ( )
Java android.app.Activity Finish
().
Eclipse . ,
.

9. ,

Android Application Development For Dum.indb 217

217

08.11.2011 12:24:02

public class ReminderEditActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_edit);
}
}

5 reminder_
edit, . reminder_edit
,
.
Android
ReminderEditActivity, .
activity application AndroidManifest.xml, .

<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".ReminderListActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name=
"android.intent.action.MAIN" />
<category android:name=
"android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ReminderEditActivity"
android:label="@string/app_name" />
</application>
!

AndroidManifest.xml,
,
, Android .


.
 . .
 . , , .
 . , .
 . , .

218

Android Application Development For Dum.indb 218

III.

08.11.2011 12:24:02

, . 9.1.

. 9.1.

, res/
layout reminder_edit.xml (,
, , ). .
1. Eclipse res/layout
NewAndroid XML File (
Android XML).
2. File reminder_edit.xml.
3. Layout (), , .
4. res/layout.
5. Select the root element (
) ScrollView ( ).
6. Finish ().
, , . 9.1.
reminder_edit.xml , 9.3.

9. ,

Android Application Development For Dum.indb 219

219

08.11.2011 12:24:02

9.3. reminder_edit.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
5
<LinearLayout
6
android:orientation="vertical"
7
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title" />
12
<EditText android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
15
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/body" />
18
<EditText android:id="@+id/body"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="5"
android:scrollbars="vertical"
android:gravity="top" />
24
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/date" />
27
<Button
android:id="@+id/reminder_date"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
31
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/time" />
34
<Button
android:id="@+id/reminder_time"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
38
<Button android:id="@+id/confirm"
android:text="@string/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
42
</LinearLayout>
</ScrollView>

220

Android Application Development For Dum.indb 220

III.

08.11.2011 12:24:02

, .
 5. ScrollView,
,
. . 9.1 . , 90,
, .
ScrollView
.
, ,
.
 6. ScrollView . LinearLayout, .
 7. ,
.
 12. . , string.
xml title .
 15. , .
 18. .
 24. , .
minLines ( ) 5,
gravity () top (). ,
, , .
 27. . . strings.xml
date .
 31. . DatePickerDialog, Android.
, .
 34. .
. . strings.xml
time .
 38. .
TimePicker,
Android. ,
.
 42. , ,
, .

9. ,

Android Application Development For Dum.indb 221

221

08.11.2011 12:24:02

strings.xml body confirm, .


ListActivity , , , . ,
, , ,
. ( Cursor)
- ,
.
res/layout
TextView reminder_row.xml. , 9.4.

9.4. reminder_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"/>

,
10 ,
. text1,
.
TextView Android
. Android Android simple_list_
item_1 Android.R.layout, ,
XML.
:
http://android.git.kernel.org/?p=platform/frameworks/
base.git;a=blob;f=core/res/res/layout/simple_list_item_1.xml;
h=c9c77a5f9c113a9d331d5e11a6016aaa815ec771;hb=HEAD

:
http://bit.ly/9GzZzm

ListActivity ,
. , ,
. 12

222

Android Application Development For Dum.indb 222

III.

08.11.2011 12:24:02

SQLite, , . ,
ListActivity , setListAdapter().


onCreate() ReminderListActivity.java
setContentView() .
String[] items =
new String[] {"1","2","3","4"}; 1
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.reminder_row,
R.id.text1, items);
4
setListAdapter(adapter);
5

,
.
 1. , .
 4. ArrayAdapter .
ListView . .
Java, , ArrayAdepter. ArrayAdapter .
this. (
Context, ).
R.layout.reminder_row . ,
ListView .
R.id.text1. TextView R.layout.
reminder_row. .
items. , ListView.
 5. setListAdapter(), ListActivity,
ListView. ListView
ArrayAdapter, 4.
Task Reminder . Eclipse
RunRun () <Ctrl+F11>.
ListView (. 9.2).

ListActivity. 12
ListActivity SQLite.


,
. View
.

9. ,

Android Application Development For Dum.indb 223

223

08.11.2011 12:24:03

. 9.2.

 . . .
 . . .
. ListActivity. 11 Button.


ListActivity Android , (.. ). .
ReminderListActivity.java onCreate().
@Override
protected void onListItemClick(ListView l, View v,
int position, long id) {
super.onListItemClick(l, v, position, id);
}


onListItemClick(), ListActivity.
.
 1. ListView, .
 v. ListView, .
 position. , .
 id. , .

224

Android Application Development For Dum.indb 224

III.

08.11.2011 12:24:03

, , .
, ,
ReminderEditActivity. , .


( , ) , .
ListActivity,
onCreate() ReminderListActivity:
registerForContextMenu(getListView());

registerForContextMenu() ,
. . , . registerForContextmenu()
View, ListActivity .
getListView() ListView, .
getListView() ListActivity.
, ListView, , .
ListView ,
registerForContextMenu(), onCreateContextMenu(). . onCreateContextMenu() .
ReminderListActivity .
@Override
public void onCreateContextMenu(ContextMenu menu,
View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
}

.
 menu. .
 v. , (..
).
 menuInfo. , .
v.
, .
, , ,
. , Delete (). Delete 10.

9. ,

Android Application Development For Dum.indb 225

225

08.11.2011 12:24:03


, , . ( Task
Reminder), .
.
, . ,
, .

Android. 7 , . .



Android. Intent () ,
Android ( ). ,
, Android.
, ,
. , .
. ReminderListActivity
onListItemClick().
@Override
protected void onListItemClick(ListView l, View v,
int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this,
ReminderEditActivity.class);
i.putExtra("RowId", id);
startActivity(i);
}

4
5
6

.
 4. Intent, this
ReminderEditActivity, .
 5. Intent.
-.
RowId, , . ,

226

Android Application Development For Dum.indb 226

III.

08.11.2011 12:24:03

ReminderEditActivity Intent .
, ,
12 ReminderEditActivity .
 6. .
Android
Android ,
.


, . . ,
, .
RowId. 12 RowId
ReminderEditActivity SQLite .
,
onCreate() ReminderEditActivity.
if(getIntent() != null) {
Bundle extras = getIntent().getExtras();
int rowId =
extras != null ? extras.getInt("RowId") : -1;
// rowId
}

1
2
3

.
 1. getIntent() Activity.
. , null,
.
 2. Bundle ( /)
getExtras().
 3. ,
Bundle. , RowId ,
getInt().
RowId, 12 SQLite
Task .
SQLite ( 12), , , .

9. ,

Android Application Development For Dum.indb 227

227

08.11.2011 12:24:03


Android ,
. , , SMS,
Twitter, Facebook Google Latitude.
Android . Task Reminder ,
, . 9.5 , .

9.5.
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, ", !");
i.putExtra(Intent.EXTRA_SUBJECT, " ");
Intent chooser = Intent.createChooser(i,
" ?");
startActivity(chooser);

1
2
3
4
5
6

.
 1. , Android - , , .
 2. . MIME. MIME . . , ,
.
 3. ,
, . ,
. .
, ,
-. ,
, .
,
.
 4. 3,
. , .
 5. . Intent , . . .
 6. . , .

228

Android Application Development For Dum.indb 228

III.

08.11.2011 12:24:03

, 9.5, . 9.3.
, , Android ,
(. 9.4).

. 9.3. ;

!

. 9.4. ,


. startActivity()
, .
9.5 ,
startActivity(chooser) startActivity(i).
, Android
. ,
, . , ,
, . Android ,
DDMS , . , , .

, , . Android, , .

9. ,

Android Application Development For Dum.indb 229

229

08.11.2011 12:24:03

10


...




, ,
!
, , , .
Android.
Android
. .
 . ,
. , ,
<MENU>. .
. , .
. .
.
. ,
. More (),
. More , .
.
 .
Linux Windows, , ( ).
 . , . Android 2.2
, Linux Windows,
.

. - , Task Reminder .

Android Application Development For Dum.indb 230

08.11.2011 12:24:03


Android Android
Market, , , , . ?
, ,
. :
 , ;
 ;
 , , .
, , . . ,
, , ,
. ,
. . , ,
, . ,
. , .


Java, XML, res/menu. XML
,
. .

XML
XML, .
1. res menu.
2. list_menu.xml.
3. , 10.1.

10.1. ReminderListActivity
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android=
"http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_insert"
android:icon="@android:drawable/ic_menu_add"
android:title="@string/menu_insert" />
</menu>

10.

Android Application Development For Dum.indb 231

231

08.11.2011 12:24:03

, . . 4 . android:icon
Android.
drawable . Android
ldpi, mdpi hdpi. ,
android.R.drawable http://developer.
android.com/reference/android/R.drawable.html.
android.R .
, .
4. strings.xml menu_insert .
5. ReminderListActivity .
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.list_menu, menu);
return true;
}

4 MenuInflater, XML.
inflate .
, onCreateOptionsMenu().
6. MENU.
(. 10.1).


, . -
.
ReminderListActivity.
@Override
public boolean onMenuItemSelected(int featureId,
MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_insert:
createReminder();
return true;
}
return super.onMenuItemSelected(featureId, item);
}

232

Android Application Development For Dum.indb 232

2
3
4
5
6

III.

08.11.2011 12:24:03

. 10.1.

, .
 2. , . featureId
, .
item , .
 3. .
switch .
getItemId() MenuItem.
 4.
. 10.1.
 5. ,
createReminder(),
.
 6. true,
onMenuItemSelected() , .
createReminder() ,
: , . .

10.

Android Application Development For Dum.indb 233

233

08.11.2011 12:24:03


createReminder() ReminderEditActivity, . ReminderListAcitvity.
private static final int ACTIVITY_CREATE=0;
private void createReminder() {
Intent i =
new Intent(this, ReminderEditActivity.class);
startActivityForResult(i, ACTIVITY_CREATE);
}

,
ReminderEditActivity. startActivityForResult() ,
.
, , . ReminderEditActivity
.
.
 Intent i. , ReminderEditActivity.
 ACTIVITY_CREATE. ,
onActivityResult(). .
ACTIVITY_CREATE
ReminderListActivity.
private satatic final int ACTIVITY_CREATE=0;


ReminderEditActivity. onActivityResult(). : , , ,
.
ReminderListActivity.
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
//
}

, 12, SQLite.
.

234

Android Application Development For Dum.indb 234

III.

08.11.2011 12:24:03

 requestCode. , startActivityForResult().

, ( ) switch. onMenuItemSelected()
switch.
 resultCode. ,
setResult(). , , - .
, .
 intent. ,
. putExtra().
, onActivityResult().
, , .


, .
.
, . XML Java
MenuInflater. , ,
registerForContextMenu() . 9
.
onCreateContextMenu(). 9.
Task Reminder , . . ,
Android , .

XML
, XML res/menu.
list_menu_item_longpress.xml .
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android=

10.

Android Application Development For Dum.indb 235

235

08.11.2011 12:24:03

"http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_delete"
android:title="@string/menu_delete" />
</menu>

title menu_delete.
strings.xml menu_delete
. , . ,
, .


,
onCreateContextMenu() ReminderListActivity.
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.list_menu_item_longpress, menu);
}

, onCreateOptionsMenu() . , XML
, . ,
ReminderListActivity , (. 10.2). ,
onContextMenuItemSelected(), .



, . ,
ReminderListActivity.
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_delete:
//
return true;
}
return super.onContextItemSelected(item);
}

236

Android Application Development For Dum.indb 236

2
3
4

III.

08.11.2011 12:24:03

. 10.2.

, .
 2. ,
. item .
 3. switch, , list_menu_
item_longpress.xml.
 4. menu_delete list_menu_item_longpress.xml. strings.xml
menu_delete .
, ( 5)
, .
. 12 ,
SQLite.
list_menu_item_longpress.xml . , switch
onContextmenuItemSelected() .

10.

Android Application Development For Dum.indb 237

237

08.11.2011 12:24:03

11


...





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


EditText.
. EditText

.

EditText
9 reminder_edit.xml, .
<EditText android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

. , .
EditText
, , . Android ,
. EditText, reminder_edit.xml.

Android Application Development For Dum.indb 238

08.11.2011 12:24:03

<EditText android:id="@+id/body"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="5"
android:scrollbars="vertical"
android:gravity="top" />

Task Reminder
. ,
,
, . .
 minLines. EditText. EditText
TextView , minLines. EditText
5 . , , . , :
, .
 scrollbars. ,
,
. .
 gravity.
EditText, . ,
, .. .
, gravity top.

. 11.1. gravity ,

11.

Android Application Development For Dum.indb 239

239

08.11.2011 12:24:03


EditText ,
. , , .
,
. EditText
.
. , ,
. . , @. ,
,
.
inputType
. , .
:
http://developer.android.com/reference/android/widget/
TextView.html#attr_android:inputType


. ,
, , -
.
, , ,
. , Android ,
. Android : DatePicker
TimePicker. .
, .
DatePicker TimePicker , Dialog,
, DatePicker TimePicker.


DatePicker TimePicker Task
Reminder. . reminder_edit.xml
, DatePicker TimePicker,
, 11.1.

240

Android Application Development For Dum.indb 240

III.

08.11.2011 12:24:04

11.1. ( )
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/date" />
<Button
android:id="@+id/reminder_date"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/time" />
<Button
android:id="@+id/reminder_time"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>

12

.
 1. TextView . date, .
 4. , DatePickerDialog, .
 9. . .
 12. ,
TimePickerDialog.


,
. , .


,
, ,
ReminderEditActivity.java.
onCreate() :
registerButtonListenersAndSetDafaultText()

Eclipse , .
. . , , , registerButtonListenersAndSetDefaultT
ext(). , 11.2.

11.

Android Application Development For Dum.indb 241

241

08.11.2011 12:24:04

11.2.
mDateButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_PICKER_DIALOG);
}
});
updateDateButtonText();
updateTimeButtonText();

1
4
5

8
9

, .
 1. mDateButton.
, .
.
private Button mDateButton;

onCreate()
setContentView().
mDateButton = (Button) findViewById(R.id.reminder_date);

onClickListener() . , .. 5.
 4. ,

.
 5. , ,
showDialog() .
, .
, DATE_
PICKER_DIALOG.
, .
private static final int DATE_PICKER_DIALOG = 0;
private static final int TIME_PICKER_DIALOG = 1;


showDialog(),
.
 8. , .
11.5.
 9. , .
11.5.

242

Android Application Development For Dum.indb 242

III.

08.11.2011 12:24:04

showDialog()
showDialog() .
showDialog()
onCreateDialog(), . showDialog(),
ReminderEditActivity , 11.3.

11.3. showDialog()
onCreateDialog()
@Override
protected Dialog onCreateDialog(int id) {
switch(id) {
case DATE_PICKER_DIALOG:
return showDatePicker();
}
return super.onCreateDialog(id);
}

2
4

private DatePickerDialog showDatePicker() {


DatePickerDialog datePicker =
new DatePickerDialog(ReminderEditActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear,
int dayOfMonth) {
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, monthOfYear);
mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDateButtonText();
}
}, mCalendar.get(Calendar.YEAR),
mCalendar.get(Calendar.MONTH),
mCalendar.get(Calendar.DAY_OF_MONTH));
return datePicker;
}

10

private void updateDateButtonText() {


SimpleDateFormat dateFormat =
new
SimpleDateFormat(DATE_FORMAT);
String dateForButton =
dateFormat.format(mCalendar.getTime());
mDateButton.setText(dateForButton);
}

29

11.

Android Application Development For Dum.indb 243

13

17
19
21
22

25
26

30
31
32

243

08.11.2011 12:24:04

, .
 2. onCreateDialog() showDialog(). onCreateDialog() id , showDialog().
 4. , ,
onCreateDialog(), , showDialog(). DATE_
PICKER_DIALOG, ,
showDatePicker().
 10. showDatePicker(),
DatePickerDialog.
 13. DatePickerDialog,
. Context
ReminderEditActivity.this. , . onDateSetListener() , 1322.
.
DatePickerDialog 25.
 17. onDateSet(), ,
DatePickerDialog, . .
DatePicker view. , .
int year. .
int monthOfYear. . 0
11 Calendar.
int dayOfMonth. .
 19-21. mCalendar
Calendar. , ReminderEditActivity DatePickerDialog
TimePickerDialog. 1921
Calendar set. mCalendar .
private Calendar mCalendar;
mCalendar = Calendar.getInstance();

,
onCreate(). getInstance()
Calendar .
 22. m C a l e n d a r
,

244

Android Application Development For Dum.indb 244

III.

08.11.2011 12:24:04

DatePickerDialog.
updateDateButtonText(),
2931.
25.
DatePickerDialog.
. ,
get mCalendar. mCalendar , . mCalendar
,
DatePickerDialog ,
mCalendar. .
26. Dialog, onCreateDialog(). DatePickerDialog
Dialog, DatePickerDialog.
onCreateDialog() , .
29. 22, updatgeDateButtonText() mCalendar .
updateDateButtonText() , , .
, , , DatePickerDialog, ,
.
30. SimpleDateFormat, . (, , .).
, Java (http://
download-llnw.oracle.com/javase/1.4.2/docs/api/java/text/
SimpleDateFormat.html), .
DATE_FORMAT
SimpleDateFormat. , . DATE_FORMAT
ReminderEditActivity :
private static final String DATE_FORMAT = "yyyy-MM-dd";

 "yyy-MM-dd" , ,
. . , 22 2010 20100922.
 31. SimpleDateFormat mCalendar getTime() mCalendar. ,
DATE_FORMAT,
SimpleDateFormat 30.

11.

Android Application Development For Dum.indb 245

245

08.11.2011 12:24:04

 32.
setText() Button.
, 31.
DatePickerDialog , .


TimePickerDialog , .


TimePickerDialog DatePickerDialog.
onClickListener(). mTimeButton
ReminderEditActivity :
private Button mTimeButton;

onCreate()
:
mTimeButton = (Button) findViewById(R.id.reminder_time);

,
. , 11.4, regist
erButtonListenersAndSetDefaultText() .

11.4.

mTimeButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_PICKER_DIALOG);
}
});

, , , showDialog() , . , onCreateDialog(), showDialog(), ,


. switch
TimePickerDialog. , TIME_PICKER_DIALOG ( ).
onCreateDialog()
return showDatePicker().
case TIME_PICKER_DIALOG:
return showTimePicker();

246

Android Application Development For Dum.indb 246

III.

08.11.2011 12:24:04

showTimePicker()
11.5.

11.5. showTimePicker()
private TimePickerDialog showTimePicker() {
TimePickerDialog timePicker =
new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view,
int hourOfDay, int minute){
mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
mCalendar.set(Calendar.MINUTE, minute);
updateTimeButtonText();
}
}, mCalendar.get(Calendar.HOUR_OF_DAY),
mCalendar.get(Calendar.MINUTE), true);
return timePicker;
}

5
6
7
8
10
11

11.5 showDatePicker(),
, , .
.
 3. TimePickerDialog OnTimeSetListener(), ,
TimePickerDialog.
 5. , onTimeSet(),
.
 6. Calendar,
.
 7. Calendar,
.
 8.
updateTimeButtonText(), 11.6.
 10. ,
TimePickerDialog.
Calendar, .
 11. ,
TimePickerDialog. Calendar,
.
true, 24
( 12 p.m. a.m.).

11.

Android Application Development For Dum.indb 247

247

08.11.2011 12:24:04

TimePickerDialog onCreateDialog(), .
8 updateTimeButtonText().
updateDateButtonText(), . ,
updateTimeButtonText() ,
. 11.6.
ReminderEditActivity.

11.6. updateTimeButtonText()
private void updateTimeButtonText() {
SimpleDateFormat timeFormat =
new SimpleDateFormat(TIME_FORMAT);
String timeForButton =
timeFormat.format(mCalendar.getTime());
mTimeButton.setText(timeForButton);
}

2
3
4

.
 2. SimpleDateFormat, . TIME_FORMAT,
ReminderEditActivity :
private static final String TIME_FORMAT = "kk:mm";

SimpleDateFormat ,
Calendar ,
12:45 p.m.
 3. , 2.
 4. . timeForButton, 3.

, . .
, .



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

248

Android Application Development For Dum.indb 248

III.

08.11.2011 12:24:04

. , .
AlertDialog.
 .
, .
ProgressDialog.
 . , (.. ). Dialog. Java ,
Dialog,
XML.


- ,
? ,
,
. ,
. : ?
, , . ,
- .
 , - (, ProgressDialog
).
 , EditText, (, ).
 , .
 .
 . ,

GPS SD. .
 , , .
,
, .

11.

Android Application Development For Dum.indb 249

249

08.11.2011 12:24:04

( , -
..),
, ,
.
, ,
. Android . ProgressDialog ProgressBar.


AsyncTask,
.
:
http://d.android.com/resources/articles/painless-threading.html

, AsyncTask .


, .
.
1. ?
. ProgressDialog, , , ,
, . ProgressDialog
Android http://d.android.com/
guide/topics/ui/dialogs.html#ProgressDialog.
. . 2.
2. ?
3. ,
AlertDialog.
. , Dialog.
XML.
Android http://d.
android.com/guide/topics/ui/dialogs.html#CustomDialog.
. . 3.
4. , , :
? ?
. AlertDialog
, onClickListener().
. . 4.

250

Android Application Development For Dum.indb 250

III.

08.11.2011 12:24:04

5. ?
. AlertDialog.
. . 5.
6. ?
. AlertDialog.
. .
- .


. Android
AlertDialog.Builder,
AlertDialog . onClickListener() .
Task Reminder AlertDialog.Builder , , , ,
11.7.
, Task Reminder. (. 11.2), .

. 11.2. AlertDialog

11.

Android Application Development For Dum.indb 251

251

08.11.2011 12:24:04

, , 11.7. , 11.7,
,
.

showDialog() onCreateDialog(),
.

11.7. AlertDialog
AlertDialog.Builder
AlertDialog.Builder builder =
new AlertDialog.Builder(ReminderEditActivity.this);
builder.setMessage(" ?")
.setTitle(" ")
.setCancelable(false)
.setPositiveButton("",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
//
}
})
.setNegativeButton("",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {dialog.cancel();
}
});
builder.create().show();

2
3
4
5
6
7

12
14

17

,
. .
 2. AlertDialog.Builder
this. .
 3. , AlertDialog (.
. 11.2). .
 4. AlertDialog. , .
 5. Cancelable false. ,

AlertDialog, .
 6. . ,

252

Android Application Development For Dum.indb 252

III.

08.11.2011 12:24:04

, . , .
.
 7. 7-11 onClickListener() .
.
9 , .
 12. . , ,
AlertDialog. ,
.
 14. .
onClickListener(). . , , cancel()
Dialog.
 17. create() show().
AdertDialog
.
AlertDialog AlertDialog.
Builder , Dialog. , Dialog
, AlertDialog.
Dialog , AlertDialog , .
( ,
, 11.7),
AlertDialog , 11.7. ReminderEditActivity ,
.
- ReminderListAcitvity. 12
SQLite.
Dialog Android
:
http://d.android.com/guide/topics/ui/dialogs.html


, , , . , -
?

11.

Android Application Development For Dum.indb 253

253

08.11.2011 12:24:04

, .
.
, ? , !
, Android
. , ,
Android.
. .
, . Android,
.
 TextWatcher. EditText , . ,
.
 OnSave. ,
, . , .
 onFocusChanged(). ,
onFocusChanged().
.
Task Reminder ,
.

-, , Toast, . ,
, .
, , , ,
.
Toast.makeText(ReminderEditActivity.this,
" ",
Toast.LENGTH_SHORT).show();

.
, , - . , , ,
.

254

Android Application Development For Dum.indb 254

III.

08.11.2011 12:24:05


.
.
 AlertDialog. ,
,
AlertDialog, . : AlertDialog , , .
 .
EditText , , . , ,
. .
 . Android, .
, . : .
.
, . ,
Google ,
.
. . , . , 14
,
. . ,
,
- .

11.

Android Application Development For Dum.indb 255

255

08.11.2011 12:24:05

12


...





?

SQLite
SQLite


.
Task Reminder. , ,
? , Android Java
.
SQLite.
, , .
SQL, .
.
, , .


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


Android .
 . , -. 15.

Android Application Development For Dum.indb 256

08.11.2011 12:24:05

 . . , ,
. ,
. , ,
, .
 .
, .
getCacheDir() Activtity Context. ,
Android
. 1
.
 . Android
,
. SD . , , . , .
.
,
(, ), .
Environment.
getExternalStorageState(), .
Android 2.2
. getExternalFilesDir() Context.
, , , , , ..

Android :
http://d.android.com/guide/topics/data/
data-storage.html#filesExternal.html

 SQLite. Android
SQLite, SQL
(Structured Query Language ).
SQLite , Android,
iPhone, Windows, Linux Mac,
. Android ,
, SQL .. SQLite ,
Task Reminder .

12.

Android Application Development For Dum.indb 257

257

08.11.2011 12:24:05

 . ,
. , Flickr ,
.
Flickr. ,
- ,
Twitter, Facebook Basecamp. HTTP,
,
.



. , . .
, (, Twitter), ,
. ( )
. SQLite.
, SQLite.
, SQLite
,
. . .
,
Android Market ( ). , -
, .


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

258

Android Application Development For Dum.indb 258

III.

08.11.2011 12:24:05


Android Market,
, . , , ,
., ,
. , .
,
, . ,
, Silent Mode Toggle (
) GPS,
.
Silent Mode Toggle . ,
, , , .
Android Market , .
, ,
.
, . , .


- ,
AndroidManifest.xml . SQLite , Task Reminder , ( 13):
 android.permission.RECEIVE_BOOT_COMPLETED;
 android.permission.WAKE_LOCK.
RECEIVE_BOOT_COMPLETED , . WAKE_LOCK , .
13 AlarmManager.
,
. Android .
SD.
, :
 android.permission.INTERNET;
 android.permission.WRITE_EXTERNAL_STORAGE.

12.

Android Application Development For Dum.indb 259

259

08.11.2011 12:24:05

AndroidManifest.xml
.
 AndroidManifest.xml.
, AddUses Permission (
). .
 AndroidManifest.xml . . uses-permission
element. XML
:
<uses-permission android:name= "android.permission.WAKE_LOCK />

WAKE_LOCK RECEIVE_BOOT_COMPLETED Task


Reminder.
Android:

http://d.android.com/reference/android/Manifest.permission.html
!

, ,
, , . ,
. , . ,
, .

SQLite
Task Reminder , .
SQLite. , , ,
.

SQLite
, Task Reminder,
SQLite. ReminderEditActivity
.
1. .
2. .
3. .
ReminderListActivity .
1. .
2. ,
.

260

Android Application Development For Dum.indb 260

III.

08.11.2011 12:24:05

SQLite android.database.
SQLite Activity. Java ( ,
).
. , , ,
, . .

Java
Android Java ,
. RemindersDbAdapter.java.
, .
, DVD- . ,
Java. Java
SQLite SQLite Java.



. ReminderDbAdapter , 12.1.

12.1. , ReminderDbAdapter
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE =
"reminders";
private static final int DATABASE_VERSION = 1;

1
2
3

public static final String KEY_TITLE = "title";


5
public static final String KEY_BODY = "body";
public static final String KEY_DATE_TIME =
"reminder_date_time";
public static final String KEY_ROWID = "_id";
8
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE + " ("
+ KEY_ROWID + " integer primary key autoincrement,
+ KEY_TITLE + " text not null, "
+ KEY_BODY + " text not null, "
+ KEY_DATE_TIME + " text not null);";

11
12
14

private final Context mCtx;


public RemindersDbAdapter(Context ctx) {
this.mCtx = ctx;
}

21
23

12.

Android Application Development For Dum.indb 261

"

261

08.11.2011 12:24:05

.
 1. ,
Android.
 2. , .
.
 3. .
.
onUpgrade() DatabaseHelper (. ).
 5-8. .
 11. DatabaseHelper . DatabaseHelper
SQLiteOpenHepler Android.
 12.
SQLite, , , .
 14. , . .
.
 21. , SQLite.
 23. .
DATABASE_CREATE, .

SQLite
SQLite , Task Reminder. ,

. , , ,
(. 12.1). 58 12.1.
, 0100100101010010. ,
:
,
, . 12.1.

. 12.1. SQLite

262

Android Application Development For Dum.indb 262

III.

08.11.2011 12:24:05

14 12.1 ,
. , . SQLite
reminders data.
, .
 create table DATABASE_TABLE. ,
SQLite reminders.
 ROW_ID. .
integer primary key autoincrement. integer ,
. primary key
, ROW_ID
. autoincrement SQLite
(.. ). ,
1, 2 3,
ROW_ID 4.
 KEY_TITLE. . text SQLite ,
. not null ,
null, ..
- .
 KEY_BODY. . , KEY_TITLE.
 KEY_DATE_TIME. .
, . ,
, , . , SQLite,
Java, ,
. , , Java
.
SQLite http://www.sqlute.org/datatype3.
html#datetime.


.
SQLiteOpenHelper. RemindersDbAdapter ,
12.2. Java
RemindersDbAdepter.

12.2. ,
private static class DatabaseHelper
extends SQLiteOpenHelper {
DatabaseHelper(Context context) {

12.

Android Application Development For Dum.indb 263

263

08.11.2011 12:24:05

super(context, DATABASE_NAME,
null, DATABASE_VERSION);

}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db,
int oldVersion,
int newVersion) {
// .
//
// ALTER.
}

7
8

12

.
 1. ,
SQLiteOpenHelper.
 3. SQLiteOpenHelper. .
getReadableDatabase() getWriteableDatabase()
SQLiteOpenHelper (
mDbHelper).
 7. onCreate(),
.
 8. , .
execSQL() SQL.
SQLite
execSQL().
 12. onUpgrade() ,
.
.
getReadableDatabase()
getWritableDatabase() DatabaseHelper. RemindersDbAdapter.
public RemindersDbAdapter open()
throws android.database.SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}

264

Android Application Development For Dum.indb 264

III.

08.11.2011 12:24:05

open() ( ) DatabaseHelper. this. , (ReminderEditActivity ReminderListActivity)


DatabaseHepler.
open() RemindersDbAdapter().


, ,
,
. , RemindersDbAdapter.
public void close() {
mDbHelper.close();
}

. ReminderEditActivity,
Back () .


? .
,
10 000
. ,
. .
,
.
ALTER SQL -

onUpgrade(),
, .

.
,
. ,
Task Reminder
, . , .


SQLite
.
. ReminderListActivity, . , ,
,
.

12.

Android Application Development For Dum.indb 265

265

08.11.2011 12:24:05


,
. .
1.
2.
3.
4.
5.

.
.
EditText.
RemindersDbAdapter.
.

SQLiteDatabase,
RemidnersDbAdapter, .

,
, ReminderEditActivity.
RemindersDbAdapter, onCreate(). , open() RemindersDbAdapter onResume(). RemindersEditActivity.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new RemindersDbAdapter(this);
setContentView(R.layout.reminder_edit);
// ... onCreate()

RemindersDbAdapter,
. ,
, .
ReminderEditActivity.
EditText EditText ReminderEditActivity. , .
ReminderEditActivity.
private EditText mTitleText;
private Button mConfirmButton;
private EditText mBodyText;

onCreate().
mConfirmButton = (Button) findViewById(R.id.confirm);
mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);

Calendar,
DatePicker TimePicker. , -
. EditText ( ). confirm.

266

Android Application Development For Dum.indb 266

III.

08.11.2011 12:24:05

,
registerButtonListenersAndSetDafaultText().
mConfirmButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
saveState();
setResult(RESULT_OK);
Toast.makeText(ReminderEditActivity.this,
getString(R.string.task_saved_message),
Toast.LENGTH_SHORT).show();
finish();
}
});

3
4
5

.
 3. saveState(), .
 4. ReminderEditActivity.
, ReminderEditActivity startActivityForResult().
RESULT_OK
ReminderListActivity , finish()
, . RESULT_OK Activity. onActivityResult() ReminderListActivity.

, , .
 5. , ,
.
task_saved_message .
 7. finish(), ReminderEditActivity.
ReminderEditActivity onSave(),
12.3. RemindersDbAdapter
.

12.3. saveState()
private void saveState() {
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();
SimpleDateFormat dateTimeFormat = new
SimpleDateFormat(DATE_TIME_FORMAT);
String reminderDateTime =
dateTimeFormat.format(mCalendar.getTime());
long id = mDbHelper.createReminder(title, body,
reminderDateTime);

2
3

5
6
8

12.

Android Application Development For Dum.indb 267

267

08.11.2011 12:24:05

.
 2-3. EditText.
 5. SimpleDateFormat,
SQLite.
DATE_TIME_FORMAT.
, :
public static final String DATE_TIME_FORMAT =
"yyyy-MM-dd kk:mm:ss";

, , : 2011-11-20 12:34:21. SQLite


,
.
 6. .
 8. createReminder() ReminderDbAdapter, mDbHelper.
RemindersDbAdapter .
EditText Calendar createReminder() RemindersDbAdapter.
SQLite, ReminderEditActivity ,
.

RemindersDbAdapter
- , , , ? , ! , , . , . SQLite
RemindersDbAdapter.
RemindersDbAdapter . 12.4 ReminderDbAdapter, , . .
ReminderDbAdapter ,
.

12.4. ReminderDbAdapter
public class RemindersDbAdapter {
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE =
"reminders";
private static final int DATABASE_VERSION = 1;

268

Android Application Development For Dum.indb 268

III.

08.11.2011 12:24:05

public static final String KEY_TITLE = "title";


public static final String KEY_BODY = "body";
public static final String KEY_DATE_TIME =
"reminder_date_time";
public static final String KEY_ROWID = "_id";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE + " (" + KEY_ROWID
+ " integer primary key autoincrement, "
+ KEY_TITLE + " text not null, "
+ KEY_BODY + " text not null, "
+ KEY_DATE_TIME + " text not null);";
private final Context mCtx;
public RemindersDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public RemindersDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public long createReminder(String title,
String body, String
reminderDateTime) {
38
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_BODY, body);
initialValues.put(KEY_DATE_TIME, reminderDateTime);
return mDb.insert(DATABASE_TABLE, null,
initialValues);

44

}
public boolean deleteReminder(long rowId) {
return mDb.delete(DATABASE_TABLE, KEY_ROWID +
"=" + rowId, null) > 0;
}

47

public Cursor fetchAllReminders() {


return mDb.query(DATABASE_TABLE,

51

12.

Android Application Development For Dum.indb 269

48

269

08.11.2011 12:24:05

new String[] {KEY_ROWID,


KEY_TITLE, KEY_BODY, KEY_DATE_TIME},
null, null, null, null, null);
}
public Cursor fetchReminder(long rowId)
throws SQLException {
55
Cursor mCursor =
mDb.query(true, DATABASE_TABLE,
new String[] {KEY_ROWID, KEY_TITLE,
KEY_BODY, KEY_DATE_TIME}, KEY_ROWID +
"=" + rowId, null,null, null, null,
null);
56
if (mCursor != null) {
mCursor.moveToFirst();
57
}
return mCursor;
}
public boolean updateReminder(long rowId,
String title, String body, String
reminderDateTime) {
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_BODY, body);
args.put(KEY_DATE_TIME, reminderDateTime);
return mDb.update(DATABASE_TABLE, args, KEY_ROWID +
"=" + rowId, null) > 0;
}
// SQLiteOpenHelper .
// .
}

63
64

69

.
 38. createReminder(). ContentValues , .
 44. insert().
long . ReminderEditActivity , 13 AlarmManager . insert() .
 47. deleteReminder(), rowId , .

270

Android Application Development For Dum.indb 270

III.

08.11.2011 12:24:05

 48. rowId delete(), .


delete() .
 51. fetchAllReminders(),

query(). Cursor
, query(). query() .
 55. fetchReminder(), .
 56. query() Cursor.
 57. Cursor , . moveToFirst(),
Cursor,
. , null.
,
. ,
. , . -
, , .
 63. updateReminder(), update().
update() . .
 64. ContentValues. , SQLite.
 69. , .
, , SQLite.
, .


,
( )
insert(), .
 table. , .
DATABASE_TABLE.

12.

Android Application Development For Dum.indb 271

271

08.11.2011 12:24:05

 nullColumnHack. SQL
. (values) , NULL. null,
.
 values. , .
initialValues, -.


.
query(), , . query()
Cursor,
, .
query() .
 distinct. .
. distinct true.
 table. , . DATABASE_TABLE.
 columns. , .
null . ,
. ,
.
 selection. , , . WHERE
WHERE. null,
. null, , , .
 selectionArgs. (?). , selectionArgs.
selectionArgs , null.
 groupBy. , GROUP BY GROUP BY. null .
,
null.
 having. , , ( ). null
. , null ( ).

272

Android Application Development For Dum.indb 272

III.

08.11.2011 12:24:06

 orderBy. , ORDER BY ORDER BY. null


, ,
. null,
.
 limit. LIMIT. null LIMIT .
, , null.



.
. , update()
. update() .
 table. . DATABASE_
TABLE.
 values. ContentValues, . args, 64 12.4.
 whereClause. WHERE,
.
rowId. WHERE KEY_ROWID+"="_rowId.
 whereArgs. WHERE.
, null.


delete()
. , . , .
, .
delete() .
 table. , . DATABASE_TABLE.
 whereClause. WHERE, . null, . , rowId.
 whereArgs. WHERE.
, null.

12.

Android Application Development For Dum.indb 273

273

08.11.2011 12:24:06


, ? .
, ListView ReminderListActivity
, .
12.5 ReminderListActivity, ListView.

12.5.
ReminderListActivity, SQLite
public class ReminderListActivity extends ListActivity {
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
private RemindersDbAdapter mDbHelper;
/** */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_list);
mDbHelper = new RemindersDbAdapter(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());

14

}
private void fillData() {
Cursor remindersCursor =
mDbHelper.fetchAllReminders();
startManagingCursor(remindersCursor);
//
String[] from = new
String[]{RemindersDbAdapter.KEY_TITLE};

20
21

24

// ,
int[] to = new int[]{R.id.text1};
27
//
SimpleCursorAdapter reminders = new
SimpleCursorAdapter(this, R.layout.reminder_row,
remindersCursor, from, to);
30
setListAdapter(reminders);
31
}
//

274

Android Application Development For Dum.indb 274

III.

08.11.2011 12:24:06

@Override
protected void onListItemClick(ListView l, View v,
int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new
Intent(this, ReminderEditActivity.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID, id);
startActivityForResult(i, ACTIVITY_EDIT);
}
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent intent) {
super.onActivityResult(requestCode,
resultCode, intent); fillData();
}
@Override
public boolean onContextItemSelected(
MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_delete:
AdapterContextMenuInfo info =
(AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteReminder(info.id);
fillData();
return true;
}
return super.onContextItemSelected(item);
}

40

48

52

55
56
57

, .
 5. RemindersDbAdapter . onCreate().
 14. fillData(),
SQLite ListView.
 20. fillData() fetchAllReminders(),
, 51
12.4.
 21. startManagingCursor(),
Activity.
Cursor .
, ,
deactivate() Cursor.
, requery(),
Cursor. ,
Cursor .

12.

Android Application Development For Dum.indb 275

275

08.11.2011 12:24:06

 24. . TITLE, .. .
 27. ,
.
. 24
from (), 27 to (). 24 27.
 30. SimpleCursorAdapter, Cursor TextView, XML. , , .
SimpleCursorAdapter
.
 31. SippleCursorAdapter
setListAdapter(),
, .
 40. .
ReminderEditActivity ,
, .
 48. fillData(),
. , . fillData()
.
 52. ,
, ,
.
 55. getMenuInfo(), ,

AdapterContextMenuInfo.
, , .
 56. RemindersDbAdapter , i d
AdapterContextMenuInfo. id
.
rowId .
 57. fillData()
. , .

276

Android Application Development For Dum.indb 276

III.

08.11.2011 12:24:06

SimpleCursorAdapter
30 12.5 SimpleCursorAdapter. , .
SimpleCursorAdapter , Cursor, . SimpleCursorAdapter .
 this. , .
 R.layout.reminder_row. , , .
 reminderCursor. .
 from. ,
. 24.
 to. , , from.
27.
 from to SimpleCursorAdapter ,
.
, .
. , . ,
.


,
RemidnerListActivity . ,
delete() SQLite. 48 12.4.
deleteReminder() RemindersDbAdapter
onContextSelectedItem() 56 12.5.
rowId.
AdapterContextMenuInfo,
. , ListView. ,
ListView rowId. 55 12.5
AdapterContextMenuInfo, 56 delete()
rowId .
fillData() . : , , .

12.

Android Application Development For Dum.indb 277

277

08.11.2011 12:24:06


, ,
, , . ,
, : .
, . ReminderListActivity
.
Intent i = new Intent(this, ReminderEditActivity.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID, id);
startActivityForResult(i, ACTIVITY_EDIT);


ReminderEditActivity i Intent, (id), . ReminderEditActivity ,
id. , ,
. . id ( ,
),
.
12.6.
.

12.6. ReminderEditActivity,

public class ReminderEditActivity extends Activity {
// ,
//
private Long mRowId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new RemindersDbAdapter(this);
setContentView(R.layout.reminder_edit);
mCalendar = Calendar.getInstance();
mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);
mDateButton = (Button) findViewById(
R.id.reminder_date);
mTimeButton = (Button) findViewById(
R.id.reminder_time);
mConfirmButton = (Button) findViewById(
R.id.confirm);

278

Android Application Development For Dum.indb 278

III.

08.11.2011 12:24:06

mRowId = savedInstanceState != null


? savedInstanceState.getLong(
RemindersDbAdapter.KEY_ROWID)
: null;
registerButtonListenersAndSetDefaultText();

22

}
private void setRowIdFromIntent() {
28
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null
? extras.getLong(RemindersDbAdapter.KEY_ROWID)
: null;
}
}
@Override
protected void onPause() {
super.onPause();
mDbHelper.close();
}
@Override
protected void onResume() {
super.onResume();
mDbHelper.open();
setRowIdFromIntent();
populateFields();
}
//
//
//
//

40

44
46
47
48

:
, ,

private void populateFields() {


if (mRowId != null) {
Cursor reminder =
mDbHelper.fetchReminder(mRowId);
startManagingCursor(reminder);
mTitleText.setText(reminder.getString(
reminder.getColumnIndexOrThrow(
RemindersDbAdapter.KEY_TITLE)));
mBodyText.setText(reminder.getString(
reminder.getColumnIndexOrThrow(
RemindersDbAdapter.KEY_BODY)));
SimpleDateFormat dateTimeFormat =
new SimpleDateFormat(DATE_TIME_FORMAT)
Date date = null;
try {
String dateString = reminder.getString(

12.

Android Application Development For Dum.indb 279

55

57
58

60

61
63
64

279

08.11.2011 12:24:06

reminder.getColumnIndexOrThrow(
RemindersDbAdapter.KEY_DATE_TIME));
date = dateTimeFormat.parse(dateString);
mCalendar.setTime(date);
} catch (ParseException e) {
Log.e("ReminderEditActivity",
e.getMessage(), e);
}

67
68
69
70
71

}
updateDateButtonText();
updateTimeButtonText();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLong(RemindersDbAdapter.KEY_ROWID,
mRowId);
82
}
private void saveState() {
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();
SimpleDateFormat dateTimeFormat = new
SimpleDateFormat(DATE_TIME_FORMAT);
String reminderDateTime =
dateTimeFormat.format(mCalendar.getTime());
if (mRowId == null) {
long id = mDbHelper.createReminder(title,
body, reminderDateTime);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper
.updateReminder(mRowId, title, body,
reminderDateTime);
}

94
95
96
97

100

}
}

.
 22. , mRowId.
onSaveInstanceState().
 28. mRowId , . Intent , mRowId null.

280

Android Application Development For Dum.indb 280

III.

08.11.2011 12:24:06

Long L. long, null long.


 40. .
 44. onResume() (.
. 5.1).
 46. , .
 47. , mRowId ,
.
 48. populateFields() .
 55. , .
 57. Cursor SQLite
mRowId. fetchReminder()
55 12.4.
 58. Cursor.
 60. Cursor.
, .
getColumnInderOrThrow(), ,
. ,
, getString()
. ,
EditText,
mTitleText.
 61. mBodyText EditText , 60,
.
 63. SQLite , .
Java SimpleDateFormat.
Java
.
 64. Date
java.util.Date.
 67. .
 68. Date.
 69. Calendar. Java
Calendar , Date,
Calendar. Calendar.

12.

Android Application Development For Dum.indb 281

281

08.11.2011 12:24:06

 70. , SimpleDateFormat. ParseException


java.text.ParseException.
 71. .
 82. mRowId . Bundle , onSaveInstanceState().
, ,
onResume(). 22, ,
,
savedInstanceState. , Android
, . ,
Google Map,
.
, Task Reminder,
savedInstanceState , . mRowId , .
 94. saveState() , : .
mRowId null, ,
savedInstanceState .
, .
 95. .
 96. , ,
.
 97. mRowId ,
.
 100. .
, , .
, ,
. . 14.

282

Android Application Development For Dum.indb 282

III.

08.11.2011 12:24:06

13


...
 AlarmManager
 AlarmManager


.
, , , ..
. ,
, , , . ,
(.. ) .
Task Reminder.
, ,
. , , -
,
. Windows 7 , . Linux
cron. Android AlarmManager, ,
Windows.

AlarmManager
Task Reminder : , . , -
. . Task Reminder , , , .
. , , , , . , ,
. ? AlarmManager.
AlarmManager , .

Android Application Development For Dum.indb 283

08.11.2011 12:24:06

, AlarmManager , .
Task Reminder. ,
, , ,
, , , .. 14 .
Task Reminder ,
onReceive(). , ,
. , , . WAKE_LOCK ( ), .


AlarmManager
AlarmManager ,
. Task
Reminder saveState(). , ,
.
 ReminderManager.java.
AlarmManager.
 OnAlarmReceiver.java. ,
AlarmManager. AndroidManifest.xml
.
<receiver android:name=".OnAlarmReceiver" />

OnAlarmReceiver Android ,
,
AndroidManifest.xml, .
 WakeReminderIntentService.java. .
 ReminderService.java. WakeReminderIntentService, (. 14).
WakeReminderIntentService,
AndroidManifest.xml
:
<service android:name=".ReminderService" />

284

Android Application Development For Dum.indb 284

III.

08.11.2011 12:24:06

ReminderManager
, ReminderManager
AlarmManager.
ReminderManager ,
AlarmManager.
saveState() ReminderEditActivity, .
new ReminderManager(this).setReminder(mRowId, mCalendar);

ReminderManager
AlarmManager mRowId
, mCalendar.
13.1 ReminderManager.

13.1. ReminderManager
public class ReminderManager {
private Context mContext;
private AlarmManager mAlarmManager;
public ReminderManager(Context context) {
mContext = context;
mAlarmManager =
(AlarmManager)context.getSystemService(
Context.ALARM_SERVICE);
}
public void setReminder(Long taskId,
Calendar when) {
Intent i =
new Intent(mContext, OnAlarmReceiver.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID,
(long)taskId);
PendingIntent pi =
PendingIntent.getBroadcast(mContext, 0, i,
PendingIntent.FLAG_ONE_SHOT);
mAlarmManager.set(AlarmManager.RTC_WAKEUP,
when.getTimeInMillis(), pi);
}

12
13
14

16

17

.
 6. ReminderManager. context.
 9. AlarmManager getSystemService().

13.

Android Application Development For Dum.indb 285

285

08.11.2011 12:24:06

 12. setReminder().
Calendar, .
 13. , .
OnAlarmReceiver.
 14. .
 16. AlarmManager ,
, ,
PendingIntent. Intent, 13. PendingIntent FLAG_ONE_
SHOT, , .
 17. set() AlarmManager
. set() .
type: AlarmManager.RTC_WAKEUP.
, triggerAtTime.
triggerAtTime: when.getTimeInMillis(). . Calendar getTimeInMillis(), long.
long , 1 1970 ( , Calendar
, ).
operation: pi. ,
.
, , , .

OnAlarmReceiver
OnAlarmReceiver ( 13.2) , . ,

BroadcastReceiver,
.

13.2. OnAlarmReceiver
public class OnAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
long rowid =
intent.getExtras().getLong(
RemindersDbAdapter.KEY_ROWID);
4
WakeReminderIntentService.

286

Android Application Development For Dum.indb 286

III.

08.11.2011 12:24:06

acquireStaticLock(context);
6
Intent i = new Intent(context,
ReminderService.class);
8
i.putExtra(RemindersDbAdapter.KEY_ROWID, rowid); 9
context.startService(i);
10
}
}

.
 4. , ,
, .
 6. WakeReminderIntentService
,
, .
 8. ,
ReminderService, .
 9. , . ReminderService
.
 10. ReminderService .
. BroadcastReceiver . ,
,
.
, Android ,
. , .
BroadcastReceiver, , . . , . OnAlarmReceiver
BroadcastReceiver.
AlarmManager ,
OnAlarmReceiver, ,
13 13.1. ,
.

WakeReminderIntentService
( 13.3) .
( ),
, .
WakeReminderIntentService ,
, , .

13.

Android Application Development For Dum.indb 287

287

08.11.2011 12:24:06

13.3. WakeReminderIntentService
public abstract class WakeReminderIntentService
extends IntentService { abstract void
doReminderWork(Intent intent);
public static final String
LOCK_NAME_STATIC=
"com.dummies.android.taskreminder.Static";
private static PowerManager.WakeLock
lockStatic=null;

3
4

public static void acquireStaticLock(


Context context) {getLock(context).acquire(); 5
}
synchronized private static PowerManager.WakeLock
getLock(Context context) {
if (lockStatic==null) {
PowerManager mgr=(PowerManager)context
.getSystemService(Context.POWER_SERVICE);

10

lockStatic=mgr.newWakeLock(PowerManager.
PARTIAL_WAKE_LOCK, LOCK_NAME_STATIC);
lockStatic.setReferenceCounted(true);

12
13

return(lockStatic);

15

}
}
public WakeReminderIntentService(String name) {
super(name);
}
@Override
final protected void onHandleIntent(
Intent intent) {
try {
doReminderWork(intent);
} finally {
getLock(this).release();
}

18

23
25
27

}
}

.
 2. , ReminderService, 7 13.4.
 3. , . .

288

Android Application Development For Dum.indb 288

III.

08.11.2011 12:24:06

 4. .
.
 5. , 8. getLock() aquire(),
,
.
, .
 8. getLock(),
PowerManager.WakeLock, Android
,
.
 10. PowerManager getSystemService(). .
 12. WakeLock newWakeLock().
.
flags: PowerManager.PARTIAL_WAKE_LOCK. , PARTIAL_WAKE_LOCK.
Android , , .
tag: LOCK_NAME_STATIC. , . 3.
 13. PowerManager ,
.
 15. WakeLock .
 18. WakeReminderIntentService,
.
 23. onHandleIntent,
IntentService. ,
onHandleIntent() , .
 25. ,
.
 27. doReminderWork() WakeLock. , , .
,
. finally
try-catch release(). finally
, , try.

13.

Android Application Development For Dum.indb 289

289

08.11.2011 12:24:06

ReminderService doReminderWork(), Task Reminder .


, doReminderWork()
ReminderService.

AlarmManager . ,
. ,
, AlarmManager.

,
.
, doReminderWork().

ReminderService
ReminderService ( 13.4)
. ,
, .

AlarmManager. 14.

13.4. ReminderService
public class ReminderService extends
WakeReminderIntentService {
public ReminderService() {
super("ReminderService");
}
@Override
void doReminderWork(Intent intent) {
Long rowId = intent.getExtras()
.getLong(RemindersDbAdapter.KEY_ROWID);
//
}

7
8

.
 1. WakeReminderIntentService.
 7. doReminderWork()
WakeReminderIntentService.
 8. , .
,
.

290

Android Application Development For Dum.indb 290

III.

08.11.2011 12:24:06


,
. , .
, . AlarmManager,
.
. , .
, ,
Android.


RECEIVE_BOOT_
COMPLETED. Android
, . Android , ,
BroadcastReceiver.
Android.
, SQLite
RemindersDbAdapter,
. .
BroadcastReceiver. Task
Reminder onBootReceiver.
AndroidManifest.xml .
<receiver android:name=".OnBootReceiver">
<intent-filter>
<action android:name=
"android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

Android , OnBootReceiver BOOT_COMPLETED. ,


OnBootReceiver , .
OnBootReceiver 13.5.

13.5. OnBootReceiver
public class OnBootReceiver
extends BroadcastReceiver {
@Override
public void onReceive(Context context,
Intent intent) {
ReminderManager reminderMgr =
new ReminderManager(context);
RemindersDbAdapter dbHelper =

13.

Android Application Development For Dum.indb 291

4
6

291

08.11.2011 12:24:07

new RemindersDbAdapter(context);
dbHelper.open();
Cursor cursor = dbHelper.fetchAllReminders();
11
if(cursor != null) {
cursor.moveToFirst();
14
int rowIdColumnIndex = cursor.getColumnIndex(
RemindersDbAdapter.KEY_ROWID);
int dateTimeColumnIndex =
cursor.getColumnIndex(
RemindersDbAdapter.KEY_DATE_TIME);
while(cursor.isAfterLast() == false) {
19
Long rowId = cursor.getLong(rowIdColumnIndex);
String dateTime =
cursor.getString(dateTimeColumnIndex);
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new
SimpleDateFormat(
ReminderEditActivity.DATE_TIME_FORMAT);
try {
java.util.Date date = format.parse(dateTime);
cal.setTime(date);

27
28

reminderMgr.setReminder(rowId, cal);
} catch (ParseException e) {
Log.e("OnBootReceiver", e.getMessage(), e);
}
cursor.moveToNext();

30

cursor.close() ;

37

dbHelper.close();

40

32
35

}
}
}
}

.
 1. OnBootReceiver.
 4. onReceive(), , .
 6. ReminderManager, .
 11.
RemindersDbAdapter. ListView ReminderListActivity.
 14. Cursor.
; .
 19. while, , . true, ,

292

Android Application Development For Dum.indb 292

III.

08.11.2011 12:24:07

35. true
, .
 27. , .
 28. Calendar , . Date
Calendar.
 30. , , , Calendar.
 32. . . .
 35. .
, isAfterLast() true, while.
19, .
 37. , . ,
, ,
Cursor, . , .
,
.
 40. ReminderDbAdapter, , ,
. .
,
, . , debuggable true, Eclipse .


OnBootReceiver,
while .
Log.d("OnBootReceiver",
" .");
Log.d("OnBootReceiver",
" = "+rowIdColumnIndex);

, DDMS. ( ) . DDMS OnBootReceiver.


, , , .
, .

13.

Android Application Development For Dum.indb 293

293

08.11.2011 12:24:07

14


...





, ,
. ,
. , ,
, . Android.


, , .
, . 14.1.


. 14.1 , . , ()
USB,
USB. ,
(. 14.2).
. 14.2 , , .
,
.



.
,
. , .
.

Android Application Development For Dum.indb 294

08.11.2011 12:24:07

. 14.1.

. 14.2.

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

14.

Android Application Development For Dum.indb 295

295

08.11.2011 12:24:07

, ,
, . . 14.3 ,
Foursquare.

. 14.3. 60% Foursquare

, , .. , , , .


NotificationManager , Android.
. ,
.
NotificationManager mgr =(NotificationManager)
getSystemService(NOTIFICATION_SERVICE);

NotificationManager
getSystemService().

296

Android Application Development For Dum.indb 296

III.

08.11.2011 12:24:07


Task Reminder ,
. ,
AlarmManager , .
.
doReminderWork() ReminderService ,
14.1.

14.1. doReminderWork()
Long rowId = intent.getExtras().
getLong(RemindersDbAdapter.KEY_ROWID);

NotificationManager mgr = (NotificationManager)


getSystemService(NOTIFICATION_SERVICE);

Intent notificationIntent = new Intent(this,


ReminderEditActivity.class);
notificationIntent.putExtra(
RemindersDbAdapter.KEY_ROWID, rowId);

5
6

PendingIntent pi = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_ONE_SHOT);

Notification note=new Notification(


android.R.drawable.stat_sys_warning,
getString(R.string.notify_new_task_message),
System.currentTimeMillis());

10

note.setLatestEventInfo(this, getString(
R.string.notifiy_new_task_title), getString(
R.string.notify_new_task_message), pi);

12

note.defaults |= Notification.DEFAULT_SOUND;
note.flags |= Notification.FLAG_AUTO_CANCEL;

14
15

// 2,147,483,647 ,
// , int
// . -
// , .
int id = (int)((long)rowId);
19
mgr.notify(id, note);
20

.
 1. , ReminderService, .
. , ,
ReminderEditActivity , .

14.

Android Application Development For Dum.indb 297

297

08.11.2011 12:24:07

ReminderEditActivity
.
 3. NotificationManager.
 5. ReminderEditActivity.
,
.
 6. .
 8. , .
, PendingIntent. FLAG_ONE_SHOT ,
.
 10. , . .
icon: android.R.drawable.stat_sys_warning.
, . .
Android, , , Android .
tickerText: getString(R.string.notify_new_task_message).
, .
when: System.currentTimeMillis(). ,
.
 12. ,
Android. XML,
.
setLatestEventInfo() .
context: this. , .
contentTitle: getString(R.string.notify_new_task_
title). .
contextText: getString(R.string.notify_new_task_
message). , .
contentIntent: pi. , .
 14. . , . , ,
.
 15.
. .

298

Android Application Development For Dum.indb 298

III.

08.11.2011 12:24:07

 19. ID ( ) . SQLite
long, int. , -
2 147 483 647 ( int Java). , . int , notify 20 int.
 20. . notify()
.
id: id. , .
Notification: note. , , .


,
.
 ,
.
 , ,
Task Reminder, .
.
 , .

, , .
ReminderEditActivity ,
.
 .

ReminderEditActivity.


strings.xml .
 notify_new_task_message. !. .
 notify_new_task_title. Task
Reminder, .. .
.

14.

Android Application Development For Dum.indb 299

299

08.11.2011 12:24:07


.
. ,
, , . , , . , .
notify() , ,
,
. , ,
Notification , ,
notify(), .


.
,
, , ,
. , , . ,
, . , . :
, , . : , Task Reminder, ,
, , , ,
.
, , .
, , . , ,
.
, , .
,
.
( NotificationManager)
cancel().
. ,
Task Reminder
. ,
cancel() .
.
cancelAll() NotificationManager.

300

Android Application Development For Dum.indb 300

III.

08.11.2011 12:24:07

15


...









PreferenceActivity

c . ,
, , . . ,
Android, . , Android
, .
Android , ,
. . , . Android
.
, , . .
, .
.

Android . PreferenceActivity. . Task Reminder .


Android , . ,
. XML,

Android Application Development For Dum.indb 301

08.11.2011 12:24:07

res/xml .
XML, layout. , , . .
 EditTextPreference. ,
.
 CheckBoxPreference. , .
 RingtonePreference. , ,
.
 ListPreference. ,
, .
, Preference DialogPreference.
DialogPreference .
,
.
EditTextPreference ListPreference , DialogPreference.
PreferenceActivity , Activity. PreferenceActivity
,
EditTextPreference.


PreferenceActivity Preference
, , (. 15.1).

SharedPreferences. ,
getSharedPreferences() Context.
PreferenceActivity
Activity, . PreferenceActivity .
Android.
PreferenceActivity, .


Android SharedPreferences, . .
, ,
.

302

Android Application Development For Dum.indb 302

III.

08.11.2011 12:24:07

. 15.2 EditTextPreference, Task Reminder. OK Android SharedPreferences, . ,


.

. 15.1.

. 15.2. Task Reminder


Android , ,
, , , , . Android , -,
, ,
. , . ,
Android ,
.
Android .
 PreferenceScreen. , . PreferenceScreen .
PreferenceActivity.
PreferenceScreen . , PreferenceScreen.

15.

Android Application Development For Dum.indb 303

303

08.11.2011 12:24:07

. PreferenceScreen
. XML P r e f e r e n c e S c r e e n ,
PreferenceScreen. XML
. , PreferenceScreen.
,
Android,
.
 PreferenceCategory. , .
 Preference. , .
Android.
, .. .
XML PreferenceScreen,
PreferenceCategory Preference , .
, . 15.1.


PreferenceActivity XML . XML, , ,
. , TextView,
.
PreferenceScreen Task Reminder.
, , . , .

. , 60 ,
, 60 ,
. , , .


res/xml, task_preferences.xml.
15.1.

15.1. task_preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android=

304

Android Application Development For Dum.indb 304

III.

08.11.2011 12:24:07

"http://schemas.android.com/apk/res/android">
<PreferenceCategory
4
android:key=
"@string/pref_category_task_defaults_key"
5
android:title=
"@string/pref_category_task_defaults_title"> 6
<EditTextPreference
7
android:key="@string/pref_task_title_key"
8
android:dialogTitle=
"@string/pref_task_title_dialog_title"
9
android:dialogMessage
="@string/pref_task_title_message"
10
android:summary=
"@string/pref_task_title_summary"
11
android:title=
"@string/pref_task_title_title" />
12
</PreferenceCategory>
<PreferenceCategory
13
android:key=
"@string/pref_category_datetime_key"
14
android:title=
"@string/pref_category_datetime_title">
15
<EditTextPreference
16
android:key=
"@string/pref_default_time_from_now_key"
17
android:dialogTitle=
"@string/pref_default_time_from_now_dialog_title" 18
android:dialogMessage=
"@string/pref_default_time_from_now_message"
19
android:summary=
"@string/pref_default_time_from_now_summary"
20
android:title=
"@string/pref_default_time_from_now_title" />
21
</PreferenceCategory>
</PreferenceScreen>

15.1 .
15.2,
XML.
 2. .
. PreferenceScreen
.
 4. ,
, . 13 .
, ,
,
PreferenceCategory .

15.

Android Application Development For Dum.indb 305

305

08.11.2011 12:24:07

 5. , SharedPreferences. .
 6. .
 7. EditTextPreference, , .
 8. , .
 9. EditTextPreference DialogPreference. ,
, . 15.2.
.
 10. , .
 11. , (. . 15.1).
 12. .
 13. PreferenceCategory
, .
 14. .
 15. .
 16. EditTextPreference, ,
.
 17. , .
 18. , .
 19. , .
 20. ,
(. . 15.1).
 21. .


,
. res/values/strings.xml .
<!-- Preferences -->
<string name="pref_category_task_defaults_key">
task_default_category</string>
<string name="pref_category_task_defaults_title">
</string>
<string name="pref_task_title_key">
default_reminder_title</string>
<string name="pref_task_title_dialog_title">

306

Android Application Development For Dum.indb 306

III.

08.11.2011 12:24:07

</string>
<string name="pref_task_title_message">
, .
</string>
<string name="pref_task_title_summary">
, .
</string>
<string name="pref_task_title_title">
</string>
--
<string name="pref_category_datetime_key">
date_time_default_category</string>
<string name="pref_category_datetime_title">
</string>
<string name="pref_default_time_from_now_key">
time_from_now_default</string>
<string name="pref_default_time_from_now_dialog_title">
</string>
<string name="pref_default_time_from_now_message">
,

</string>
<string name="pref_default_time_from_now_summary">
, .
</string>
<string name="pref_default_time_from_now_title">
</string>

.
,
. XML,
, .
PreferenceActivity.

PreferenceActivity
XML, . (
PreferenceScreen).
SharedPreferences. , . ,
.
PreferenceScreen,
, PreferenceActivity.
TaskPreferences. TaskPreferences
15.2.

15.

Android Application Development For Dum.indb 307

307

08.11.2011 12:24:07

15.2. TaskPreferences
public class TaskPreferences
extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(
R.xml.task_preferences);
EditTextPreference timeDefault = (EditTextPreference)
findPreference(getString(
R.string.pref_default_time_from_now_key));
timeDefault.getEditText().
setKeyListener(DigitsKeyListener.getInstance());
}
}

6
7

.
 1. TaskPreferences, PreferenceActivity.
 5. .
task_preferences.xml.
 6. ,
task_preferences.xml.
 7. EditText, Edit TextPreference, getEditText().
EditText , . setKeyListener(),
DigitsKeyListener, EditTextPreference .
DigitsKeyListener , .
TaskPreferences .
PreferenceActivity . ,
. ,
.
TaskPreferences
AndroidManifest.xml, .
<activity android:name=".TaskPreferences"
android:label="@string/app_name" />

308

Android Application Development For Dum.indb 308

III.

08.11.2011 12:24:07

PreferenceActivity
, ReminderListActivity. list_menu.xml, res/menu. ReminderListActivity.
list_menu.xml.
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android=
"http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_insert"
android:icon="@android:drawable/ic_menu_add"
android:title="@string/menu_insert" />
<item android:id="@+id/menu_settings"
android:icon=
"@android:drawable/ic_menu_preferences"
android:title="@string/menu_settings" />
</menu>

, ,
. Android menu_settings. strings.
xml .


, . . unMenuItemSelected()
ReminderListActivity , .
@Override
public boolean onMenuItemSelected(int featureId,
MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_insert:
createReminder();
return true;
case R.id.menu_settings:
Intent i = new Intent(this, TaskPreferences.class);
startActivity(i);
return true;
}
return super.onMenuItemSelected(featureId, item);
}

, ,
Intent TaskPreferences.
, ,
. Task Reminder,
, , . 15.3.

15.

Android Application Development For Dum.indb 309

309

08.11.2011 12:24:08

. 15.3.


PreferenceActivity , ,
, SharedPreferences . . , Android
.
Task Reminder
ReminderEditActivity , . SharedPreferences,
.


ReminderEditActivity
populateFields(). , (). ,
, , SharedPreferences
. , . .
, .

310

Android Application Development For Dum.indb 310

III.

08.11.2011 12:24:08

SharedPreferences ( 15.3). populateFields() , .

15.3. SharedPreferences
private void populateFields() {
if (mRowId != null) {
Cursor reminder = mDbHelper.fetchReminder(mRowId);
startManagingCursor(reminder);
mTitleText.setText(reminder.getString(
reminder.getColumnIndexOrThrow(
RemindersDbAdapter.KEY_TITLE)));
mBodyText.setText(reminder.getString(
reminder.getColumnIndexOrThrow(
RemindersDbAdapter.KEY_BODY)));
SimpleDateFormat dateTimeFormat = new
SimpleDateFormat(DATE_TIME_FORMAT);
Date date = null;
try {
String dateString =
reminder.getString(reminder.getColumnIndexOrThrow(
RemindersDbAdapter.KEY_DATE_TIME));
date = dateTimeFormat.parse(dateString);
mCalendar.setTime(date);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
} else {
21
SharedPreferences prefs =
PreferenceManager.
getDefaultSharedPreferences(this);
22
String defaultTitleKey =
getString(R.string.pref_task_title_key);
23
String defaultTimeKey = getString(
R.string.pref_default_time_from_now_key);
24
String defaultTitle =
prefs.getString(defaultTitleKey, "");
26
String defaultTime =
prefs.getString(defaultTimeKey, "");
27
if("".equals(defaultTitle) == false)
mTitleText.setText(defaultTitle);
30
if("".equals(defaultTime) == false)
mCalendar.add(Calendar.MINUTE,
Integer.parseInt(defaultTime));
33
}
updateDateButtonText();
updateTimeButtonText();
37
}

15.

Android Application Development For Dum.indb 311

311

08.11.2011 12:24:08

.
 21. else , .
 22. SharedPreferences getDafaultSharedPreferences PreferenceManager.
 23.
. 15.1 .
 24. , ( , , 23).
 26. getString() SharedPreferences.
, , . , .
 27. ,
26, .
 30. EditText, . , .
 33. Calendar add() Calendar.MINUTES (
). Calendar.MINUTES
Calendar ,
. , , Calendar . , Calendar 201012-31 11:45 pm add() 60 ,
2011-01-01 12:45 pm. EditTextPreference
,

Integer.parseInt().
Calendar
.
 37. , Calendar.
ReminderEditActivity.
( ), ,
.

312

Android Application Development For Dum.indb 312

III.

08.11.2011 12:24:08


Task Reminder ,
. . ,
, , .
, , . , . . ,
, ,
( ,
), .
.
,
SharedPreferences. PreferenceManager, 15.4. SharedPreferences
Editor. , 15.4.

15.4.
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit();
editor.putString("default_department", " -4");
editor.commit();

1
2
3
4

.
 1. SharedPreferences PreferenceManager.
 2. Editor edit()
SharedPreferences.
 3. , default_department,
putString() Editor. -4.
,
,
.
 4. , ,
commit() Editor. SharedPreferences.
,
SharedPreferences, ,
putString().

15.

Android Application Development For Dum.indb 313

313

08.11.2011 12:24:08

commit() Editor,
, , .

, , , .
XML, .

314

Android Application Development For Dum.indb 314

III.

08.11.2011 12:24:08

IV


The 5th Wave

......

...
...

.
....
.

.
..

Android Application Development For Dum.indb 315

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

08.11.2011 12:24:08


IV Android, .
, . Android,
SQLite
-, .
,
Android. .

Android Application Development For Dum.indb 316

08.11.2011 12:24:08

16


Android ,
. , , . , .
, , , , . ,
JSON? , .
, - , .
! , , , . ,

Android. , , Table? isEditable(), , .
, Table, , , , . , new , isEditable(). return
false, .
Android SDK, Android. 2 , SDK, ,
.
Android.
: , ? , , . ,
Android Market .
, ,
. Google ( ,
Yandex). Android , , ,
.

Android Application Development For Dum.indb 317

08.11.2011 12:24:08

Foursquare

1.
GPS . (, , ..). . ,
.
Android
Google Code. , ,
XML JSON.
Android, :
 ;
 XML;
 ;
 OAuth;
 Google Maps Map Layers;
 GPS;
 - .
, , , . http://code.google.com/p/foursquared.

LOLCat
Android, . , ,
, , SD . , , MMS . http://code.google.com/p/apps-for-android.

Amazed
,
.
, .

. http://code.google.com/p/appsfor-android.
1

2011 Foursquare. . .

318

Android Application Development For Dum.indb 318

IV.

08.11.2011 12:24:08

API-
Android SDK , , , API-. .
. , , . .
samples Android SDK.

MultiResolution
,
, MultiResolution, Android SDK.
, . Android SDK
, .
, .
, .

. samples/MultiResolution.

Last.fm
, , Last.fm API.
Last.fm API, http://last.fm/api/account.
.
.
, . , .
http://github.com/mxcl/lastfm-android.

Hubroid
Git DVCS (Distributed Version Control System ). , , Git. Hubroid GitHub.com
Git . Hubroid
GitHub API. ,
, Git. http://github.com/
eddieringle/hubroid.

16.

Android Application Development For Dum.indb 319

319

08.11.2011 12:24:08

Facebook SDK Android


, , Facebook, , , ,
. , . Facebook
Android SDK Facebook
Android. , , .
http://github.com/facebook/facebook-android-sdk.

Replica Island
, , . , Replica
Island . Android Market. ( http://code.google.
com/p/replicaisland) .

SQLite
SQLite , .., Notepad, Android. ,
SQLite. http://d.
android.com/guide/tutorials/notepad/index.html.

320

Android Application Development For Dum.indb 320

IV.

08.11.2011 12:24:08

17

, , . , , XML JSON, .. ,
, . , . , ,
, , ,
. ,

.

droid-fu
,
. ,
, , , ,
. droid-fu,
, .
droid-fu, ,
http://github.com/
kaeppler/droid.fu.

RoboGuice
, . Google, . . . http://code.google.
com/p/roboguice.

DroidDraw
Android.

Android Application Development For Dum.indb 321

08.11.2011 12:24:08

. , . , DroidDraw,
Eclipse.
- DroidDraw. www.droiddraw.org.

Draw 9-patch
, Android.
Draw 9-patch .
:
http://d.android.com/guide/developing/tools/draw9patch.html

Draw 9-patch ,
, ,
.

Hierarchy Viewer

, . Hierarchy Viewer, tools Android SDK,
. ,
, . .
, ,
.
:
http://developer.android.com/guide/developing/tools/
hierarchy-viewer.html

Application Exerciser Monkey


- Android. , ,
, . , .
:
http://developer.android.com/guide/developing/tools/monkey.html

zipalign
APK. . Eclipse ADT,
APK,

322

Android Application Development For Dum.indb 322

IV.

08.11.2011 12:24:08

8. , zipalign .
:
http://developer.android.com/guide/developing/tools/zipalign.html

layoutopt
, .
,
. :
http://developer.android.com/guide/developing/tools/layoutopt.html

Git
, .
. .
Eclipse , Git Eclipse. Git ,
.
Projectlocker.com Unfuddle.com.
, Github.com. www.git-scm.com.

Paint.NET GIMP
Android . Adobe Photoshop, . , , .
, Paint.NET GIMP.
Paint.NET .NET Framework,
.
Windows , Android. Paint.NET www.getpaint.net.
GIMP Photoshop.
Windows, Linux Mac. www.gimp.org.

17. ,

Android Application Development For Dum.indb 323

323

08.11.2011 12:24:08

Android Application Development For Dum.indb 324

08.11.2011 12:24:08

ADB 50
ADT 56
Android Market 190
196
Android SDK 48, 60
ANR 30, 175, 181
API 24
APK 190, 193
AVD 76

260
265

D
Dalvik 27
DDMS 144

E
Eclipse 53, 67

G
GPS 33

J
Java 43
JDK 46
jUnit 158

N
NDK 50

29, 119
169, 176
29
27
78
257
257
98

28
130
113

28, 173
27, 123
125
128
124
29
224

OHA 42

84
117

SQLite 256, 260, 265

112
164

X
XML 105, 114, 231

116
158
258
33
92
30

Android Application Development For Dum.indb 325

36

Action 172
Activity 123
AlarmManager 283
AppWidgetProvider 171
AsyncTask 30
AudioManager 135
BroadcastReceiver 177

08.11.2011 12:24:08

Button 120
DatePicker 240
FrameLayout 107
ImageView 114
Intent 172
IntentService 181
LinearLayout 107
ListActivity 222
NotificationManager 296
PendingIntent 172
PreferenceActivity 307
RelativeLayout 107
SharedPreferences 302
TableLayout 107
TimePicker 240
119
98
107, 111
108, 120
235

79
82
79
224

107
155
165
257

97, 186, 191, 259


296
162, 230
235
231
33
25

28, 226
172, 175
301, 304, 310

37
78
133

326

Android Application Development For Dum.indb 326

228
248, 251
144

72
291
98
30
106
170

28
130
130
73, 89, 215

160
99, 258
190
99
108, 120
93, 160, 163

33
192
258
31
203
302
88
124
161
294

162
150
107

254, 297
300
300

193

08.11.2011 12:24:08

162
121
74
192

130

171

Android Application Development For Dum.indb 327

240
61, 76, 83, 139

Linux 39

327

08.11.2011 12:24:09

Android 3



, ,

SDK Android 3

Android
3



I I

www.williamspublishing.com

ISBN 978-5-8459-1746-1

. 328




Android 3.0 SDK.
,

,

,


. ,
,
,

ActionBar.


,


Android
.
API- Android,
,

.

Android 3.0,


Android, WebM
,
,


.

JQUERY


JavaScript
..

www.williamspublishing.com

ISBN 978-5-8459-1603-7

. 329


JavaScript-
jQuery.

,

,
,

DOM-
,
.


AJAX

.


XMLHttpRequest,
AJAX,

jQuery.


jQuery UI,
,

,
jQuery JavaScript.


,
.

JAVA
7-

www.williamspublishing.com

ISBN 978-5-8459-1168-1

. 330






Java.

,



Java,
Java SE 6,



.


,

,

Java API, ,
,
AWT, Swing Java Beans.

-, ,

.

Java 2

2.


www.williamspublishing.com

ISBN 978-5-8459-1482-8

. 331



Java

,


Java SE 6.
,
, XML,
API-

(JDBC),
,

, ,
JavaBeans,

,
,
,


.

,
,

,

.

JAVA 2

1.
8-

www.williamspublishing.com

ISBN 978-5-8459-1378-4

. 332



Java

,


Java SE 6.
,


Java,
,
-

Java, ,
,
, Swing,

, ,
,

.

,
,


,
.


,

.

MICROSOFT WINDOWS
SERVER 2008 R2

,
,
,
,

. 333

www.williamspublishing.com



Windows Server 2008
R2, Active Directory,
, ,
Windows Server
2008 R2 Windows Server
2003/2008, ,
, ,
,

.


Windows Server 2008 R2

Windows Server
2008 R2,
Hyper-V DirectAccess

Failover Clustering.

,
,

-
Windows Server 2008 R2.


.

ISBN 978584591653-2

C# 4.0

www.williamspublishing.com

ISBN 978-5-8459-1684-6

. 334


C# 4.0 ,
.NET,

:
, , , ,
, , ,
, , , , ,
,

.

C#, PLINQ, TPL,
,
.




,


.

, C#.

Android



Android , ,
, . , .
3
, , StackOverflow.com Android.

, , , , ,
.
3

Android:
http://d.android.com/guide/appendix/faq/commontasks.html
3
, , Eclipse,
Java .
. ,
Android:
http://d.android.com/reference/classes.html
3
,
<Ctrl+Shift+G>.
3
DDMS , ,
, .
3
Eclipse, , ,
, . , Eclipse ,
. <F2>. ,
.
3
, <Ctrl+O> .
.
<Enter>.
3
Eclipse <Shift+PgUp>
<Shift+PgDn>.
3
Android , <Ctrl+Shift+F11>.

Eclipse





Java



Android Application Development For Dum.indb 335


<Alt+Shift+N>
<Ctrl+Shift+O>
<F3>
<Alt+Shift+R>
<Ctrl+H>
<Ctrl+Shift+T>
<Ctrl+G>
<Alt+I>
<Alt+J>

335

08.11.2011 12:24:09

Android


Android


startActivity(new Intent(this, Destination.class));
Intent.createChooser(yourIntent, "#&'QXZ[Q ]^`]&z |^]~[");

Intent i=new;
Intent(Intent.ACTION_VIEW, Uri.parse("http://example.org"));
startActivity(i);
startActivityForResult(yourIntent,YOUR_REQUEST_CODE);


3

. , , ,
. -.
.
3

dip (density-independent pixels ,


). .
dip , .
3
AndroidManifest.xml supports-screens,

Android Market
.
3
,

. - ,
.

Android Application Development For Dum.indb 336

08.11.2011 12:24:09