[英]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. 我注意到,如果我从UI线程调用startService,则在UI线程释放后才调用onHandleIntent。 ie onHandleIntent is blocked by UI Thread.
即onHandleIntent被UI线程阻止。 I tried calling startService in a AsyncTask, still onHandleIntent call is blocked until UI thread is released.
我尝试在AsyncTask中调用startService,但直到UI线程释放后,onHandleIntent调用仍被阻止。 Is this is a behavior (or bug ??) in Android ?
这是Android中的行为(或错误??)吗? or I am doing anything wrong?
还是我做错了什么? I am testing it in Android 6.0.
我正在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();
并且
handle
只是一个活动类成员变量public static Object handle = new Object();
Here is the adb log 这是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
This behavior is perfectly normal. 此行为是完全正常的。 It is not unique to
IntentService
. 它不是
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. 任何
startActivity()
, startService()
, bindService()
或sendBroadcast()
调用都不会开始执行其工作,直到您从正在进行的任何主应用程序线程回调中返回。
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. 由于在回调中将主应用程序线程占用1ms以上的时间是不明智的做法,因此此行为不会影响很多开发人员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.