Android onHandleIntent IntentService 等待 UI 线程

Android onHandleIntent IntentService waits for UI Thread

我注意到如果我从 UI 线程调用 startService,onHandleIntent 直到 UI 线程被释放后才会被调用。即 onHandleIntent 被 UI 线程阻塞。我尝试在 AsyncTask 中调用 startService,但 onHandleIntent 调用仍然被阻塞,直到 UI 线程被释放。 这是 Android 中的行为(或错误??)吗?或者我做错了什么?我正在 Android 6.0.

中测试它

这是我的意图服务

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);
          }
     }
}

这是我的 Activity 中的方法,单击我调用启动服务的按钮时将调用该方法

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();
        }

    }
}

handle只是一个activityclass成员变量public static Object handle = new Object();

这是 adb 日志

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

这种行为是完全正常的。它不是 IntentService 独有的。任何 startActivity()startService()bindService()sendBroadcast() 调用甚至都不会开始工作,直到您 return 来自您所在的任何主应用程序线程回调。

由于在回调中占用主应用程序线程超过 1 毫秒是不良做法,因此这种行为应该不会影响很多开发人员。