Search code examples
androidintentservicecommonsware-cwac

WakefulIntentService NEW CRASH reports as my users are migrated to ICS?


My users are slowly being migrated to ICS (Android 4.0 and above) and since then I can see new crash reports appearing ... it looks like its my implementation of WakefulIntentService that triggers the following error:

java.lang.NullPointerException at com.cousinHub.meteo.AppService.doWakefulWork(AppService.java:104) at com.cousinHub.meteo.WakefulIntentService.onHandleIntent(WakefulIntentService.java:70) at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.os.HandlerThread.run(HandlerThread.java:60)

line 70 looks the issue within onHandleIntent :

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

abstract public class WakefulIntentService extends IntentService {
    abstract protected void doWakefulWork(Intent intent);

    public static final String LOCK_NAME_STATIC="com.commonsware.cwac.wakeful.WakefulIntentService";
    private static PowerManager.WakeLock lockStatic=null;

    public static void acquireStaticLock(Context context) {
        getLock(context).acquire();
    }

    synchronized private static PowerManager.WakeLock getLock(Context context) {
        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);
    }

    public static void sendWakefulWork(Context ctxt, Intent i) {
        acquireStaticLock(ctxt);
        ctxt.startService(i);
    }

    @SuppressWarnings("unchecked")
    public static void sendWakefulWork(Context ctxt, Class clsService) {
        sendWakefulWork(ctxt, new Intent(ctxt, clsService));
    }

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

    @Override
    public void onStart(Intent intent, int startId) {
        if (!getLock(this).isHeld()) {  // fail-safe for crash restart
            getLock(this).acquire();
        }

        super.onStart(intent, startId);
    }

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

Any idea why this code would work fine on Gingerbread (Android < 4.0) and now breaks ??

try {
            doWakefulWork(intent);
        }

=> intent looks to be null for some reason ??

or maybe it's the next block of code that triggers the NullPointer Exception :

finally {
            getLock(this).release();
        }
  • how would you solve this ?

this way maybe ?

if ((this!=null)&&(intent!=null)) {
            try {
                doWakefulWork(intent);
            }
            finally {
                getLock(this).release();
            }
        }

Solution

  • You will notice that your exception arises from:

    com.cousinHub.meteo.AppService.doWakefulWork(AppService.java:104)
    

    First, I do not write anything in the com.cousinHub.meteo package. Second, this would be in the implementation of a doWakefulWork() method, and the only ones of those I have published are merely samples. Third, you declined to publish your implementation of doWakefulWork() and point out which is line 104.

    I humbly suggest that you examine your AppService class, line 104, to determine where you are going wrong.