Monday, February 09, 2009


Minimal code to perform logging in Google Android

To produce log message in Google Android is really quite easy.

At the top of the file in the imports section add: import android.util.Log;

And then in your code you add Log.d(TAG,message);

Where TAG and message are strings.

It is considered good form that the TAG is a constant for the class.

So I put something like this at the top of the class private static final String TAG = Foo.class.getName();

There are several levels of logging and in level order these are Log.v(), Log.d(), Log.i(), Log.w() and Log.e()

v = verbose
d = debug
i = info
w = warning
e = error

The logging is performed by LogCat which can be accessed via the adb tool that comes with the Android developer kit.

tools/adb logcat will start logging at the INFO level as that is the default.

tools/adb logcat *:V will log everything at VERBOSE level.

tools/adb logcat Wibble:W will filter most things and only show those log statements with the tag Wibble at the WARNING level.


Thursday, February 05, 2009


Minimal code to display a Toast message in Google Android

Toast is a widget to display an informational message to a user whilst they may well be doing something else. I find it useful for debugging on a real device to tell me something internal to the application has been fired. Afterwards I can just strip such things out of the build.

// show the frost pist message using the Toast widget
Toast toast = Toast.makeText(context, "Woo hoo, toast", Toast.LENGTH_LONG);;

I find the context is normally part of the method signature.
Toast.LENGTH_LONG keeps the message up for a few seconds.

There is a Toast.LENGTH_SHORT if you don't want the message to hang about for too long.



Minimal code to retreive a GPS location in Google Android

Here I describe the minimal code to retrieve a GPS location in the Google Android OS.

First up, a GPS location listener.
class GPSLocationListener implements LocationListener {

public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Somewhere else, probably in the Activity you need to tie this listener in. Here is an onCreate from a minimal application's Activity .
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {



GPSLocationListener gpsLocationListener = new GPSLocationListener();

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

long minTime = 600000;
float minDistance = 10;

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, gpsLocationListener);

minTime is a request to the OS about how often to check the GPS location. If you set it less than 1 minute (60000) then you risk the battery going flat quickly because the GPS receiver will be on all the time. The number is just a request. The OS may request a fix more often, it may check less often.

minDistance is a request to trigger the listener when the device has moved by this distance.

You also need <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
in the applications AndroidManifest.xml file.



Minimal code to send a SMS from Google Android

A minimal code for sending a SMS message from the Google Android OS.

This code assumes you're in an Activity , which is where it gets this :

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(), 0)

SmsManager smsManager = SmsManager.getDefault();

String receiver = "5556"; // the phone number of the device to send the SMS message to.

String message = "This is the SMS message I want to sending";

smsManager.sendTextMessage(receiver, null, message, pendingIntent, null);

What it requires from this is actually just the interface to the Context .

The <uses-permission android:name="android.permission.SEND_SMS"></uses-permission> is also required your build's AndroidManifest.xml file


Tuesday, February 03, 2009


Telling the android emulator where you are.

Once telnet ed into an emulator you can poke in a pair of map co-ordinates to give the impression the GPS unit has just got a fix.

Fun thing to remember is that the latitude and longitude are the wrong way round: you put the longitude first. Additionally use minus signs instead of chars, remembering North and East are positive.

So to fix a position like 55.623131N 11.997169 becomes 11.997169 55.623131

telnet localhost 5554
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying fe80::1...
telnet: connect to address fe80::1: Connection refused
Connected to localhost.
Escape character is '^]'.
Android Console: type 'help' for a list of commands
geo fix 11.997169 55.623131

Note it uses proper decimal, such that 55° 35' 10" becomes 55.58611111.

For extra fun the geo command can even take a real nmea message

geo nmea $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47

And if you tell the mapping application in the emulator to 'My Location', off the map will fly.



Sending an SMS into a Google Android emulator.

It appears that the ability to send SMS from one android emulator to the other via the SMS application is not currently working, but luckily you can send an SMS into one emulator as if it came from another...

What you do is telnet into the emulator you wish to poke the message into, and then tell it that it's just got a message from some place. In this example below, it is one of the other emulators.

telnet localhost 5554
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying fe80::1...
telnet: connect to address fe80::1: Connection refused
Connected to localhost.
Escape character is '^]'.
Android Console: type 'help' for a list of commands
sms send 5556 Hello!

If you've something listening out for SMS messages, it will now trigger and the two emulators can chat, if that is what you want.


Monday, February 02, 2009


Running multiple Android emulators

A little script to fire up multiple Google Android emulators that have their own image, i.e. can save their data separately.
It then kills any adb server that may be running, starts a new one, and lists the running emulators.

#! /bin/bash

xterm -geometry 132x100+100+100 -sb -e bash -c '/Library/android-sdk-mac_x86-1.0_r2/tools/emulator -skin HVGA-L -data droids/droid-01.img -verbose -logcat "out"' &
xterm -geometry 132x100+200+200 -sb -e bash -c '/Library/android-sdk-mac_x86-1.0_r2/tools/emulator -skin HVGA-L -data droids/droid-02.img -verbose -logcat "out"' &
xterm -geometry 132x100+300+300 -sb -e bash -c '/Library/android-sdk-mac_x86-1.0_r2/tools/emulator -skin HVGA-L -data droids/droid-03.img -verbose -logcat "out"' &
sleep 30s
/Library/android-sdk-mac_x86-1.0_r2/tools/adb kill-server
/Library/android-sdk-mac_x86-1.0_r2/tools/adb start-server
/Library/android-sdk-mac_x86-1.0_r2/tools/adb devices

