ActivityのSEARCHBUTTONがクリックされた後、AsyncTaskクラスのJSONファイルをダウンロードするAndroidアプリケーションを作成しています。そして、データのダウンロード中にアクティビティに進行状況ダイアログを表示したいと思います。しかし、私のAVDとデバイスでは、実際のアクションは私の考えとは異なります。アップロードしたこの動画(約1分)をご覧ください。https://www.youtube.com/watch?v=qKyVGZ1FxIo&feature=youtube_gdata_player
このビデオでは、検索ボタンがUIをしばらくクリックした後、ProgressDialogが非常に短時間表示され、Toastが表示されます。
クリックの直後にProgressDialogを表示し、Toastが表示される直前に非表示にしたい。
アクティビティのClickListner:
@Override
public void onClick(View v) {
DownloadJSONFile task = new DownloadJSONFile(MainActivity.this);
task.execute("1", "class", lectureName, teacherName, date, period, "");
}
AsyncTask:
import org.json.JSONArray;
import com.fc2.wiki.ap2012.komari.SearchResultActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.Toast;
public class DownloadJSONFile extends AsyncTask<String, Integer, JSONArray> {
private ProgressDialog dialog;
Context context;
JSONArray jsonArray;
boolean makeNewActivityFlag = true;
public DownloadJSONFile(Context context, boolean flag) {
this.context = context;
this.makeNewActivityFlag = flag;
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(this.context);
dialog.setTitle("こまり"); //it's Japanese
dialog.setMessage("通信中…");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
}
doInBackground:
@Override
protected JSONArray doInBackground(String... keywords) {
try {
//for test
while(!this.dialog.isShowing()){
Thread.sleep(500);
}
//I will load JSON file here
/*
JSONArray jsonArray = UTaisakuDatabaseUtil.getInstance()
.getJSONSearchResult(version, method, searchedLectureName,
searchedTeacherName, date, period, assessment);
*/
//for test
Thread.sleep(5000);
} catch (NumberFormatException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
} catch (InterruptedException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
return jsonArray;
}
onPostExecute:
@Override
protected void onPostExecute(JSONArray jsonArray) {
this.jsonArray = jsonArray;
if (this.dialog.isShowing())
dialog.dismiss();
if (this.makeNewActivityFlag) {
// Intentを作成してSearchResultActivityへ
if (jsonArray != null) {
Intent intent = new Intent(this.context,
SearchResultActivity.class);
intent.putExtra("LECTURE_NAME", searchedLectureName);
intent.putExtra("TEACHER_NAME", searchedTeacherName);
intent.putExtra("YOUBI", searchedDateString);
intent.putExtra("PERIOD", searchedPeriodString);
intent.putExtra("JSONARRAY", jsonArray.toString());
context.startActivity(intent);
} else
//in this test case,jsonArray is always null.So this Toast is always called
Toast.makeText(context, "ファイルが取得できませんでした。", Toast.LENGTH_SHORT)
.show();
}
}
}
何か案は?