3

我正在制作一个 android 应用程序,该应用程序在单击 Activity 中的 SEARCH BUTTON 后下载 AsyncTask 类中的 JSON 文件。我想在下载数据时在 Activity 上显示进度对话框。但在我的 AVD 和设备上,实际操作与我的想法不同。看我上传的这个视频(大约 1 分钟)。https://www.youtube.com/watch?v=qKyVGZ1FxIo&feature=youtube_gdata_player

在此视频中,在 SEARCH BUTTON 单击 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, "");
}

异步任务:

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();
}

做背景:

@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;
}

onPost执行:

@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();
        }
    }
}

有任何想法吗?

4

2 回答 2

3

我找到了解决我自己问题的方法!我ContextAsyncTask. 我使用Activityclass 而不是Context!All 一切正常。

public class DownloadJSONFile extends AsyncTask<String, Integer, JSONArray> {

    private ProgressDialog dialog;

    //this is the cause of bug
    //Context context;        

    //this is the answer
    Activity activity;

    JSONArray jsonArray;
    boolean makeNewActivityFlag = true;

    public DownloadJSONFile(Activity activity, boolean flag) {
        this.activity = activity;
        this.makeNewActivityFlag = flag;
    }
…
…
}

但是我无法解释为什么它在使用类时可以正常工作而在使用Activity类时不能正常工作Context。有人可以解释一下吗?如果您有任何想法,请在此处发表评论。

于 2012-11-02T02:04:56.783 回答
0

感谢您发布解决方案,我在采用来自 zxing/BarCodeReader 的代码时遇到了类似的问题。从构造函数参数中删除上下文后,睡眠被正确中断并且不再停止 UI 线程:

  AutoFocusManager(Context context, Camera camera) {
    this.camera = camera;
    taskExec = new AsyncTaskExecManager().build();
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    String currentFocusMode = camera.getParameters().getFocusMode();
    useAutoFocus =

        FOCUS_MODES_CALLING_AF.contains(currentFocusMode);
    Log.i(TAG, "Current focus mode '" + currentFocusMode + "'; use auto focus? " + useAutoFocus);
    start();
  }

       protected Object doInBackground(Object... voids) {
      try {
        Thread.sleep(AUTO_FOCUS_INTERVAL_MS);
      } catch (InterruptedException e) {
        // continue
      }
      synchronized (AutoFocusManager.this) {
        if (active) {
          start();
        }
      }
      return null;
    }
于 2013-03-07T10:10:55.683 回答