-skin HVGA-L : 480x320, landscape, puts the emulator of the screen sideways

There are other options for skin: HVGA-P is default. QVGA-L gives a 320x240, landscape and QVGA-P is 240x320, portrait

-data droids/droid-01.img : a different number for each running emulator ensures they save their data to their own area.

-verbose : lots of logging information. Can slow the emulator down to the point where they give error messages because they run things too slow...

-logcat : the first rule of logcat is: no one talks about logcat.



Getting and building Google Android from the source code on Mac OS X

Install XCode version 3.0, available from

Install MacPorts, available from

Open up a terminal, check /opt/local/bin is at the start of $PATH, and run
sudo port selfupdate
Hopefully it'll say something like:

MacPorts base version 1.700 installed
Downloaded MacPorts base version 1.700

The MacPorts installation is not outdated so it was not updated
Get these packages from port, with the following command:
POSIXLY_CORRECT=1 sudo port install gmake libsdl git-core gnupg
Go make a cup of coffee, or two, as several minutes will pass, depending on your machine and download speeds, and several terminal screens full of build comments will scroll slowly by.

Installing repo

Go back to your home directory:
cd ~
If you don't already have one make a bin folder:
mkdir bin
Make sure it's on the path:
export PATH=/Users/threaded/bin:$PATH
echo $PATH
Download the repo script:
curl >~/bin/repo
Look to see if it is executable:
ls -al ~/bin/repo
Most probably it isn't so:
chmod a+x ~/bin/repo
To build the Android files you're recommended to use a case-sensitive Journaled HFS+. If you don't want to go creating a partition, formatting it, all the other ball-n-chain, you can, on the Mac, create a disk image. It is recommend to be 8 GB, but more won't harm.

What I did:
The OS should mount the .dmg file created, at /Volumes/mydroid, and we use this for the following work:
cd /Volumes/mydroid
Run repo init to get the files:
repo init -u git://
If you get a permission denied, then you've forgotten to do the chmod to repo mentioned above, like I did. ;-)

After a little huff-n-puff, it'll ask for a name and email. The name should be real, as it'll be used for attributions, and the email address needs to be to a Google account. This doesn't have to be a Gmail address, but that's easiest.

Now you can get the files:
repo sync
Again this is long-winded, so there's time for yet another coffee.

Now, finally you can:
If you get:
build/core/ ************************************************************
build/core/ You are building on a case-insensitive filesystem.
build/core/ Please move your source tree to a case-sensitive filesystem.
build/core/ ************************************************************
build/core/ *** Case-insensitive filesystems not supported. Stop.
That'll be because you are not where you think you are, but should rather be inside a Case-Sensitive, Journaled file system. ;-)

Another chance for several more cups of coffee. Lots of warning messages fly by on the terminal that don't really mean that much.

You can see if it works by dragging the newly built emulator to a terminal window:

which should illicit the response:
emulator: ERROR: You did not specify a virtual machine name, and the system
directory could not be found.

If you are an Android SDK user, please use '@ ' or '-vm '
to start a given virtual machine (see -help-vm for details).

Otherwise, follow the instructions in -help-disk-images to start the emulator


Thursday, October 09, 2008


Gotchas with Google Android Development

If your screen resolution is not very high, you'll have problems with the Android Emulator. Needs a 768 pixel depth minimum. So you're out of luck with the standard setting on an eee PC, for example. Luckily there is a -scale setting for the emulator, which can vary between 0.1 and 3, so a value of around 0.6 makes the Android Emulator fit to a 600 pixel depth screen.

The Android Emulator can take a fair time to start up, up to 20 minutes on a PC, (but usually only 10 seconds or so on a mac pro,) so give it a minute or so with A N D R O I D displayed before you give up and look at what might be wrong. (It'll generally tell you on the command line if there really is something wrong, and that you're not being a tad hasty to kill the process.)

On really slow PCs you may get a message
Application Messaging (in
process is
not responding.

Which means, if you get it frequently, your machine is way too slow: you might like to try and install Linux or go out and buy something faster like a Mac.



Getting started with Google Android

Some clues to getting started with Google Android development.

Download the Android SDK and get the emulator to work.
Android SDK:

When you've unpacked it and tried to run the emulator you'll maybe get some failure mentioning -datadir .

I generally open up a terminal and create the directory it wants.

mkdir .android
cd .android/
mkdir SDK-1.0

or you could just put it on the command line

emulator -datadir ~/temp

Then you want the Eclipse IDE:

Eclipse IDE for Java EE Developers (i.e. the 163 mb one)

Fire up Eclipse and now install the Android Development Tools.

In Eclipse:
Software Updates...
Available Software
Add Site...

etc. etc.

I think it's best to restart Eclipse when you get to the end.

And there you have it, a Google Android development environment.


This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]