2012-04-17 24 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中执行了操作,就会出现如下操作:

相关问题