Android 无法在AsyncTask中显示ProgressDialog
我正在尝试在AsyncTask()中显示ProgressDialog。在我的代码中,我是 使用Android 无法在AsyncTask中显示ProgressDialog,android,android-fragments,android-asynctask,android-handler,Android,Android Fragments,Android Asynctask,Android Handler,我正在尝试在AsyncTask()中显示ProgressDialog。在我的代码中,我是 使用handler.postdayed运行AsyncTask。 如果没有handler.postdelayed,它将显示progressdialog final Handler handler = new Handler(); handler.postDelayed(new Runnable() { public void run() { new AsyncTask<Voi
handler.postdayed
运行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();
}
});
final Handler=new Handler();
handler.postDelayed(新的Runnable(){
公开募捐{
新建异步任务(){
@凌驾
受保护的void onPreExecute(){
progress=ProgressDialog.show(getActivity(),“正在更新配置文件”,“请稍候”,true);
progress.show();
试一试{
File File=新文件(Environment.getExternalStorageDirectory().getPath(),“/myProfileData.txt”);
if(file.exists()){
试一试{
profileDatabase.updateProfileFromDB();
profile=新的StructConfigParameters();
isUseAutoConfigProfileChecked=true;
profileDatabase.updateProfileFromDB();
File myFile=新文件(Environment.getExternalStorageDirectory().getPath()+“/myProfileData.txt”);
FileInputStream fIn=新的FileInputStream(myFile);
BufferedReader myReader=新BufferedReader(
新的InputStreamReader(fIn));
字符串aDataRow=“”;
字符串aBuffer=“”;
而((aDataRow=myReader.readLine())!=null){
aBuffer+=aDataRow+“\n”;
}
myReader.close();
}捕获(例外e){
System.out.println(“updateProfileFromDB 133中的异常”+e);
}
}
}捕获(例外e){
System.out.println(“updateProfileFromDB 22”+e中的异常);
}
}
@凌驾
受保护的Void doInBackground(Void…参数){
返回null;
}
@凌驾
受保护的void onPostExecute(void res){
进步。解散();
}
}.execute();
}
});
当您使用调用web服务时,应遵循以下步骤
由于您已经在使用AsyncTask
,所以不需要在handler中调用它,您可以在AsyncTask
方法中显示进度
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.
}
}
公共类myAsyncTask扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//在此处显示进度对话框
}
@凌驾
受保护的Void doInBackground(Void…参数){
//此方法应该只有调用web服务的代码
//或与UI无关的背景工作
//此方法中应避免UI操作
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
//您可以使用此方法执行UI工作
//在所有操作结束时关闭进度对话框。
}
}
而你的情况应该是这样的
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();
newasynctask(){
@凌驾
受保护的void onPreExecute(){
progress=ProgressDialog.show(getActivity(),“正在更新配置文件”,“请稍候”,true);
progress.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
File File=新文件(Environment.getExternalStorageDirectory().getPath(),“/myProfileData.txt”);
if(file.exists()){
试一试{
profileDatabase.updateProfileFromDB();
profile=新的StructConfigParameters();
isUseAutoConfigProfileChecked=true;
profileDatabase.updateProfileFromDB();
File myFile=新文件(Environment.getExternalStorageDirectory().getPath()+“/myProfileData.txt”);
FileInputStream fIn=新的FileInputStream(myFile);
BufferedReader myReader=新的BufferedReader(新的InputStreamReader(fIn));
字符串aDataRow=“”;
字符串aBuffer=“”;
而((aDataRow=myReader.readLine())!=null){
aBuffer+=aDataRow+“\n”;
}
myReader.close();
}捕获(例外e){
System.out.println(“updateProfileFromDB 133中的异常”+e);
}
}
}捕获(例外e){
System.out.println(“updateProfileFromDB 22”+e中的异常);
}
返回null;
}
@凌驾
受保护的void onPostExecute(void res){
进步。解散();
}
}.execute();
类装入数据扩展异步任务
{
私有进程对话框;
@凌驾
受保护的void onPreExecute()
{
prgDialog=newprogressdialog(DataActivity.this);
setMessage(“请稍候…”);
prgDialog.setCancelable(假);
prgDialog.show();
super.onPreExecute();
}
@凌驾
受保护的Void doInBackground(Void…参数)
{
//做一些工作,比如加载数据
返回null;
}
@凌驾
受保护的void onPostExecute(void结果)
{
super.onPostExecute(结果);
prgDialog.cancel();
//加载完成后做一些工作
}
}
更改
progress = ProgressDialog.show(getActivity(), "Updating Profile",
"Please Wait", true);
progress.show();
对此
progress = ProgressDialog.show(YourActivityName.this, "Updating Profile",
"Please Wait", true);
progress.show();
将您的代码从pre方法移动到background maethod..不需要在此1中创建处理程序。ProgressDialog=新建ProgressDialog(上下文);或2。它将使用handler完成
getActivity()
有什么问题?只有代码片段不是好答案。添加一些描述。
progress = ProgressDialog.show(getActivity(), "Updating Profile",
"Please Wait", true);
progress.show();
progress = ProgressDialog.show(YourActivityName.this, "Updating Profile",
"Please Wait", true);
progress.show();