我试图通过从 Activity 调用的 Asynctask 访问 REMOTE android 服务。
我遇到了一个问题,我认为这是由重用线程引起的。我在 AsyncTask 中使用了 Lopper,当它获得一个线程时,它已经被先前对远程服务的调用分配了,我收到一个错误,即每个线程只允许一个 Looper。我不这么认为,因为我试图在错误的线程上访问 UI。
我可以用一些非常简单的代码来模拟这个问题。我在活动上创建了一个按钮,只需按 6 次。希望有人可以建议我如何解决这个问题。我试过检查 looper 不为空,但这只会让我遇到另一个问题,所以我想尝试解决这个问题。
这是我的活动
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonClick(View view)
{
MyAsychTask myAsychTask = new MyAsychTask() ;
myAsychTask.execute() ;
}}
异步任务
public class MyAsychTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
Log.wtf("Thead details", Thread.currentThread().getName() + " : " + Thread.currentThread().getState().toString() + " : Looper def: " + ((Looper.myLooper()==null) ? "none" : "looper") ) ;
Looper.prepare();
Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
// in real app process incoming messages here then quit looper
Log.wtf("executing message", " ") ;
this.getLooper().quit() ;
}
};
new simulateREMOTEservice(myHandler) ;
Looper.loop() ;
Log.wtf("exited looper", " ") ;
return null ;
}}
class simulateREMOTEservice{
public simulateREMOTEservice(Handler myHandler)
{
Log.wtf("simulating REMOTE service", " ") ;
myHandler.sendEmptyMessage(0) ;
}}
日志文件是
10-18 19:00:12.420: A/Thead details(3081): AsyncTask #1 : RUNNABLE : Looper def: none
10-18 19:00:12.420: A/simulating REMOTE service(3081):
10-18 19:00:12.420: A/executing message(3081):
10-18 19:00:12.420: A/exited looper(3081):
10-18 19:00:14.280: A/Thead details(3081): AsyncTask #2 : RUNNABLE : Looper def: none
10-18 19:00:14.290: A/simulating REMOTE service(3081):
10-18 19:00:14.290: A/executing message(3081):
10-18 19:00:14.300: A/exited looper(3081):
10-18 19:00:16.140: A/Thead details(3081): AsyncTask #3 : RUNNABLE : Looper def: none
10-18 19:00:16.150: A/simulating REMOTE service(3081):
10-18 19:00:16.150: A/executing message(3081):
10-18 19:00:16.150: A/exited looper(3081):
10-18 19:00:17.830: A/Thead details(3081): AsyncTask #4 : RUNNABLE : Looper def: none
10-18 19:00:17.840: A/simulating REMOTE service(3081):
10-18 19:00:17.840: A/executing message(3081):
10-18 19:00:17.840: A/exited looper(3081):
10-18 19:00:19.480: A/Thead details(3081): AsyncTask #5 : RUNNABLE : Looper def: none
10-18 19:00:19.480: A/simulating REMOTE service(3081):
10-18 19:00:19.490: A/executing message(3081):
10-18 19:00:19.490: A/exited looper(3081):
10-18 19:00:21.810: A/Thead details(3081): AsyncTask #1 : RUNNABLE : Looper def: looper
10-18 19:00:21.820: E/AndroidRuntime(3081): FATAL EXCEPTION: AsyncTask #1
10-18 19:00:21.820: E/AndroidRuntime(3081): java.lang.RuntimeException: An error occured while executing doInBackground()
10-18 19:00:21.820: E/AndroidRuntime(3081): at android.os.AsyncTask$3.done(AsyncTask.java:266)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.lang.Thread.run(Thread.java:1020)
10-18 19:00:21.820: E/AndroidRuntime(3081): Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
10-18 19:00:21.820: E/AndroidRuntime(3081): at android.os.Looper.prepare(Looper.java:76)
10-18 19:00:21.820: E/AndroidRuntime(3081): at com.example.testlooper.MyAsychTask.doInBackground(MyAsychTask.java:15)
10-18 19:00:21.820: E/AndroidRuntime(3081): at com.example.testlooper.MyAsychTask.doInBackground(MyAsychTask.java:1)
10-18 19:00:21.820: E/AndroidRuntime(3081): at android.os.AsyncTask$2.call(AsyncTask.java:252)
10-18 19:00:21.820: E/AndroidRuntime(3081): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-18 19:00:21.820: E/AndroidRuntime(3081): ... 4 more