gpt4 book ai didi

android - 如果我将 AsyncTask 移动到不同的文件,它会更快吗?

转载 作者:搜寻专家 更新时间:2023-11-01 08:02:33 25 4
gpt4 key购买 nike

我只是有一些关于 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 从当前 Activity 中取出并放在另一个文件中,它会执行我的 doInBackGround 方法更快。我问这个是因为当我的 AsyncTask 正在执行时,它需要相当长的时间才能完成,而且在我的 logcat 中还有这条消息 09-24 20:12:55.928: I/Choreographer(824) : 跳过 195 帧!应用程序可能在其主线程上做了太多工作。 在执行 AsyncTask 时填满了我的整个 logcat。我只是想知道如果我移动它会有所不同。

最佳答案

I just want to know will It make a difference if I move it.

不,不会。

此外,doInBackground() 的行为并不是您遇到困难的根源。它是其他东西,也许是您的 onPostExecute()。该错误非常明确地说明了您的问题(“可能在其主线程上做了太多工作”),并且 doInBackground() 在另一个线程上完成了它的工作。

关于android - 如果我将 AsyncTask 移动到不同的文件,它会更快吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18993567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com