This sort of issue can happen with other aspects of Android as well. As an example that I've personally encountered, if you don't ever release a camera object and your app crashes, the camera will be unavailable until the user reboots. You can handle these types of situation like so:
// Store the default uncaughtexceptionhandler in a variable.
private Thread.UncaughtExceptionHandler previousHandler = Thread.currentThread().getUncaughtExceptionHandler(originalExceptionHandler);
@Override
public void onCreate(Bundle bundle){
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler(){
@Override
public void uncaughtException(Thread thread, Throwable ex) {
try{
// release your wakelock here.
}
catch(Exception e){
// log
}
finally{
thread.setUncaughtExceptionHandler(previousHandler);
previousHandler.uncaughtException(thread, ex);
thread.setUncaughtExceptionHandler(previousHandler);
};
}
});
}
What you're doing here is overriding the normal crash behavior when an exception is encountered and giving yourself time to free up any problematic spots that really need to be cleaned up before the app fully crashes and you have no hope to recover.