private void onHandleIntent ( GroundyTask groundyTask ) { if ( groundyTask == null ) { return ; } boolean requiresWifi = groundyTask . keepWifiOn ( ) ; if ( requiresWifi ) { mWakeLockHelper . acquire ( ) ; } L . d ( TAG , "Executing value: " + groundyTask ) ; TaskResult taskResult ; try { taskResult = groundyTask . doInBackground ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; taskResult = new Failed ( ) ; taskResult . add ( Groundy . CRASH_MESSAGE , String . valueOf ( e . getMessage ( ) ) ) ; } if ( taskResult == null ) { throw new NullPointerException ( "Task " + groundyTask + " returned null from the doInBackground method" ) ; } if ( requiresWifi ) { mWakeLockHelper . release ( ) ; } //Lets try to send back the response Bundle resultData = taskResult . getResultData ( ) ; resultData . putBundle ( Groundy . ORIGINAL_PARAMS , groundyTask . getArgs ( ) ) ; resultData . putSerializable ( Groundy . TASK_IMPLEMENTATION , groundyTask . getClass ( ) ) ; switch ( taskResult . getType ( ) ) { case SUCCESS : groundyTask . send ( OnSuccess . class , resultData ) ; break ; case FAIL : groundyTask . send ( OnFailure . class , resultData ) ; break ; case CANCEL : resultData . putInt ( Groundy . CANCEL_REASON , groundyTask . getQuittingReason ( ) ) ; groundyTask . send ( OnCancel . class , resultData ) ; break ; } }
This method is invoked on the worker thread with a request to process . Only one Intent is processed at a time but the processing happens on a worker thread that runs independently from other application logic . So if this code takes a long time it will hold up other requests to the same IntentService but it will not hold up anything else .