Global Exception handling in Android

To be able to catch any exception you may encounter that isn’t part of a try-catch-clause, you can use the Thread.setDefaultUncaughtExceptionHandler( Thread. UncaughtExceptionHandler ) method.

In the handler you can then perform whatever action you want. Don’t forget to call “System.exit()” though, because otherwise this won’t work!

A simple example:

  1. public class SomeActivity extends Activity {
  3.     private static final String LOG_TAG = "SomeActivity";
  5.     @Override
  6.     protected void onCreate(Bundle savedInstanceState) {
  7.         super.onCreate(savedInstanceState);
  8.         Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
  9.             @Override
  10.             public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
  11.                 Log.e(LOG_TAG, "OMG! Uncaught Exception!");
  12.                 // Without calling System.exit() this will not work.
  13.                 System.exit(2);
  14.             }
  15.         });
  16.     }
  17. }

For some more details have a look at “Using Global Exception Handling on android” on


To do some faster image manipulation I’ve been looking at Renderscript.

Renderscript provides a platform-independent computation engine that operates at the native level. Use it to accelerate your apps that require extensive computational horsepower.

This made it a better candidate than using the NDK with JNI. It’s also easier to maintain as an Android developer.

What I wanted to achieve is a fast way to apply Gaussian blur. Googling around will provide you plenty ways to do this in Java. It let me to a blog post about using a Convolution matrix combined with various kernels to achieve different effects. Unfortunately doing this on large images in Java is slow.

The other option was to use JNI, but I don’t feel confident enough about writing C/C++. There are, however, some libraries out there worth looking at. OpenCV and Lightbox’ PhotoProcessing for instance. But when you’re just trying to use one or two effects, this seems a bit overkill.

Last but not least is Renderscript. This is a bit more accessible than the NDK and only results in a slight loss of performance compared to using the NDK. The only requirement is Android 3+ (API level 11 and above).
If you’re looking to get started with Renderscript, Google has a good introduction and more in-depth article about the design goals of Renderscript. The Renderscript reference comes in very handy when you are writing your own scripts.
At first I was happy to find the ScriptIntrinsicBlur class, but unfortunately that one is only available from API level 17. Discarding that option, I decided to implement my own class to create a Gaussian blur effect. To start work on a fast algorithm for Gaussian blur, I had a look at how this is being done for games and came across a blog post about “Efficient Gaussian blur“. I’ve made a first (yet to be further optimized) version and tried it out, you can find the script at Github. This implementation already renders a Gaussian blur effect extremely fast. It looks great when using the emulator, but unfortunately when used on a phone it seems to produce some scanline-like side-effect.

The biggest downside of Renderscript is that it is only available on Android 3+. It does, however, look like Google is working on a compatibility library for Android 2.2. When you clone ““, there seems to be a “renderscript\v8” folder. I tried building it using the supplied Make file, but there were some errors.

I hope the compatibility library for Renderscript arrives soon, so it can be used across a bigger range of devices.

Sending SMS

Being able to send an SMS to a device can come in really handy from time to time. Below are two methods for sending text to a device using SMS. The first is to send to a hardware device, the second to send to a virtual device on an emulator.

Hardware device

  1. > adb shell
  2. $ am start -a android.intent.action.SENDTO -d sms:01123123123 --es sms_body "Text to send" --ez exit_on_sent true
  3. $ input keyevent 22
  4. $ input keyevent 66

For more info, see “How do I send an SMS from a shell?” on Stackoverflow.


  1. Open the DDMS perspective in Eclipse.
  2. Under “Telephony Actions” select “SMS”.
  3. Fill out “Incoming number” and “Message”.
  4. Hit “Send”

For more info, see “Using DDMS” on the Android developers site.

Fast, user-friendly and power-efficient network communication on Android

Notes from Erik Hellman’s talk about “Fast, user-friendly and power-efficient network communication on Android” on the second day of DroidCon UK 2012.

Doing network communication the right way isn’t all that hard, just keep a couple of recommendations in mind. There is no need to “re-invent the wheel”, other fields have struggled with this problem before, we can learn from them. There are also a lot of open source projects that tackle various network related problems, quite often one of those will meet your requirements.

That being said, handling the network on a mobile phone can be difficult. The connection can drop out of nowhere, the quality can change or you might not even be online at all. There are a couple of things that developers can do to make the experience better:

  •  HTTP Client handling
  • Background network I/O
  • Playing server

Generally speaking HttpUrlConnection is the best client to use, except for Eclair and Froyo, for these API versions the Apache HTTP client has fewer bugs.
Make sure to wrap the HttpUrlConnection streams in buffers and always exhaust the streams.

