Pages

Tuesday, May 22, 2012

Escaping String Resources

I just stumbled on the fact that quotes won't work in Strings in xml resource files (That's single or double quotes.)  More to the point, I stumbled on the fact that the Eclipse resource editor doesn't automatically translate forbidden characters into the desired format, nor does it indicate that the string you have just typed is illegal.  So you could be stuck with the generated resource class (the "R" class) refusing to recompile for no readily apparent reason.  (You'd have to switch to the xml view on the resource editor to see an indication of where the error is.

So how do you get around the problem?  This guide shows how you can either put the entire string in quotes or, as in C, precede the apostrophe/quote with a backslash.  But surprisingly, you can't use HTML-style escape characters (such as ")

Tuesday, March 13, 2012

Moving work off the UI Thread

This is a mistake that I've seen a lot of people make (by which I mean me.)  I can't speak for other platforms, but in both Swing and Android, there is only one thread running the UI.  In other words, while your handler method is running in response to a button press or some other UI action, nothing else is getting done in the interface, including listening for any other input by the user.


That's fine when your handler is only doing small actions in response the user actions.  But if you have to do something that could take several seconds, you should start a new thread to do it so the UI thread can go back to its work.

But where this can really trip you up is an activity that takes an unpredictable amount of time.  Or, more specifically, an activity that usually takes a short time, but could drag on.  Network communications are a place where this can happen: the network may react quickly most of the time, but become congested occasionally.  I imagine many people will be making this mistake in mobile programming.

There are a number of different ways of approaching Threads in Android, which I'll start reviewing in upcoming posts.  The simplest way is to use the traditional Java way: create a class implementing the Runnable interface, then create a Thread object, passing an instance of your Runnable class as a parameter.

You'll find this approach is quite limited because your new thread can't do anything to affect the UI - only the UI's thread can.  So if you want the interface to react to the actions of the background thread, you'll want to try a different approach.

Monday, January 16, 2012

Expiring Debug Certificate

I recently found that the application I was working on was mysteriously not compiling.  Eclipse wasn't pointing out any errors in the code, so it lead to a brief moment of panic/frustration.

It turns out this was just the Android SDK's way of wishing me a happy anniversary as an Android developer:  My signing certificate had expired.

[Aside for anyone who has stayed blissfully ignorant of the signing process]  All Android applications have to be signed with a certificate to identify the creator.  By default, your applications are signed with a special Debug Certificate which can't be used to distribute your application.  This certificate is automatically created when you first compile an application, and it expires after one year.  Once it expires, you need to recreate it. [End of aside]

You can find an explanation of it at the Android Developer site.  I'll save you some trouble and quote the easy fix for the problem here:

To fix this problem, simply delete the debug.keystore file. The default storage location for AVDs is in ~/.android/ on OS X and Linux, in C:\Documents and Settings\<user>\.android\ on Windows XP, and in C:\Users\<user>\.android\ on Windows Vista and Windows 7.

The other problem is you'll have to reinstall apps you installed using the old certificate.  And while you're at it, this would be a good time to read the whole page on signing and familiarise yourself with the concept. You know, just in case you hadn't bothered to learn about it until now.