AsyncTask中的进度对话框不显示

时间:2013-07-26 09:24:25

标签: android android-asynctask progressdialog

我知道这有类似的问题,但我还是没有找到答案。问题是这个长时间操作的进度对话框不会显示,但仍在进行处理。我认为上下文存在问题,但不知道如何解决这个问题。

public class MainActivity extends Activity {

Utilities uti = new Utilities();
SharedPreferences prefs = null;
private ContactServiceActivity contactService;
ProgressDialog mProgressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    contactService = new ContactServiceActivity(getApplicationContext());

    doFirstRun();

    Intent i = new Intent(getBaseContext(), ContactListActivity.class);
    startActivity(i);
}

private void doFirstRun() {
    SharedPreferences settings = getSharedPreferences("pl.stxnext.stxcontactsync", MODE_PRIVATE);
    if (settings.getBoolean("isFirstRun", true)) {

        new firstRunTask().execute();

        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("isFirstRun", false);
        editor.commit();
    }
}

private class firstRunTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(MainActivity.this);
        mProgressDialog.setTitle("Trwa synchronizacja danych");
        mProgressDialog.setMessage("Może to zająć chwilę, proszę czekać.");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        contactService.getAssetsAtFirstRun();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        mProgressDialog.dismiss();
        uti.showToast(getBaseContext(), "Zapisano kontakty.");
    }
}

}

4 个答案:

答案 0 :(得分:2)

创建一个构造函数,如:

Context _context;
public firstRunTask(Context context)
{
    _context=context;
}

并在对话框中使用此_context作为上下文。

答案 1 :(得分:2)

Try like this 

 private class LongOperation extends AsyncTask<String, Void, String> {

         @Override
         protected String doInBackground(String... params) {


            // Do something

            return "Executed";

         }      

         @Override
         protected void onPostExecute(String result) {
           if(mProgressDialog.isShowing()){
             mProgressDialog.dismiss();
         }


         }

         @Override
         protected void onPreExecute() {
         ShowLoading();
         }

         @Override
         protected void onProgressUpdate(Void... values) {

         }

     }
     private void ShowLoading(){
            mProgressDialog = new ProgressDialog(this);
            //mProgressDialog.setMessage("Loading Please wait ....");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
        }

答案 2 :(得分:1)

通过调用doFirstRun()启动asyctask后,您正在启动一个活动; ,因此您没有看到创建的progressdialog。如果你删除/注释startActivity部分如下,它应该工作:


doFirstRun();
// comment the following
//Intent i = new Intent(getBaseContext(), ContactListActivity.class);
//startActivity(i);

如果你仍然想要开始那个活动,那么你应该在那之后开始asynctask。

答案 3 :(得分:1)

这样做 -

private class firstRunTask extends AsyncTask<Void, Void, Void> {
ProgressDialog mProgressDialog;
@Override
protected void onPreExecute() {
    super.onPreExecute();
    mProgressDialog=ProgressDialog.show(MainActivity.this, "Trwa synchronizacja danych", "Może to zająć chwilę, proszę czekać.");

}

@Override
protected Void doInBackground(Void... params) {
    contactService.getAssetsAtFirstRun();
    return null;
}

@Override
protected void onPostExecute(Void result) {

   if(mProgressDialog != null)
{
if(mProgressDialog.isShowing())
  {

       mProgressDialog.dismiss();
        uti.showToast(getBaseContext(), "Zapisano kontakty.");}

    }
 }