2013-08-05 5 views
0

내 앱이 처음으로 59kb의 txt 파일 인 사용자 계약서를 보여주기 시작했을 때. Text View에 파일을 읽고 추가하는 데 시간이 걸리기 때문에 비동기 작업에서 텍스트 파일을 추가하고 진행하는 동안 진행률 막대를 표시하기로 결정했지만 파일이 텍스트보기에 추가 될 때까지 진행률 막대가 고정됩니다. 그런 일이 발생하면 앱이 응답하기까지 시간이 걸리고 때로는 ANR이 발생합니다. 여기 내 비동기 작업 코드 :AsyncTask 동안 UI가 멈춤

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 메서드의 텍스트보기에 추가했습니다. 파일을 읽는 방식을 변경해야합니까? 또는 다른 것입니다. 미리 감사드립니다.

+0

어떻게 'AsyncTask'를 호출합니까? – codeMagic

+0

변호사세요? 사용자 동의를 위해 59K ?? 나는 사용자 동의가 오랫동안 당신을 보호 할 것이라고 생각하지 않습니다. 왜냐하면 사용자가 읽고 동의하는 것이 불가능하기 때문입니다. 중요한 내용을 담은 매우 짧은 탄환 목록을 작성하고 외부 웹 페이지에 대한 완전한 합의에 대한 링크를 제공해야합니다. – auval

+0

나는 알고있다. 그러나 상사는 클라이언트와의 이전의 문제 때문에 그 동의를 원한다. 저를 믿으십시오, 나는 그것을 더 짧게 만들 것을 제안했다, 운이 없다. – wnieves19

답변

3

먼저 onProgressUpdate을 사용하여 진행률 표시 줄을 업데이트 할 수 있습니다.

ANR을 피하기 위해 Html.fromHtml은 UI 스레드를 차단하는 비싼 호출이라고 생각합니다. doInBackground 메서드에서 Html 값을 반환하고 UI 차단을 피할 수 있습니다.

example of use onProgressUpdate to update the progress bar

+0

안녕하세요. html.fromHtml을 사용했습니다. html 파일 이었기 때문입니다.하지만 HTML 파일은 작기 때문에 txt 파일로 옮깁니다. 제거했지만 행운이 없다. – wnieves19

+0

어쩌면 textview 대신 webview를 사용할 수 있으며, html 페이지를 저장하기 위해 assets 폴더를 사용할 수도있다. 이 질문에서와 같이 : http://stackoverflow.com/questions/8737488/problems-loading-html-asset-into-webview/8737547#8737547 –

+0

재미있군, 나는 그것을 시도하고 다시 돌아올거야 – wnieves19

관련 문제