I'm making an android application which downloads JSON file in the AsyncTask class after SEARCH BUTTON in Activity is clicked. And I want to display Progress Dialog on the Activity while downloading data. But on my AVD and device, actual action is different from my thought. See this video (about 1 minute) I uploaded. https://www.youtube.com/watch?v=qKyVGZ1FxIo&feature=youtube_gdata_player
In this video,after SEARCH BUTTON clicked the UI freeze for a while, and then ProgressDialog is shown for a very short moment and Toast is shown.
I want ProgressDialog to be shown right after click, and be dismissed right before Toast is shown.
ClickListner in Activity:
@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();
}
}
}
Any ideas?