2013-08-07 74 views
0

根據工作流程的過程,我在一個活動中創建了3個視圖。 即viewA-> viewB-> viewC,然後在viewC上,當我做HTTP-POST(使用AsyncTask)時,顯示進度對話框。 我嘗試2種方式來顯示進度對話框:使用runOnUiThreadprogressDialog未在一個活動中顯示多個視圖

  1. ()來顯示progressDialog,也沒有顯示。
  2. 在AsyncTask中編寫顯示progressDialog代碼。在onExExcute()中進行進度對話框顯示並在onPostExecute()中關閉,它在doinbackground任務之後顯示,並且onPostExecute()也不會執行。

任何人都可以幫忙嗎?

感謝

SAM

這裏是主要的活動代碼:

public void setA(){ 
    setContentView(R.layout.a_fm); 
    Button aNextBtn=(Button)findViewById(R.id.aNextBtn); 
    aNextBtn.setOnClickListener(this); 
} 
public void setB(){ 
    setContentView(R.layout.b_fm); 
    Button bNextBtn=(Button)findViewById(R.id.bNextBtn); 
    bNextBtn.setOnClickListener(this); 
} 
public void setC(){ 
    setContentView(R.layout.c_fm); 
    Button cNextBtn=(Button)findViewById(R.id.cNextBtn); 
    cNextBtn.setOnClickListener(this); 
} 
    public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.aNextBtn: 
       setB(); 
       break; 
    case R.id.bNextBtn: 
       setB(); 
       break; 
    case R.id.cNextBtn: 
       postmsg(); 
       break; 
     } 
    } 
    public void postmsg(final Info info) 
{ 
    postDialog=new ProgressDialog(AssistFm.this); 
    postDialog.setMessage(getString(R.string.alert_sendmsg_sending)); 
    postDialog.show(); 
    this.runOnUiThread(new Runnable() {@Override 
     public void run() { 
      // TODO Auto-generated method stub 
      send_online=sendlogtowebservice(info); 
      SEND_COUNT++; 
      if (send_online) 
      { 
       postDialog.dismiss(); 
       AlertDialog.Builder builder = new Builder(A_activity.this); 
       builder.setMessage(getString(R.string.alert_sendmsg_success)); 
       builder.setTitle(getString(R.string.sendmsg_title)); 
       builder.setPositiveButton(getString(R.string.button_OK), new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss();   
         setA(); 
         SEND_COUNT=0; 
        } 
       }); 
       builder.create().show();  
      } 
      else 
      { 
       postDialog.dismiss(); 
       if (SEND_COUNT<SEND_COUNT_MAX) 
       { 
        AlertDialog.Builder builder = new Builder(A_activity.this); 
        builder.setMessage(getString(R.string.alert_sendmsg_retry)); 
        builder.setTitle(getString(R.string.sendmsg_title)); 
        builder.setNegativeButton(getString(R.string.button_Cancel), new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.dismiss();  
         } 
        }); 

        builder.setPositiveButton(getString(R.string.button_OK), new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.dismiss(); 
          postmsg(info); 
          SEND_COUNT++; 
         } 
        }); 
        builder.create().show(); 
       } 
       else 
       { 
        AlertDialog.Builder builder = new Builder(AssistFm.this); 
        builder.setMessage(getString(R.string.alert_sendmsg_error)); 
        builder.setTitle(getString(R.string.sendmsg_title)); 

        builder.setPositiveButton(getString(R.string.button_OK), new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.dismiss(); 
          setA(); 
         } 
        }); 
        builder.create().show(); 
       } 
      } 
     } 
    }); 

} 
private boolean sendlogtowebservice(Info info) { 
    boolean isTrue = false; 
    int result_code = 0; 
    Object []param = new Object[3]; 
    HttpResponse response = null;  
    String result_str; 
    try { 
     String sURL=url; 
     HttpClient client = new DefaultHttpClient();   
     ArrayList<BasicNameValuePair> paierList = new ArrayList<BasicNameValuePair>(); 
     paierList.add(new BasicNameValuePair("person_firstname", info.person_firstname)); 
     paierList.add(new BasicNameValuePair("person_lastname", info.person_lastname)); 
     paierList.add(new BasicNameValuePair("person_mobile", info.person_mobile)); 

     param[0] = sURL; 
     param[1] = paierList; 
     param[2] = client; 
     AsyncTask<Object, Object, HttpResponse> res = new HttpReqTask().execute(param); 
     response = (HttpResponse) res.get(); 
     result_code=response.getStatusLine().getStatusCode(); 
     result_str = EntityUtils.toString(response.getEntity()); 
     if (result_str.equals("00")) 
     { 
      isTrue = true; 
     } 
    } 
    catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    catch (Exception e) 
    { 
     Log.e("HttpAPI.callHttpPost()", "Error", e); 
    } 
    return isTrue; 

}; 

這裏是HTTP-POST的AsyncTask代碼:

import java.util.ArrayList; 

import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.protocol.HTTP; 

