我AsyncTask
正在运行Activity
。在AsyncTask
doInBackground(Object[])
方法中,我执行网络操作,使用HTTP POST
方法将一些数据发送到服务器,接收响应,然后在其onPostExecute(Object)
中它将响应发送到Service
,然后所有作业都从Service
执行。问题是:在Activity
完成之前,我无法关闭AsyncTask
。 Activity
只会冻结,直到AsyncTask
完成。有时,如果AsyncTask
需要更多时间来完成,屏幕会变为黑色,并保持这样状态直到AsyncTask
完成。这是为什么?怎么避免这个?我尝试在AsyncTask
cancel(boolean)
方法中呼叫Activity
onStop()
,但它仍然执行相同的操作。
编辑:我的AsyncTask
代码:
private static class PostCommentTask extends AsyncTask<String, Void, String> {
Activity activity;
int status;
Toast indicationMsg;
public PostCommentTask(Context context) {
activity = (Activity) context;
}
private boolean isNetworkAvailable() {
Log.i("ilog", "isNetworkAvailable() called");
ConnectivityManager connManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected() && activeNetworkInfo.isAvailable();
}
protected String doInBackground(String... param) {
if (!isNetworkAvailable()) {
return "";
}
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(param[7]);
String responseBody = "error in PostComment.doInBackground()";
try {
List<NameValuePair> params = new ArrayList<NameValuePair>(7);
params.add(new BasicNameValuePair("somevar", param[0]));
params.add(new BasicNameValuePair("somevar", param[1]));
params.add(new BasicNameValuePair("somevar", param[2]));
params.add(new BasicNameValuePair("somevar", param[3]));
params.add(new BasicNameValuePair("somevar", param[4]));
params.add(new BasicNameValuePair("somevar", param[5]));
params.add(new BasicNameValuePair("somevar", param[6]));
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = httpClient.execute(httpPost);
status = response.getStatusLine().getStatusCode();
Log.i("ilog", "response status: " + status);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
responseBody = EntityUtils.toString(responseEntity);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("ilog", "Response: " + responseBody);
return responseBody;
}
public void onPostExecute(String responseBody) {
String success = "{\"status\":\"OK\"}";
if (responseBody.equals(success) && status == 200){
indicationMsg = Toast.makeText(activity, "Success!!!", Toast.LENGTH_LONG);
CommentsFragment.CommentsHandler commentsHandler = new CommentsFragment.CommentsHandler();
Messenger messenger = new Messenger(commentsHandler);
Intent serviceIntent = new Intent(activity, WindowService.class);
serviceIntent.putExtra("messenger", messenger);
serviceIntent.putExtra("somevar", somevar);
serviceIntent.putExtra("somevar", activity.getIntent().getStringExtra("somevar"));
activity.startService(serviceIntent);
//
}
else
indicationMsg = Toast.makeText(activity, "Failed...", Toast.LENGTH_LONG);
indicationMsg.show();
}
}
CommentsHandler
class是一个处理Service
响应的类。这就是我在Activity
和Service
之间进行沟通的方式。
我使用以下代码启动AsyncTask
:
public static class CommentsFragment extends Fragment {
/** ... **/
@Override
public void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/** ... **/
Button queryButton = (Button) rootView.findViewById(R.id.query_button);
queryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/** ... **/
new PostCommentTask(activity).execute(somevar, somevar, somevar, somevar, somevar, somevar, somevar, somevar);
/** ... **/
}
});
/** ... **/
}
/** ... **/
}
答案 0 :(得分:0)
我已经解决了这个问题。 Activity
冻结是因为Activity
我将从AsyncTask
方法中使用onResume()
,因此它必须等到第一个任务来自关闭Activity
在执行自己的任务之前完成,这导致了UI的冻结。现在我使用executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, Object[]
能够并行运行多个任务,而不是逐个运行(逐个)。