2013-01-24 108 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按钮在警报对话框中,否则循环在警报对话框上不停地运行。 –

相关问题