0
doInBackground()工作正常.. Looper.loop()后的代码将不起作用。在Looper.Loop()之后不执行打印并且不执行onPostExceute()。我需要等到方法1,2,3执行。如果不使用Looper.prepare(),则会在方法1中发生异常。Looper.loop()之后不执行onPostExecute()
@Override
protected Void doInBackground(Void... params) {
try {
if (Looper.myLooper() == null)
Looper.prepare();
method1();
method2();
method3();
Looper.loop();
Log.d(TAG,"after loop()");
} else {
method4(); //inside asyn task
}
Log.d(TAG,"doInBackground end");
}catch (Exception e) {
Log.d(TAG,"doInBackground exception "+e);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
try {
Log.d(TAG, "onPostExecute");
//............
}catch (Exception e){
Log.d(TAG,"onPostExecute end");
}
}
@Override
protected void onPreExecute() {
//.....
}
如果我没有使用Looper.loop()和Looper.prepare(),是否会发生任何异常。如果我没有使用Looper.prepare(),我会得到异常。 –
如果您在不需要读取消息队列时未通过使用prepare来获得异常,那么您正在做其他事情。当你应该在UI线程上创建一个Handler时,可能会在AsyncTask上创建一个Handler。 –