我对AsyncTask
请参阅此AsyncTask
以及
public class ListView extends ListActivity {
ArrayList<HashMap<String, String>> questionList;
final String TAG_RESULTS = "results";
final String TAG_QUESTION_SUBJECT = "Subject";
final String TAG_QUESTION_NUMANSWERS = "NumAnswers";
final String TAG_QUESTION = "question";
final String TAG_QUESTION_CONTENT = "Content";
final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
final String TAG_ANSWERS = "Answers";
final String TAG_ANSWER = "Answer";
final String TAG_ANSWERS_CONTENT = "content";
final String TAG_QUERY = "query";
final String TAG_COUNT = "count";
ProgressDialog pDialog;
LoadAllData mTask;
JSONArray question = null;
android.widget.ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.listview);
questionList = new ArrayList<HashMap<String, String>>();
mTask = new LoadAllData();
mTask.execute();
}
@Override
protected void onListItemClick(android.widget.ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
HashMap<String, String> item = questionList.get(pos);
Intent i = new Intent(ListView.this, SingleListItem.class);
i.putExtra(TAG_QUESTION_SUBJECT, item.get(TAG_QUESTION_SUBJECT));
i.putExtra(TAG_QUESTION_CONTENT, item.get(TAG_QUESTION_CONTENT));
i.putExtra(TAG_QUESTION_CHOSENANSWER, item.get(TAG_QUESTION_CHOSENANSWER));
startActivity(i);
}
@Override
public void onBackPressed() {
if (mTask != null && mTask.getStatus() != AsyncTask.Status.FINISHED)
{
mTask.cancel(true);
}
super.onBackPressed();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
if (mTask != null && mTask.getStatus() != AsyncTask.Status.FINISHED)
{
mTask.cancel(true);
}
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
if (pDialog != null)
{
if(pDialog.isShowing())
{
pDialog.dismiss();
}
super.onPause();
}
}
class LoadAllData extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ListView.this);
pDialog.setMessage("Loading Data. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
pDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
mTask.cancel(true);
finish();
}
});
try {
Intent in = getIntent();
String searchTerm = in.getStringExtra("TAG_SEARCH");
String query = URLEncoder.encode(searchTerm, "utf-8");
String URL = "http://example.com";
JSONParsser jParser = new JSONParsser();
JSONObject json = jParser.readJSONFeed(URL);
try {
JSONArray questions = json.getJSONObject("all").getJSONArray("questions");
for(int i = 0; i < questions.length(); i++) {
JSONObject question = questions.getJSONObject(i);
String Subject = question.getString(TAG_QUESTION_SUBJECT);
String ChosenAnswer = question.getString(TAG_QUESTION_CHOSENANSWER);
String Content = question.getString(TAG_QUESTION_CONTENT);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_QUESTION_SUBJECT, Subject);
map.put(TAG_QUESTION_CONTENT, Content);
map.put(TAG_QUESTION_CHOSENANSWER, ChosenAnswer);
questionList.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_URL) {
if(file_URL!=null && file_URL.equals("0")) {
pDialog.dismiss();
Toast.makeText(ListView.this, "No data found", Toast.LENGTH_SHORT).show();
finish();
}else{
if (pDialog != null && pDialog.isShowing()) pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(getBaseContext(), questionList,
R.layout.listelements,
new String[] { TAG_QUESTION_SUBJECT }, new int[] {
R.id.Subject,});
setListAdapter(adapter);
}}}
现在我想问的问题是,如果我从当前活动中取出AsyncTask
并将其放在另一个文件中,它是否会更快地执行我的doInBackGround
方法。我问这个是因为当我的AsyncTask
正在执行时,它需要相当长的时间才能完成,而且在我的logcat中,当09-24 20:12:55.928: I/Choreographer(824): Skipped 195 frames! The application may be doing too much work on its main thread.
正在执行时,此消息AsyncTask
会填满我的整个logcat。我只是想知道如果我移动它会有所作为。
答案 0 :(得分:2)
我只是想知道如果我移动它会有所作为。
不,它不会。
此外,doInBackground()
的行为不是你困难的根源。这是其他的东西,也许是你的onPostExecute()
。错误是非常明确的问题(“可能在其主线程上做了太多工作”),doInBackground()
在另一个线程上工作。
答案 1 :(得分:2)
您确定此代码块属于doInbackground方法吗?
pDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
mTask.cancel(true);
finish();
});
错误清楚地表明您正在做一些不适合UI线程的额外工作(在上面的代码中不清楚)
答案 2 :(得分:0)
关于更新的代码 - 问题仍然不明确。
我建议你查看后台代码并查找涉及活动或UI的任何内容 - 在后台线程上发出UI线程请求可能会导致一些模糊的问题。 setOnCancelListener()是一个。 getIntent()是另一个。评论他们,看看会发生什么。
然后开始分解它。注释掉一些代码,将它们从等式中移除,看看它是否会发生任何变化。
此时需要进行一些搜索,所以越小越好,错误的代码就越好。最终你会弄清楚是什么导致了这个问题,从中可以找出原因。