A Simple Android App and a Threading Bug
by
Eric M. Burke, Principal Software Engineer
Object Computing, Inc. (OCI)
Introduction
By now, you probably know what Google
Android is: an
open source operating system, virtual machine, and SDK for mobile devices. In 2008,
T-Mobile released the first Android phone, the
G1. 2009 will
bring many different phones from a variety of carriers.
Android presents an exciting opportunity for programmers. Millions
of people will purchase Android phones in 2009, each including a link to the
Android Market.
For a nominal
$25 registration fee, any programmer can distribute free Android applications
on the Market. Beginning in January, you'll be able to sell commercial applications,
as well.
This article shows a simple Android application created with
the freely-available Android SDK using Eclipse. You can use any IDE you like, but
Eclipse currently offers the best Android support since Google provides an Android
development plugin. After showing this simple app, we tackle a common threading
bug and show how to fix it.
If you are new to Android development, you may want to work through
Google's
Notepad Tutorial before proceeding.
Threading Rules
Like other GUI toolkits, the Android user interface is single-threaded.
To avoid locking up the GUI, long running operations must run in background
threads. This should sound familiar to Swing programmers, although Android differs
in two notable ways:
- Android fails fast when background threads update GUI components.
Rather than silently ignoring this kind of threading bug, Android throws
CalledFromWrongThreadException and immediately terminates the activity.
- If a long-running process
locks up the UI, Android intervenes and displays this dialog to the user:
These are welcome improvements because they encourage correct
code and help programmers locate bugs early in the development process. They also
prevent poorly written applications from locking up your entire phone.
|
|
Read more...
|
|
Today I want to talk a little bit about concurrency in Android
applications, and the problems it poses on the developer. If you have used Android
on your phone before, it’s likely that you have stumbled upon applications which
load data off the internet, or perform other time consuming operations. The problem
with time consuming operations is that, well, they consume time, and if they aren’t
perfomed concurrently to Android’s user interface thread (the main thread), then
the UI will lock up — certainly not a good user experience. So, it’s pretty obvious
that on internet phones like those based on Android, highly concurrent applications
are more the rule than the exception.
I guess I don’t have to mention that developing concurrent applications
is everything but simple. Keeping threads that share data in sync is not a trivial
task and prone to errors. What makes it even more difficult in Android is the fact
that while your application is loading data, it may suddenly be interrupted by an
incoming phone call or because the user decided to flip the screen into a different
orientation. You may think that your thread will get paused while the activity that
created it is brought to the background (or even gets destroyed). That’s not what
happens though: any thread will continue running until it completes, even if your
activity or service is not alive anymore. And that’s where the pain starts.
|
|
Read more...
|
|
User interface responsiveness is a crucial thing for all applications
that require user interaction, but maybe even more so when programming for mobile
handsets. Google’s Android
programming environment unfortunately does not provide any mechanism for handling
operations that may require a fair amount of time to complete, but which in itself
are not meant to be implemented as
Android Services. An example for this would be network I/O in an
Activity, such as posting data to (or retrieving data from) a remote Web server.
Because the Android runtime will terminate any Activity that does not respond within
a couple of seconds, it is impossible (and simply a bad idea anyway) to perform
such tasks from within the UI main thread.
|
|
Read more...
|
|
I don’t know how many hours I’ve spent struggling with Android’s
theme engine, trying to figure out — mostly by trial and error — how styles are
applied and how to properly define them (let’s be honest: the documentation on styles
in Android is a bit lacking). I thought it may be worth sharing what I’ve found
out so far, in order to save my fellow Android developers from baldness due to ripped
out hair (I sure lost some). This is a work in progress (so is Android), and some
information may be inaccurate. If you find anything that you think is wrong or unclear,
please let me know by dropping a line in the comments section. On another note,
I assume that you have read the
reference documentation on Android styles, if not, you should do that now.
What are Android styles?
A style in Android is a collection of attribute/value pairs applied
to a view, an Activity or the entire application (which means, all Activities in
your application). Styles for Activities are called themes, but do not get confused:
they are syntactically equivalent to styles (because they are styles),
they merely have a different scope. At this point I want to clarify something upfront:
Themes or styles in Android have nothing to do with user styles known from
applications such as, say, Firefox. Android users can not change the looks of Android
applications by downloading themes from the Market or creating their own (yet).
Android themes are of interest for developers only, think of them as syntactic sugar,
a way of making your application’s view code more
DRY.
|
|
Read more...
|
|
Dedexer
got competition! Not one but immediately two -
smali and baksmali
is a DEX assembler/disassembler pair. The programs are brand new and - like Dedexer
- they are also based on a Jasmin-like format. I tried them very shortly, the disassembler
- baksmali - worked correctly and indeed produced an output similar to dedexer but
I could not compile the output of baksmali back to DEX format with the assembler
- smali. The disassembler does not handle ODEX files either.
|
|
Read more...
|
|