0

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

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

4 回答 4

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();
        }
于 2013-07-26T09:28:59.630 回答
2

创建一个构造函数,例如:

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

并将此 _context 用于对话框中的上下文。

于 2013-07-26T09:28:32.373 回答
1

通过调用 doFirstRun(); 启动 asyctask 后,您正在启动一个活动;,因此您看不到创建的进度对话框。如果您按如下方式删除/注释 startActivity 部分,它应该可以工作:


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

如果您仍然想启动该活动,那么您应该在那之后启动 asynctask。

于 2013-07-26T09:37:06.457 回答
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.");}

    }
 }
于 2013-07-26T09:46:06.113 回答