2013-07-26 97 views
0

我知道這裏有類似的問題,但我仍然沒有找到答案。問題是這個長時間操作的進度對話框不會顯示出來,但仍然在處理完成。我認爲上下文有問題,但不知道如何解決這個問題。AsyncTask中的進度對話框不顯示

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."); 
    } 
} 

}

+0

嘗試this..http://stackoverflow.com/questions/17585759/cant-dismiss-progressdialog-after-the-asynctask-complete/17585792#17585792 – TheFlash

回答

1

您正在通過調用doFirstRun()啓動asyctask後開始活動; ,因此你沒有看到創建的進度對話框。如果刪除/發表意見startActivity部分如下,它應該工作:


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

如果你仍然想反正啓動活動,那麼你應該開始後的的AsyncTask。

2

創建一個構造函數,如:

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

,並在對話框中使用此_context上下文。

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(); 
     } 
+0

仍無法正常工作 –

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.");} 

    } 
}