【发布时间】:2025-12-03 17:40:01
【问题描述】:
我的 Android 应用程序中有一个 AsyncTask<Task, Void, Boolean> 线程。当该线程完成执行时,我想通过Toast.makeText() 显示消息。为此,我在 if 以及 doInBackground 方法的 else 内部添加了 Toask.makeText()。线程已成功完成其执行,但未出现 toast 的消息。那么可能是什么问题呢?
代码:
@Override
protected Boolean doInBackground(Task... arg0) {
try {
Task task = arg0[0];
QueryBuilder qb = new QueryBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(qb.buildContactsSaveURL());
StringEntity params =new StringEntity(qb.createTask(task));
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
if(response.getStatusLine().getStatusCode()<205)
{
/*this is the message inside if*/
Toast.makeText(context, "inside -IF", Toast.LENGTH_SHORT).show();
return true;
}
else
{
/*this is the message inside else*/
Toast.makeText(context, "inside -ELSE", Toast.LENGTH_SHORT).show();
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
【问题讨论】:
标签: android multithreading android-studio