import android.os.AsyncTask; 


public class HttpReqTask extends AsyncTask<Object, Object, HttpResponse>{ 

    @Override 
    protected HttpResponse doInBackground(Object... params){ 
    String url = (String)params[0]; 
    ArrayList<NameValuePair> paierList = (ArrayList<NameValuePair>)params[1]; 
    HttpClient httpclient = (HttpClient)params[2]; 

    HttpPost request = new HttpPost(url); 
    HttpResponse response = null; 
    try { 
     request.setEntity(new UrlEncodedFormEntity(paierList, HTTP.UTF_8)); 
     response = httpclient.execute(request); 
    } catch (Exception e) { 
     throw new RuntimeException(e); 
    } 
    return response; 
} 

} 

回答

0

沒有人知道這是如何發生的?

我將AsyncTask更改爲下方,當進度對話框試圖關閉進度對話框時,進度對話框只顯示一個沖刷。

public class HttpReqTask extends AsyncTask<Object, Object, HttpResponse>{ 
private Context mCtx; 
private ProgressDialog progressDialog; 

public HttpReqTask(Context context){  
    myLog.i("HttpReqTask constructor, before show dialog!"); 
      progressDialog = new ProgressDialog(mCtx); 
    progressDialog.setMessage("Your progress dialog message..."); 
      progressDialog.show(); 
      myLog.i("HttpReqTask constructor finish, start show dialog!"); 
} 

@Override 
protected HttpResponse doInBackground(Object... params){ 
    myLog.i("do in background execute"); 
    String url = (String)params[0]; 
    ArrayList<NameValuePair> paierList = (ArrayList<NameValuePair>)params[1]; 
    HttpClient httpclient = (HttpClient)params[2]; 
    String s=null; 
    HttpPost request = new HttpPost(url); 
    HttpResponse response = null; 
    try { 
     request.setEntity(new UrlEncodedFormEntity(paierList, HTTP.UTF_8)); 
     response = httpclient.execute(request); 
     //add for longer process time. 
     for (int i=0;i<10000000;i++) 
     { 
      s="a"; 
     } 
    } catch (Exception e) { 
     throw new RuntimeException(e); 
    } 
    myLog.i("finish in background execute"); 
    return response; 
} 

@Override  
protected void onPostExecute(HttpResponse result) {  
    myLog.i("HttpReqTask onPostExecute(), before dismiss dialog!"); 
    if (progressDialog!=null) progressDialog.dismiss();  
    myLog.i("HttpReqTask onPostExecute(), after dismiss dialog!"); 
}  

}

0

似乎沒有人知道答案...... 根據我的研究,我可以通過創建一個新的線程所示的progessDialog,但是AlertDialog在新線程,不能現在顯示...

public void postmsg(final OnlineHistoryInfo info) 
{ 
    dialog=new ProgressDialog(mActivity.this); 
    dialog.setMessage(getString(R.string.alert_sendmsg_sending)); 
    dialog.setCancelable(false); 
    dialog.show(); 
    myLog.i("dialog show!");   
    new Thread(){ 
     @Override 
     public void run() 
     { 
      try 
      { 
       myLog.i("new thread: run!"); 
       // TODO Auto-generated method stub 
       send=sendlogtowebservice(info); 
       //send_online=true; 
       SEND_COUNT++; 

      } 
      finally 
      { 
       dialog.dismiss(); 
       if (send) 
       { 
        myLog.i("send:true!"); 
        new DataRule(AssistFm.this).saveOnlineHistory(info); 
        AlertDialog.Builder builder = new Builder(mActivity.this); 
        builder.setMessage(getString(R.string.alert_sendmsg_success)); 
        builder.setTitle(getString(R.string.sendmsg_title)); 
        builder.setPositiveButton(getString(R.string.button_OK), new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          setA(); 
          SEND_COUNT=0; 
         } 
        }); 
        builder.create().show();  
       } 
       else 
       { 
        myLog.i("send:false!"); 
        if (SEND_COUNT<SEND_COUNT_MAX) 
        { 
         AlertDialog.Builder builder = new Builder(mActivity.this); 
         builder.setMessage(getString(R.string.alert_sendmsg_retry)); 
         builder.setTitle(getString(R.string.sendmsg_title)); 
         builder.setNegativeButton(getString(R.string.button_Cancel), new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           dialog.dismiss();  
          } 
         }); 

         builder.setPositiveButton(getString(R.string.button_OK), new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           postmsg(info); 
           SEND_COUNT++; 
          } 
         }); 
         builder.create().show(); 
        } 
        else 
        { 
         AlertDialog.Builder builder = new Builder(mActivity.this); 
         builder.setMessage(getString(R.string.alert_sendmsg_error)); 
         builder.setTitle(getString(R.string.sendmsg_title)); 

         builder.setPositiveButton(getString(R.string.button_OK), new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           setA(); 
          } 
         }); 
         builder.create().show(); 
        } 
       } 
      } 
     } 
    }.start(); 



}//postmsg() finish