Enable caching, it will save a lot of network traffic.

To avoid blocking the UI thread, always run your network related tasks on a different thread using a Service with a Handler implementation.
If you need to poll a URL on a regular basis, use the AlarmManager to schedule requests at a set (slightly inexact) interval.

An Android device could also serve as a server (think multiplayer gaming). This could be quite complex to create, but luckily the library will probably meet most of your needs.

And lastly, don’t use a splash-screen, there should be no need for it. Give the user as much control as possible.

You can watch the full presentation on the Droidcon UK website or read it in pdf format on Sony’s site.

Custom Notifications

Today I ran into an interesting problem with custom Notifications. The following error kept on being thrown while I was testing with a Samsung Galaxy Ace phone:

“android.widget.RemoteViews$ActionException: can’t find view: …”

My custom Notification contains an ImageView and some TextViews. The image for the ImageView was being loaded from a remote server. When the image was retrieved I tried updating the Notification by modifying a clone of the original Notification I sent, calling:

  1. RemoteView.setImageViewBitmap(myViewId, bitmap)

 After this the app would crash. The LogCat output would then show the error above.

Having a close look at the other Notifications displayed by the phone, it turned out that ImageViews were automatically removed.

To solve the issue you have to make sure to always create a new Notification, that way your custom XML gets inflated and the ImageView is (again) accessible.

Device Fragmentation by Robin Puthli

Notes from Robin Puthli’s talk about Device Fragmentation among Android devices on the 1st day of DroidCon UK 2012.

Device fragmentation starts to become a big part of Android development as there are more and more Android devices available. As a result there are a lot of different resolutions and screen sizes that need to be taken into account. has a very interesting report on their blog that illustrates this fragmentation.

Luckily (device) fragmentation is a very old problem. It had to be solved on operation systems (Window, Mac, Linux, etc), browsers (Chrome, FireFox, Internet Explorer, etc), Java (ME, SE, FX, etc) and more before.

There are roughly nine strategies you can pick from to deal with device fragmentation.
Multi-manual: Phone vs tablet, writing separate code for each.
Derive multi: QT or J2ME Polish, Mono Touch, writing pseudo code and then letting the framework generate the rest.
One (size) fits all: HTML5, PhoneGap. This approach is based on coding for the lowest common denominator.
Single adapt
: Writing adaptive layouts, using 9-patches, fragments.

“There is no silver bullet. You have to pick and mix [from the available solutions].”

The full presentation can be found at Itude Mobile’s blog.

OpenCV for Android by Erik Hellman

Notes from Erik Hellman’s talk about OpenCV for Android on the 1st day of DroidCon UK 2012.

Open source Computer Vision library, containing loads of modules and image (related) algorithms.

The code is very well documented, but most samples are in C++.

A couple of examples of what you can do with OpenCV are:

  • Object detection, i.e. face detection.
  • Salient feature detection, i.e. hand or a door frame.
  • Motion tracking.

The talk mainly covered demonstrations of what you can do with OpenCV, rather than going into more detail.

Library: Download OpenCV (for Android)
Documentation:  OpenCV API Reference

AllJoyn by Mitch Williams

Notes from Mitch Williams’ talk about AllJoyn on the 1st day of DroidCon UK 2012.

“AllJoyn™ is an open-source application development framework that enables ad hoc, proximity-based device-to-device communication that is OS agnostic.”

There are other frameworks like DLNA, UPnP and Bonjour as well. DLNA would be a good choice if you only wanted to do one-to-one device communication. UPnP and Bonjour are missing a lot of the higher level communication compared to AllJoyn.

AllJoyn is multi-platform as a result of the native implementation being written in C++. The currently supported platforms are:

  • Android 2.1 and above
  • iOS
  • Window XP, 7 and 8
  • Linux
  • OS X

Source code: AllJoyn @ Github.
Documentation: AllJoyn Java docs.

Introduction to Polaris by Cyril Mottier

Notes from Cyril Mottier’s talk about Polaris on the 1st day of DroidCon UK 2012.

Polaris is a mapping library for Android. It was created because with the current Google Maps API it is painful to even do simple stuff.

Some features of Polaris include:

Gesture support

  • Simple tap (opens a call out)
  • Double tap (zoom in or zoom to the maximum zoom level for the location)
  • Long press

(Map) Annotations with a “variable anchor” (the arrow/pointer of the annotation is dynamically placed based on the location’s position on screen).

Demo: Polaris Sample @ Google Play Store.
Source code: Polaris @ Github.

NOTE: Polaris may become obsolete as Google is working on a new Maps API for Android.