首页 > 解决方案 > 异步任务进度对话框需要很长时间才能完成 android 中的简单任务

问题描述

当我调用这个异步任务时,它需要很长时间,但是任务很短,当我直接输入代码时它工作正常,但是当我使用异步任务时它需要时间,我已经从这个站点找到了解决方案,
进度对话框异步任务比预期更长的时间, 但它不能正常工作,

public class Towing_TaskCollectAmountCash extends AsyncTask<Double,Void,Void>{

private ProgressDialog progressDialog;
Activity activity;


public Towing_TaskCollectAmountCash(Activity activity,Double collectedAmount) {
    this.activity = activity;
    this.collectedAmount=collectedAmount;
    progressDialog=new ProgressDialog(activity);
}

//progress Dialog Showing
@Override
protected void onPreExecute() {
    progressDialog.setTitle("Please Wait...");
    progressDialog.setCancelable(false);
    progressDialog.setIndeterminate ( true ) ;
    progressDialog.show();
}
@Override
protected Void doInBackground(Double... amount){
     referenceOfDriverWallets=FirebaseDatabase.getInstance().getReference().child("Wallet").child("Drivers").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
     referenceOfDriverWallets.addListenerForSingleValueEvent(new ValueEventListener({
        @Override
        public void onDataChange(DataSnapshot dataSnapshot){
          if(dataSnapshot.child("TotalRideCollection").exists()){
              totalRideCollection= Double.valueOf(dataSnapshot.child("TotalRideCollection").getValue().toString());
            }
        }

         @Override
        public void onCancelled(DatabaseError databaseError) {
        }
});}

//Progress Dialog dismiss
@Override
protected void onPostExecute(Void aVoid) {
    progressDialog.dismiss();
}

}

这是主要的 MainActivity 调用代码

Towing_TaskCollectAmountCash obj=new 
Towing_TaskCollectAmountCash(CollectPayment.this,collectedAmount);
obj.execute();

请帮助我,在此先感谢

标签: androidandroid-asynctaskprogressdialog

解决方案


尝试这样的事情:

ProgressDialog

private ProgressDialog progressDialog = null;

现在只是一个简单的Firebase调用方法:

private Void getData(){
    if(progressDialog == null){
        progressDialog = new ProgressDialog(context);
        progressDialog.setTitle("Please Wait...");
        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate ( true ) ;
    }
    progressDialog.show();

     referenceOfDriverWallets=FirebaseDatabase.getInstance().getReference().child("Wallet").child("Drivers").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
     referenceOfDriverWallets.addListenerForSingleValueEvent(new ValueEventListener({
        @Override
        public void onDataChange(DataSnapshot dataSnapshot){
          if(dataSnapshot.child("TotalRideCollection").exists()){
              totalRideCollection= Double.valueOf(dataSnapshot.child("TotalRideCollection").getValue().toString());

              progressDialog.dismiss();
            }
        }

         @Override
        public void onCancelled(DatabaseError databaseError) {
            progressDialog.dismiss();
        }
    });
}

如果您从上下文中调用它,Activity则可能是:

YourActivity.this

您似乎没有使用该collectedAmount参数。



笔记:

我没有检查查询 Firebase 的代码。我只是假设它已经起作用了。


推荐阅读