私はUACでプロジェクト用の小さなアプリケーションを作成していますが、onClickイベント後にProgressDialogを表示するのに問題があります。Android:ボタンonClickが3秒後に反応します
基本的に私のアクティビティでonClickが呼び出されたとき、私はAsyncTaskを開始し、onPreExecuteではprogressDialogを開始し、onPostExecuteで再びそれを閉じます。
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<Recipe> recipes = null;
try {
ParsedElement x = new ParsedElement(v.getContext());
Object[] params = new Object[2];
params[0] = txtSearch.getText().toString();
params[1] = v;
recipes = (ArrayList<Recipe>) x.execute(params).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Intent switchToDisplayPage = new Intent(SearchActivity.this, UIGenerator.class);
Bundle params = new Bundle();
params.putParcelableArrayList("recipes", recipes);
switchToDisplayPage.putExtras(params);
startActivity(switchToDisplayPage);
}
});
問題は約3秒間、「何もしない」ボタンをクリックすると発生します。私はすでにAndroid Monitorを見て、ボタンをクリックするとCPU使用率が最大40%であることを発見しました。
ProgressDialog progressDialog;
Context v;
public ParsedElement(Context v) {
this.v = v;
}
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(v);
progressDialog.setTitle("Downloading");
progressDialog.setMessage("Recipes are loading...");
progressDialog.setIndeterminate(false);
progressDialog.show();
}
@Override
protected Object doInBackground(Object[] params) {
String searchString = (String) params[0];
View v = (View) params[1];
ArrayList<Recipe> recipes = new ArrayList<>();
try {
System.out.println(System.currentTimeMillis());
ArrayList<URL> single = new ArrayList<>();
single.add(new URL(Constants.baseSearchURL + searchString));
Document mainDocument = (Downloaders.getDocumentFromURL(single)).get(0);
Elements mainElements = mainDocument.select("div.img_wrap img[src]");
ArrayList<URL> urls = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
urls.add(new URL(Constants.baseSearchURL + searchString +
Constants.pageString + Integer.toString(i)));
}
ArrayList<Document> tempDocument = Downloaders.getDocumentFromURL(urls);
for (Document docu : tempDocument) {
Elements tempElement = docu.select("div.img_wrap img[src]");
for (Element a : tempElement) {
mainElements.add(a);
}
}
for (Element link : mainElements) {
recipes.add(new Recipe(link.attr("title"), link.attr("abs:src")));
}
} catch (IOException ex) {
ex.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return recipes;
}
@Override
protected void onPostExecute(Object aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
}
この場合、ProgressDialogは表示されません。私は基本的に "私の主なスレッドであまりにも多くのことをやっている"と述べたスレッドをたくさん読んでいます。明らかに私は重いものをやっていません。
ありがとうございます!