package com.markupartist.sthlmtraveling.service;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;

abstract public class WakefulIntentService extends IntentService {
    private static String TAG = "WakefulIntentService";
    public static final String LOCK_NAME_STATIC =
        "com.markupartist.sthlmtraveling.service.WakefulIntentService.Static";
    private static PowerManager.WakeLock lockStatic = null;

    public WakefulIntentService(String name) {
        super(name);
    }

    synchronized private static PowerManager.WakeLock getLock(Context context) {
        Log.d(TAG, "About to get lock");
        if (lockStatic == null) {
            PowerManager mgr =
                (PowerManager)context.getSystemService(Context.POWER_SERVICE);
            lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    LOCK_NAME_STATIC);
            lockStatic.setReferenceCounted(true);
        }
        return lockStatic;
    }

    abstract void doWakefulWork(Intent intent);

    public static void acquireStaticLock(Context context) {
        Log.d(TAG, "About acquire static lock");
        getLock(context).acquire();
    }

    @Override
    final protected void onHandleIntent(Intent intent) {
        doWakefulWork(intent);
        getLock(this).release();
    }
}

@@ -365,18 +365,15 @@
 NAME
-_STATIC =%0A
+ =%0A    
     
@@ -668,49 +668,8 @@
 ) %7B%0A
-        Log.d(TAG, %22About to get lock%22);%0A
     
@@ -728,32 +728,16 @@
 er mgr =
-%0A               
  (PowerM
@@ -743,16 +743,17 @@
 Manager)
+ 
 context.
@@ -793,16 +793,17 @@
 RVICE);%0A
+%0A
         
@@ -870,28 +870,8 @@
 OCK,
-%0A                   
  LOC
@@ -880,15 +880,8 @@
 NAME
-_STATIC
 );%0A 
@@ -939,16 +939,17 @@
       %7D%0A
+%0A
         
@@ -955,16 +955,17 @@
  return 
+(
 lockStat
@@ -966,16 +966,17 @@
 ckStatic
+)
 ;%0A    %7D%0A
@@ -1244,24 +1244,42 @@
 t intent) %7B%0A
+        try %7B%0A    
         doWa
@@ -1309,32 +1309,336 @@
     
-getLock(this).release();
+%7D finally %7B%0A            PowerManager.WakeLock lock = getLock(getApplicationContext());%0A            if (lock.isHeld()) %7B%0A                try %7B%0A                    lock.release();%0A                %7D catch (Exception e) %7B%0A                    Log.e(TAG, %22Exception when releasing wakelock%22);%0A                %7D%0A            %7D%0A        %7D
 %0A   

Add some error handling around releasing wake locks