当我的应用程序第一次启动时,它应该显示用户协议,这是一个 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 方法中的文本视图中。我应该改变读取文件的方式吗?或者是别的什么。提前致谢