Originally posted at https://tech.labs.oliverwyman.com/blog/2010/09/26/android-postcode-app/
I’ve been playing around with Android development recently, and one of the apps is now worth sharing with everyone else. It’s not particularly complex, but last I looked, there wasn’t an app to do this: tell me what my postcode is. Given you’re holding a phone that knows where it is, and that can talk to the web where there are various services that will tell you what the postcode is for a given location, why not tie these things together? So, I built a little app that does all of this. It looks like the screenshot to the right, and that QR code you see is the market link to download it, or read on for more details about the development of it.
Android development has turned out to be remarkably nice, especially given I’ve done both a lot of embedded systems work and then tried to get stuff working on my old Symbian phone (short answer: don’t bother, it’s deeply frustrating to work with), and so this was a breath of fresh air. There was a few interesting items along the way however:
1) Location lookup:
First we need a location. You’ll need to cope with the fact that potentially the GPS could be switched off (the Android compatibility definition ensures there will be GPS, but not that it’ll be on), and there could potentially be other location providers, so it’s nice to use those as well if they’re any good. So, we dig through the LocationManager, wake up every provider we can find with getProviders() and tell them to give us some data ASAP. There are probably ways to do this more nicely, but given that we not only need to get accurate data, we need to make sure it’s not that old (which we can’t actually select on unfortunately, but we can ignore incoming data that’s too old), the easiest way is just to tell everyone to give you data, and then switch everything back off again once we’ve got the data. On my Desire, when outside or at least not in the bowels of the Tube, I tend to get location data in a few seconds like this. I also setup a timer for a minute, so if we’ve kept trying for that long and nothing good comes up, we throw up an error message and tell the user to try again later.
2) Postcode lookup
So we got a latitude/longitude, and it’s fairly recent, but now we need that translated into a postcode. Luckily, since the release of the OS OpenData dataset, which can be munged to get lat/long -> postcode, a few web services have popped up that’ll do this for us. They’re not all very reliable though, and given they’re web services, it’s entirely possible they’ll go away or change their API at some point, so we have a list of different services to try, and if anything goes wrong we just skip a service and try the next one. Current order is
Theoretically all 3 could go down eventually, but that should be enough for a bit. Ordering is based on my rather crude estimates of which ones have been most reliable/best data during my testing of the app. Of course, the phone’s data connection could not work, but again we throw an error and suggest you get the data connection sorted out if this happens.
3) Displaying the text
I started off just putting the text into a simple TextView, but it wasn’t very big and a bit difficult to read at the default font sizes. I could have started fiddling around with the font size and gotten something that would look good on my phone, but given I’d like this to be at least slightly cross-compatible, I ended up writing a revised TextView called AutoScaledTextView that automagically resizes text so it’s as big as possible given the amount of space available for the View (Android’s term for the basic displayable widget). It’s a bit kludgy (given non-fixed width fonts), but it seems to work reasonably well and does achieve the goal of displaying the postcodes nice and big.
Overall, this process has been fairly painless. I spent a bit of time figuring out how to do exactly what I wanted, but for the most part I felt Android was helping me along as opposed to being something I had to wrestle with. Provided you’ve got a phone (I’ve done parts of this before I had the phone, but it’s a bit nastier that way), getting yourself up and running and building pretty complex apps is quite easy, and I’ll be posting about another one of those that reuses the data we’ve gathered here today in another way later on. Code for all of this is on Github as usual, or you can just download it from the Market. Enjoy! I hope to see some reviews/bug reports soon