1

When my app starts for the first time its supposed to show the user Agreement which is a txt file of 59kb. Since it takes some time for it to read and append the file to a Text View I decided to to that in an Async task and show a progress bar while it does, however the progress bar freezes until the file is appended to the text view, and when that happens it take some time for the app to respond, sometimes causing a ANR. here's my Async Task code:

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

As you can see I append the file to the text view in the postExecute method. Should I change the way I'm reading the file? or is it something else. Thanks in advance

wnieves19
  • 550
  • 4
  • 16
  • How are you calling the `AsyncTask`? – codeMagic Aug 05 '13 at 15:29
  • Are you a lawyer? 59K for user agreement?? I don't think user agreement that long will protect you, because it's impossible for a user to to read and agree. You should write a very short list of bullets with the really important stuff, and provide a link to the full agreement on an external web page. – Amir Uval Aug 05 '13 at 15:31
  • I know, but the boss want that agreement because of previous problems with the client. Believe me I suggested to make it shorter, no luck though. – wnieves19 Aug 05 '13 at 15:38
  • @codeMagic I'm using new DownloadFilesTask().execute(); to call the AsyncTask – wnieves19 Aug 05 '13 at 15:43

1 Answers1

3

First you could use the onProgressUpdate for update the progress bar.

For avoid your ANR I suspect that the Html.fromHtml is a expensive call that is blocking your UI thread. You could return the Html value in the doInBackground method and avoid the UI blocking.

example of use onProgressUpdate to update the progress bar

Jordi Coscolla
  • 1,066
  • 6
  • 8
  • Hi I had the Html.fromHtml because it was an html file before but then I move it to a txt file because it was smaller. I removed it but no luck – wnieves19 Aug 05 '13 at 15:47
  • Maybe you can use a webview instead of a textview, also you can use the assets folder for store the html page. like in this question: http://stackoverflow.com/questions/8737488/problems-loading-html-asset-into-webview/8737547#8737547 – Jordi Coscolla Aug 05 '13 at 15:55
  • hmm interesting, I'll try that and get back to you – wnieves19 Aug 05 '13 at 15:59
  • Thanks man It worked great....It's loding super fast I dont even need a progress dialog...Thanks again – wnieves19 Aug 05 '13 at 16:23