2012-04-17 23 views

回答

2

如果您與AsynTask合作,那麼您可以在onPostExecute()中顯示它。 http://www.mysamplecode.com/2011/09/android-asynctask-httpclient-with.html

AlertDialog alertDialog = new AlertDialog.Builder(
         AlertDialogActivity.this).create(); 


    // Setting Dialog Title 
    alertDialog.setTitle("Alert Dialog"); 

    // Setting Dialog Message 
    alertDialog.setMessage("Welcome to AndroidHive.info"); 

    // Setting OK Button 
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
      // Write your code here to execute after dialog closed 
      Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show(); 
      } 
    }); 

    // Showing Alert Message 
    alertDialog.show(); 

更多的幫助與警報看到http://www.androidhive.info/2011/09/how-to-show-alert-dialog-in-android/

0

如果使用AsyncTask,那麼你可以編寫代碼顯示消息(也許吐司),在onPostExecute()

0

它不是不可能連接後臺線程與UI.With處理程序的幫助下,你可以發送messages.By檢查消息,你可以顯示alert.i認爲這段代碼將幫助你。

Thread animator = new Thread() { 
     public void run() { 
      int i = 0; 
      try { 
       sleep(4000); 
       while (i < 4) { 
        sleep(50); 
        handler.sendMessage(handler.obtainMessage(i)); 
        i++; 
       } 
      } catch (Exception e) { 

      } 
     } 
    }; 
    animator.start(); 
    handler = new Handler() { 

     @Override 
     public void handleMessage(Message msg) { 
      super.handleMessage(msg); 
      if (msg.what == 0) { 
       animatedimage.setImageResource(R.drawable.sub1); 
      } else if (msg.what == 1) { 
       animatedimage.setImageResource(R.drawable.sub2); 
      } else if (msg.what == 2) { 
       animatedimage.setImageResource(R.drawable.sub3); 
      } else if (msg.what == 3) { 
       animatedimage.setImageResource(R.drawable.sub4); 
      } 
     } 

    }; 

如果您使用的是Assync tasy你能做到在

onPostExecute() 
0

啓動一個異步線程。異步線程爲您提供了三種方法:OnPreExecute(),doInBackground()和onPostExecute()。

在UI線程上調用第一個和最後一個方法,所以一旦在doInBackground中執行了操作,就會出現如下操作:

相關問題