android - 异步任务进度对话框需要很长时间才能完成 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();
请帮助我,在此先感谢
解决方案
尝试这样的事情:
为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 的代码。我只是假设它已经起作用了。
推荐阅读
- google-assistant-sdk - 我可以在没有树莓派的情况下在 python 程序中使用谷歌助手库吗?
- xamarin.forms - Xamarin.Forms 中的 MasterDetailPage 已更改?
- javascript - Javascript async await with Promise all 并获取返回的 [object promise]
- arrays - 本地阵列上的内联汇编
- java - Android InputStream 无法从蓝牙 HC-05 接收整个消息
- angular - 以角度形式从子组件中获取父级的属性
- c# - Xamarin UI 测试无法点击按钮
- ios - 我可以从后台队列中快速调用静态方法吗?
- usb - FTDI 实现标准 CDC 协议?
- javascript - Firebase functions algolia onWrite