2016-08-24 11 views
0

UIスレッドからstartServiceを呼び出すと、UIスレッドが解放されるまでonHandleIntentが呼び出されないことに気付きました。つまり、onHandleIntentはUIスレッドによってブロックされます。 AsyncTaskでstartServiceを呼び出そうとしましたが、UIスレッドが解放されるまでonHandleIntent呼び出しはまだブロックされています。 これはAndroidのの動作(またはバグ??)ですか?私は間違ったことをしていますか? Android 6.0でテストしています。ここで Android onHandleIntent IntentServiceがUIを待つスレッド

は、ここに私の意図サービス

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

ある

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はちょうど私がサービスを開始呼び出していますボタンのクリック時に呼び出されます、私の活動の方法であり、アクティビティクラスメンバ変数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 
+1

そのバグではなく、その機能は、何も間違っていません – pskink

答えて

0

この動作は完全に正常です。 IntentServiceではユニークではありません。すべてstartActivity()startService()bindService()、またはsendBroadcast()コールは、あなたがメインアプリケーションスレッドコールバックから戻ってくるまで作業を開始しません。

コールバックでメインアプリケーションスレッドを1ミリ秒以上延期することは実践的ではないため、この動作は非常に多くの開発者に影響を与えません。

+0

ありがとう。私は間違いなくUIスレッドをブロックしたくありません。私の意図は、非同期インテントサービスからのデータを同期することです。私は持っているサービスはBLEを介してデータを取得し、それは非同期であり、通常500ミリ秒でデータを取得します。 UIコンポーネントがコールバックを心配する必要がなく、単に同期呼び出しでデータを取得する必要があるように、UIスレッドで最大1分待つことができるかどうかを確認したかったのです。 UIコンポーネントはファサードを呼び出し、ファサードは1秒間待機し、データを取得できない場合は例外をスローします。 UIコンポーネントは例外を処理する必要があるだけです –

+0

@ KannanLoganathan:「UIコンポーネントで最大1分待つことができますか?UIコンポーネントはコールバックを心配する必要はなく、単に同期呼び出しでデータを取得する必要があります" - いいえ。まず、あなたの質問にあなたが引用した問題があります。第2に、あなたのUIはこの時間中に完全に凍結されます。第三に、メインアプリケーションスレッドを数秒間張っておくと、Androidがアプリを終了させます。これは、アプリがひどく壊れていると仮定しているからです。 – CommonsWare

関連する問題