我注意到如果我从UI线程调用startService,则在释放UI线程之前不会调用onHandleIntent。即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中的方法,点击我正在调用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();
}
}
}
和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
答案 0 :(得分:0)
这种行为完全正常。它并非IntentService
所独有。在您从任何主应用程序线程回调中返回之前,任何startActivity()
,startService()
,bindService()
或sendBroadcast()
调用都不会开始执行其工作。
由于在回调中占用主应用程序线程超过1毫秒是不好的做法,这种行为不应该影响很多开发人员。