我试图在AsyncTask()中显示ProgressDialog。在我的代码我
使用handler.postdelayed
运行AsyncTask
。
没有handler.postdelayed它显示progressdialog。
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(getActivity(), "Updating Profile", "Please Wait", true);
progress.show();
try {
File file = new File(Environment.getExternalStorageDirectory().getPath(), "/myProfileData.txt");
if (file.exists()) {
try {
profileDatabase.updateProfileFromDB();
profile = new StructConfigParameters();
isUseAutoConfigProfileChecked = true;
profileDatabase.updateProfileFromDB();
File myFile = new File(Environment.getExternalStorageDirectory().getPath() + "/myProfileData.txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
myReader.close();
} catch (Exception e) {
System.out.println("Exception In updateProfileFromDB 133 " + e);
}
}
} catch (Exception e) {
System.out.println("Exception In updateProfileFromDB 22 " + e);
}
}
@Override
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void res) {
progress.dismiss();
}
}.execute();
}
});
答案 0 :(得分:1)
当您使用AsyncTask
来呼叫网络服务时,您应该遵循这些内容。
由于您已经在使用AsyncTask
,因此无需在处理程序中调用它,您可以在AsyncTask
的{{3}}方法中显示进度。
public class myAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Show your progress dialog here
}
@Override
protected Void doInBackground(Void... params) {
// this method should only have code to call web service
// or background work which should not be related to UI
// UI operations should avoid in this method
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// you can do UI work in this method
// Dismiss your progress dialog at the end of all operaions.
}
}
虽然你的情况应该是
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(getActivity(), "Updating Profile", "Please Wait", true);
progress.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
File file = new File(Environment.getExternalStorageDirectory().getPath(), "/myProfileData.txt");
if (file.exists()) {
try {
profileDatabase.updateProfileFromDB();
profile = new StructConfigParameters();
isUseAutoConfigProfileChecked = true;
profileDatabase.updateProfileFromDB();
File myFile = new File(Environment.getExternalStorageDirectory().getPath() + "/myProfileData.txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
myReader.close();
} catch (Exception e) {
System.out.println("Exception In updateProfileFromDB 133 " + e);
}
}
} catch (Exception e) {
System.out.println("Exception In updateProfileFromDB 22 " + e);
}
return null;
}
@Override
protected void onPostExecute(Void res) {
progress.dismiss();
}
}.execute();
答案 1 :(得分:0)
Try the followoing
ProgressDialog progressDialog = new ProgressDialog(mainActivity);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(false);
progressDialog.setMessage("Loading Please Wait");
progressDialog.show();
答案 2 :(得分:0)
class LoadData extends AsyncTask<Void, Void, Void>
{
private ProgressDialog prgDialog;
@Override
protected void onPreExecute()
{
prgDialog = new ProgressDialog(DataActivity.this);
prgDialog.setMessage("Please wait...");
prgDialog.setCancelable(false);
prgDialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params)
{
// do some work like loading data
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
prgDialog.cancel();
// do some work when loading finish
}
}
答案 3 :(得分:0)
变化
progress = ProgressDialog.show(getActivity(), "Updating Profile",
"Please Wait", true);
progress.show();
到这个
progress = ProgressDialog.show(YourActivityName.this, "Updating Profile",
"Please Wait", true);
progress.show();