【问题标题】:UI Freezing during AsyncTaskAsyncTask 期间 UI 冻结
【发布时间】:2013-08-05 15:25:30
【问题描述】:

当我的应用程序第一次启动时,它应该显示用户协议,这是一个 59kb 的 txt 文件。由于它需要一些时间来读取文件并将其附加到文本视图中,因此我决定在异步任务中执行此操作并在执行时显示进度条,但是进度条会冻结,直到文件附加到文本视图中,当这种情况发生时,应用程序需要一些时间来响应,有时会导致 ANR。这是我的异步任务代码:

  public class DownloadFilesTask extends AsyncTask<String, Integer, String> {

    AlertDialog alertDialog = null;
    Button getstarted, cancel;  
        TextView text2;  
        AlertDialog.Builder builder;
        LayoutInflater inflater = (LayoutInflater) Profile.this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.agreement, (ViewGroup) findViewById(R.id.layout_root));

protected void onPreExecute(){
     progressDialog = ProgressDialog.show(Profile.this, "", "Loading.....", true);
        getstarted=(Button) layout.findViewById(R.id.get_started);
        cancel=(Button) layout.findViewById(R.id.cancel);

        cancel.setVisibility(View.GONE);
        text2 = (TextView) layout.findViewById(R.id.ag);

        builder = new Builder(Profile.this);
        builder.setView(layout);
        alertDialog = builder.create();
        alertDialog.setCancelable(false);
         alertDialog.setTitle(getString(R.string.terms));   
    }

@Override
protected String doInBackground(String... arg0) {
     StringBuilder sb = new StringBuilder();
try {
        AssetManager am = getAssets();
        InputStream in = am.open("EULA.txt");
         BufferedReader reader = new BufferedReader(new InputStreamReader(in));

            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }

            in.close();


    } catch(Exception e){

           System.out.println("FILE NOT FOUND : "+ e.getMessage());
    }

       return sb.toString();
        }
 // This is called when doInBackground() is finished
  protected void onPostExecute(String result) {
      progressDialog.dismiss();
      alertDialog.show();
        text2.setText(Html.fromHtml(result));  

       }

       // This is called each time you call publishProgress()
}

如您所见,我在 postExecute 方法中将文件附加到文本视图中。我应该改变读取文件的方式吗?或者是别的什么。提前致谢

【问题讨论】:

  • 你怎么打电话给AsyncTask
  • 你是律师吗? 59K用户协议??我认为用户协议不会长久保护您,因为用户不可能阅读并同意。您应该写一个非常简短的项目符号列表,其中包含真正重要的内容,并在外部网页上提供完整协议的链接。
  • 我知道,但是老板想要那个协议,因为之前客户有问题。相信我,我建议缩短它,但没有运气。
  • @codeMagic 我正在使用 new DownloadFilesTask().execute();调用 AsyncTask

标签: android android-asynctask stringbuilder


【解决方案1】:

首先您可以使用onProgressUpdate 来更新进度条。

为了避免您的 ANR,我怀疑 Html.fromHtml 是一个阻塞您的 UI 线程的昂贵调用。您可以在 doInBackground 方法中返回 Html 值并避免 UI 阻塞。

example of use onProgressUpdate to update the progress bar

【讨论】:

  • 嗨,我有 Html.fromHtml 因为它之前是一个 html 文件,但后来我将它移动到一个 txt 文件,因为它更小。我删除了它,但没有运气
  • 也许您可以使用 webview 代替 textview,也可以使用 assets 文件夹来存储 html 页面。喜欢这个问题:stackoverflow.com/questions/8737488/…
  • 嗯,很有趣,我会试试然后回复你
  • 谢谢伙计,它工作得很好......它的速度超快我什至不需要进度对话框......再次感谢
猜你喜欢
  • 1970-01-01
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多