Kannan Loganathan Kannan Loganathan - 3 months ago 21
Android Question

Android onHandleIntent IntentService waits for UI Thread

I noticed that if i call startService from UI thread, onHandleIntent is not called until after UI thread is released. i.e onHandleIntent is blocked by UI Thread. I tried calling startService in a AsyncTask, still onHandleIntent call is blocked until UI thread is released.
Is this is a behavior (or bug ??) in Android ? or I am doing anything wrong? I am testing it in Android 6.0.

Here is my Intent Service

public class TestIntentService extends IntentService {

private static final String TAG = TestIntentService.class.getSimpleName();

public TestIntentService() {
super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "onHandleIntent Called");

try {
for (int i = 1; i <= 10; i++) {
Thread.sleep(1000);
Log.d(TAG, "onHandleIntent After " + i + " seconds");
}

synchronized (MainActivity.handle) {
MainActivity.handle.notify();
}

} catch (Exception e) {
Log.e(TAG, "Exception", e);
}
}
}


Here is the method in my Activity that will be called on click of a button where I am calling start Service

protected void onStartServiceButtonClicked(View view) {
Log.d(TAG, "startServiceClicked");

Intent intent = new Intent(context, TestIntentService.class);
startService(intent);
Log.d(TAG, "After calling startService");


synchronized (handle) {
try {
Log.d(TAG, "Before waiting");
handle.wait(4000);
Log.d(TAG, "After handler Wait");
} catch (InterruptedException e) {
e.printStackTrace();
}

}
}


and
handle
is just an activity class member variable
public static Object handle = new Object();


Here is the adb log

08-24 10:06:27.777 13564-13564/com.kl.testintentservice D/MainActivity: startServiceClicked
08-24 10:06:27.778 13564-13564/com.kl.testintentservice D/MainActivity: After calling startService
08-24 10:06:27.778 13564-13564/com.kl.testintentservice D/MainActivity: Before waiting
08-24 10:06:31.778 13564-13564/com.kl.testintentservice D/MainActivity: After handler Wait
08-24 10:06:31.782 13564-13564/com.kl.testintentservice I/Choreographer: Skipped 252 frames! The application may be doing too much work on its main thread.
08-24 10:06:31.783 13564-16520/com.kl.testintentservice D/TestIntentService: onHandleIntent Called
08-24 10:06:32.784 13564-16520/com.kl.testintentservice D/TestIntentService: onHandleIntent After 1 seconds
08-24 10:06:33.784 13564-16520/com.kl.testintentservice D/TestIntentService: onHandleIntent After 2 seconds
08-24 10:06:34.785 13564-16520/com.kl.testintentservice D/TestIntentService: onHandleIntent After 3 seconds
08-24 10:06:35.786 13564-16520/com.kl.testintentservice D/TestIntentService: onHandleIntent After 4 seconds
08-24 10:06:36.786 13564-16520/com.kl.testintentservice D/TestIntentService: onHandleIntent After 5 seconds
08-24 10:06:37.786 13564-16520/com.kl.testintentservice D/TestIntentService: onHandleIntent After 6 seconds
08-24 10:06:38.786 13564-16520/com.kl.testintentservice D/TestIntentService: onHandleIntent After 7 seconds
08-24 10:06:39.787 13564-16520/com.kl.testintentservice D/TestIntentService: onHandleIntent After 8 seconds
08-24 10:06:40.787 13564-16520/com.kl.testintentservice D/TestIntentService: onHandleIntent After 9 seconds
08-24 10:06:41.787 13564-16520/com.kl.testintentservice D/TestIntentService: onHandleIntent After 10 seconds
08-24 10:06:41.787 13564-16520/com.kl.testintentservice D/TestIntentService: handle notified

Answer

This behavior is perfectly normal. It is not unique to IntentService. Any startActivity(), startService(), bindService(), or sendBroadcast() call will not even begin doing its work until you return from whatever main application thread callback you are on.

Since tying up the main application thread in a callback for more than 1ms is poor practice, this behavior should not impact very many developers.

Comments