Android进度对话框不会从asynctask

时间:2016-09-03 14:31:37

标签: android android-asynctask progressdialog

我在PreferenceFragment中有一个进度对话框,并且不会更新其进度:

进度对话框在以下侦听器

中初始化
 @Override
public boolean onPreferenceClick(Preference preference) {
     if (getString(R.string.sync).equalsIgnoreCase(preference.getKey())) {
        String saveTitle = getString(R.string.crop__wait);
        String saveDescription = getString(R.string.sync_is_running);
        progressDialog = ProgressDialog.show(getActivity(), saveTitle, saveDescription, false, true);
        progressDialog.setProgress(10);
        progressDialog.setMax(100);
        progressDialog.show();
        new SyncAsyncTask(this).execute();
        return true;
    } else  if (getString(R.string.changePassword).equalsIgnoreCase(preference.getKey())) {
        ChangePasswordDialog.createDialog(getString(R.string.changePassword)).show(getActivity().getSupportFragmentManager(), "YES_NO_DIALOG");
        return true;
    }

    return true;
}

在这里我正在更新进度对话框,但是在ui上没有任何进展。仅显示ProgressDialog的以下部分:标题,描述和微调器。

@Override
    public void updateProgress(int progress) {
        total += progress;
        progressDialog.setProgress(total);
        progressDialog.incrementProgressBy(total);

    }

奇怪的是,这段代码使得进度可见:

    progressDialog = new ProgressDialog (getActivity());
    progressDialog.setIndeterminate(false);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.setCancelable(true);
    progressDialog.setTitle(saveTitle);
    progressDialog.setMessage(saveDescription);
    progressDialog.setMax(100);
    progressDialog.show();

我也发布了异步任务代码,但我确信没问题:

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import java.lang.ref.WeakReference;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class SyncAsyncTask extends AsyncTask<Void, Integer, Boolean> {

    private WeakReference<ISyncronizable> ref = null;
    private final int totalCount = 21;

    public SyncAsyncTask(ISyncronizable syncronizable) {
        ref = new WeakReference<>(syncronizable);
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        if (ref.get().getContext() != null) {
            return synchronizeDB(ref.get().getContext());
        }

        return false;
    }

    @Override
    protected void onPostExecute(Boolean result) {      
        if (ref.get().getContext() != null) {
            ref.get().finishedSyncronization(result);
            ref.clear();
        }
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        ref.get().updateProgress(values[0]);
    }

    public boolean synchronizeDB(final Context context) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, 2000);
        cal.set(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);

        SoapSyncService ss = SoapSyncService.getSoapSyncService();
        boolean successfullSync = true;

        SystemSettingBean systemSettingsBean = SQLMceHelper.getDb(context).getLatestSyncDate();

        Date date = cal.getTime();
        String serverDateAndTime = ss.getServerDateAndTime();
        if (systemSettingsBean == null || systemSettingsBean.getDateValue() == null) {
            systemSettingsBean = new SystemSettingBean();
            systemSettingsBean.setDateValue(serverDateAndTime);

            date = cal.getTime();
        } else {
            //........
            if (numberOfChanges == 0) {
                return true;
            }
        }
        publishProgress(1);
 //........
        publishProgress(5);
        //........
        publishProgress(4);
         //........

        publishProgress(4);
         //........
        publishProgress(4);
         //........

        publishProgress(4);
         //........

        publishProgress(4);
         //........

        publishProgress(4);
         //........

        publishProgress(5);
        //........

        publishProgress(5);
        //........

        publishProgress(5);
        //........

//       //........
        publishProgress(5);
         //........


        publishProgress(5);
        //........

        publishProgress(5);
         //........
        publishProgress(5);
        //........

        publishProgress(5);
        //........

        publishProgress(5);
         //........

        publishProgress(5);
         //........

        publishProgress(5);
         //........

        publishProgress(5);
        //........

        publishProgress(5);
        //........

        if (successfullSync) {
             //........
        }
        publishProgress(5);
        return successfullSync;
    }
}

界面代码:

import android.content.Context;

public interface ISyncronizable {

    public Context getContext();
    public void updateProgress(int progress);
    public void finishedSyncronization(boolean isSuccessfullyFinished);
}

奇怪的是,使用此代码可以看到进度:

progressDialog = new ProgressDialog (getActivity());
        progressDialog.setIndeterminate(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setCancelable(true);
        progressDialog.setTitle(saveTitle);
        progressDialog.setMessage(saveDescription);
        progressDialog.setMax(100);
        progressDialog.show();

1 个答案:

答案 0 :(得分:0)

问题在于您传递给publishProgress()的值。

您已将ProgressBar的最大值设置为100,因此,如果您想要查看进度的任何可见更改,请改用10,20,30等等。

所以在你的doInBackground()中,你会做这样的事情:

if (successfullSync) {
    publishProgress(100);
    //........
}