Hardway Imposed Design Considerations
Hardway Imposed Design Considerations
DEVICES
DEVELOPING FOR MOBILE AND EMBEDDED
ndrod does a lot to simplify mobile- or embedded-devIce software development, but vou
understand the reasons behind the conventions. There are several tactors to accoun
Iroid in for
particular,
wh need
g software for mobile and embedded devices, and when develop1ng for Android in
In this chapter you'll learn some of the techniques and best practices for writ
ng efhcient Android code. In later examples, efficiency is sometimes compro-
mised for clarity and brevity when new Android concepts or functionality are
Introduced. In the best tradition of "Do as I say, not as I do, the examples are
designed to show the simplest (or easiest-to-understand) uway of doing some-
thing, not necessarily the best way of doing it.
Limited RAM
Limited permanent storage capacity
Small screens with low resolution
Be Efficient
Manufacturers of embedded devices, particularly mobile devices, generally value small size and long
hattery life over potential improvecn n processor speed. For developers, that means losing the
Developing for Mobile and Embedded Devices 39
s s o r
desktop
pertormance mprOvements; for mobile
11 rocCSsOr pert
and server
hardware, this usually results
d r c t h
br
mobiles. with brighter, higher
mobiles,
devices, it instead means
thinner, more
nwer-cfheient resolution screens. By comparison, improvements
power take a back seat. in
process
Android lets you specify that your application can be installed on the SD card as an alternative to
in detail in Chapter 3), but there are significant restrictions to this
using internal memory (described
approach and it isn't suitable for all applications. As a result, the compiled size of your application is
is ensuring that your application is polite in its
an important consideration, though more important
use of system resources.
Deeded
a signiticant
challenge.
i n t e r t a c e s c a n be
itutive, and pleasing user
only la
know ing that
users will often at ance the
and reen.
atrons
appln l
Wite vour
use h redng the numbeI ol contral.
cen. Ma.
Mat
THons n t u n v e
nformathon
and
tront and
cas1 o
centet pttng, th
the ones
vou tlcreate in Chapter
er 44, are an
wntnals, situh
(oraphk al
as
Rather th excelle
a lor
anhormarnon in a wau
thats easv to understand.
than a secreen
hurtons and rext-entr boves, use colors, shapes, and graphics to
to convey intorn co.
fult
YouTi also need to consder how touch input is going affect
your interface
to affect vour nation,
svlas has passed: now it's all about hnger input, so make sure your V .
Yow'll learn
Chapters 3 and 4.
some
techniques for optimizing your Ul for different screen sizes
ize nin
e Chapter 6. "Usng lnternet Resources," vou "'l learn how to use Internet
resources In VOHT afplications
Further details. inclhdng how to detect the kind of network connections avail
abic at run time, are included n Chapler 16, "Bluetooth, NFC, Networks, and
Wi-Fi.
At What Cost?
t vou're a mobile device owner, you know all too well that some of your device's functionality can
igerally come at a price. Services such as SMS and data transfer can incur additional fees from your
service provider.
Ir's obvious why any costs associated with functionality in your applications should be minimized,
and rhat users should be made aware when an action they perform might result in their being
charged.
Ir's a good approach to assume that there's a cost associated with any action involving an interac
tion with the outside world. In some cases (such as with GPS and data transfer), the user can toggle
Android settings to disable a potentially costly action. As a developer, it's important that you use
and respect those settings within your application.
In any case, it's important to minimize interaction costs by doing the following
Scheduling big updates or transfers at off-peak times or when connected via Wi-Fi by using
Alarms and Broadcast Receivers, as shown in Chapter 9
C n the best solution is to use a lower-quality option that comes at a lower cost.
When
USing location-based services, as described in Chapter 13, *Maps, Geocoding, and Location-
cdServices," you can select a location provider based on whether there is an associated cost.
Withir
n your location-based applications, consider giving users the choice of lower cost or greater
accuracy.
Developing for Mebile and Embedded Devices
nt to
ensure seamlessness and
responsiveness no matter what the speed, latenc y, and ava
b l t y of network access. Some techniques include limiting the functionality of your applc at0, or
tnetwork
ne nerwork lookups to cached bursts, when the ava1lable network connecton supports onily
r l i ng network
Further details, including bow to detect the kind of network connections avail
able at run time, are included in Chapter 16, "Bluctooth, NFC, Networks, and
Wi-Fi."
At
What Cost?
if vou're a mobile device owner, you know all too well that some of your device's functionality can
Iiterally come at a price. Services such as SMS and data transfer can incur additional fees from your
service provider.
It's obvious why any costs associated with functionality in your applications should be minimized,
and that users should be made aware when an action they perform might result in their being
charged.
Ir's a good approach to assume that there's a cost associated with any action involving an interac-
tion with the outside world. In some cases (such as with GPS and data transfer), the user can toggle
Android settings to disable a potentially costly action. As a developer, it's important that you use
and respect those settings within your application.
In any case, it's important to minimize interaction costs by doing the following:
Sropping all data transfers and GPS updates when your Activity is not visible in the tore-
ground (provided they're only used to update the Ul)
Keeping the refresh/update rates for data transters (and location lookups) as low as
practicable
Scheduling big updates or transfers at of-peak times or when connected via Wi-Fi by us1ng
Alarms and Broadcast Receivers, as shown in Chapter 9
Often the best solution is to use a lower-quality option that comes at a lower cost.
when using location-based services, as described in Chapter 13, "Maps, Geocoding, and Location
bascd Services," you can select a location provider based on whether there is an associated cost.
wthin your location-based applications, consider giving users the choice of lower cost or greater
accuracy.
42 CHAPTER 2
t o dehn
or
different
f.
different user
people wll h
suw
hard Although
either plan5.
are
osts contract
c n w m e r a n e r s
provider
and
SA15.
insrime s e r e
free
have
which seems cheaper, consil
rherwren
will wh
orhers
R t r a n s t e r s ,
technique
based on
ading data from the lnternet, ask t
Ted
dara
particular download
times whes
Rathe
her
than
enfon
For
ing a
example,
when
users
are
limit
their
transters
to
hey're cO
choose or
available
tses
nerwork
wa
re
7sr ans
E n v i r o n m e n t
User's
Considering
the a p p l i c a t i o n as the most iportant
me
vour
users
will think of your
assume
that
cant
You
rheir devi roots as a mobile phone platf..
Ir'salso important to consider when and how your users will use your applications.
People us
mobiles all the time- on the train,
walking down the street, or even while driving their cars, i
can't make
people use their phones appropriately, but you can make sure that
don't distract them any more than your applications
necessary.
What does this mean in terms
of software design? Make sure that your
Is
predictable and well behaved-Start application:
they Te not in the by ensuring that your Activities
foreground. Android fires event handlers when
suspend wnen
resumed, you can so
pause Ul updates and your Activity is pa SeD
ble-there's no point network lookups when
Or updating your Ul if no one can see your applicatiou
processing
withour the Ul
in the
background, Android provides a it. If you need
to ue up.
overheads. Service class designed tor tns
Switches seamlessly pa
of mobile from the
devices, it's likely that background to the foregroun
ground- With the multitaskg
background. It's important your
that they applications will regularly move
terministic process to life" "come nd out ofthe
every chance management
will get it means quickly and seamlesslv.
quickly seamiessiy Android snol
ensure killed to free that if your
seamlessness by
don't notice saving resources. This applicat in the background,there
the should be in visible to the Youc
should bedifference between application state and queuing updates
user.
to it
seamless, with usersrestarting and queuing update so that youri
Is resuming
politeYour
Instead, application should being shown the Ul andyour application. Switc? ate they lasts
your
use
Notihcations
application isn't in the (detailed in
ne
never steal focus or application
Chapter 10) to interru a user's curren
toreground. There are request your use User's a t t e n t i o n v
mingcalls are announced by a rimgtone and/or vibration, when you have unread mes
t h e ED flashes;, and when you have new voice mail, a small unread mail icon appears
g he status bar. All these fechniques and more are available to your application using the
in the
Notihcanons mechanism
Deescnts an attractive and intutive Ul- Your application is likely to be one of several in
rue at any time, so it s important that the Ul you present is easy to use. Spend the time and
resources necessary to produce a Ul that is as attractive as it is functional, and don't force
ers fo interpret and relearn your application every time they load it. Usng it should be
imnle, easy, and obviousparticularly given the limited sereen space and distracting user
environment.
device. You've no doubt experienced the trustration of a "frozen" piece of software; the mul-
of a mobile makes this even more annoying. With the possibility of delays
rifunctional nature
caused by slow and unreliable data connections, it's important that your application use
worker threads and background Services to keep your Activities responsive and, more impor
Pertormance
Responsiveness
Freshness
Security
Seamlessness
Accessibility