2013-01-24 49 views
0

我想顯示警報對話框取決於屬性,當用戶點擊確定按鈕時,再次調用函數來獲取運行過程中的更新值。如何在運行線程中顯示警報對話框?

我有下面的代碼: -

  importingProgress = ProgressDialog.show(context, getString(R.string.progressNewsListTitle), 
       getString(R.string.progressProjectListMessage), true); 

     new Thread(new Runnable() { 

      public void run() { 
       try 
       { 
        app.SetOtherTaskRunning(true); 
        Ib_clients client = db.Ib_clients_GetById(app.GetCustomerId());    
        try { 
         LogManager.WriteToFile("---------------- Getting News from Webservice :- "+DateFormat.getDateTimeInstance().format(new Date())+"----------------"); 
         CommonFuctions.CreateXml(context,h,client,db,app.GetBookMonth(),app.GetBookQuater(),app.GetBookYear(),Constants.News,app.GetWebServiceLastSyncDate(Constants.ServiceType.NEWS.toString()),Constants.ServiceType.NEWS,null,null,null,null,null); 
         Return reponse=null; 
         do 
         {       
          reponse=CommonFuctions.SendingRequest(context, handler, db); 
          if(reponse.type.compareTo("warning")==0) 
          { 


           h.post(new Runnable() { 

            public void run() { 
             AlertDialog.Builder alert = new AlertDialog.Builder(context);  
             alert.setTitle(context.getString(R.string.information)); 
             alert.setMessage("dsgdgd"); 
             alert.setPositiveButton(context.getString(R.string.logoutDialogOk), new DialogInterface.OnClickListener() { 
              public void onClick(DialogInterface dialog, int whichButton) {  


              } 
             });   
             alert.show(); 
            } 
           });  


          } 

         }while(reponse.type.compareTo("warning")==0); 


        } catch (IOException e) { 

         e.printStackTrace(); 
        } 



       } 
       catch (Exception e) { 
        //Log.d(Constants.TAG, e.getMessage()); 
        e.printStackTrace(); 
       } 

       if (importingProgress != null) 
       { 
        importingProgress.dismiss(); 
        importingProgress=null; 
       }  


      } 
     }).start(); 

如果響應類型是警告,然後顯示給用戶的消息,當點擊OK按鈕,然後再次調用CommonFuctions.SendingRequest(背景下,處理程序,DB),以獲得更新的值。直到我們得到的響應類型是警告,我們需要向用戶顯示警告對話框,並再次調用CommonFuctions.SendingRequest(context,handler,db)。

返回分類: -

public class Return { 

public String type; 
public String msg; 
public boolean isSuccess; 
public int client_id; // for getting clientid from server 
public int booking_id; // for getting bookingid form server 
    } 
+0

把你alertdialog代碼獨立於主線程。嘗試在runonUIThread上運行它。 – GrIsHu

+0

你可以通過使用處理程序和它的post方法來做到這一點,因爲它必須在UI線程上運行 – techieWings

回答

0

你將不得不使用的處理程序,以顯示AlertDialog因爲UI只能由主線程處理。

另一種方法是使用的AsyncTask的多,然後使用的AsyncTask的onPostExcecute()顯示AlertDialog

請隨意問任何進一步的懷疑。

0

嘗試運行您的對話框,如下面runonUIthread:

runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          AlertDialog.Builder alert = new AlertDialog.Builder(context);  
            alert.setTitle(context.getString(R.string.information)); 
            alert.setMessage("dsgdgd"); 
            alert.setPositiveButton(context.getString(R.string.logoutDialogOk), new DialogInterface.OnClickListener() { 
             public void onClick(DialogInterface dialog, int whichButton) {  
             } 
            });   
            alert.show(); 
         } 
       }); 
+0

我也可以使用handler.post()方法來顯示警報對話框,但我想停止循環,而用戶沒有點擊ok按鈕在警報對話框中,否則循環在警報對話框上不停地運行。 –

相關問題