Wednesday, August 17, 2011

Alternatives to the BOOT_COMPLETED broadcast

The normal way to start an Application or Service is to implement a Broadcast Receiver that listens for the "boot completed" broadcast. This approach will only work for Applications that are not installed on the SD card, because "boot completed" is broadcasted before the SD card is mounted, therefore the Application can never receive the broadcast, see also: But it is generally a bad idea to force the installation on internal phone memory as it is restricted. It is more likely for a user to keep an Application installed if it uses as little as possible internal memory.
There is an alternative that can be used in many cases. The USER_PRESENT intent is broadcast when a user is present after the device wakes up, e.g. the user unlocks the screen. With this intent it is possible to start a service that is installed on the SD card.

Wednesday, August 3, 2011

Localization of Help Files

My application is using an html file in the asset folder as a help page. Unfortunately there is no direct support from the Android system to load assets based on locale. An easy and elegant workaround is to use a R.String value for the file name:

String helpFile = getString(R.string.help_html);
String url = "file:///android_asset/" + helpFile;

Then create html files for all the supported languages and name them, e.g., de_help.htm, es_help.html. Create the appropriate language folders in res, e.g., values-de, values-es and create string.xml files with the appropriate entries for the help files,e.g.:

<string name="help_html">de_help.html</string>

At this point you probably also want to translate the rest of the strings in your application.