2014-03-02 48 views
0
import android.widget.Toast; 
public class Security extends MainActivity { 

    Socket sock,sock1; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.security); 

     new Thread(new Runnable() 
     { 
      public void run() 
      { 
      try{ 
       sock=new Socket(ip,port1); 
       BufferedReader br=new BufferedReader(new InputStreamReader(sock.getInputStream())); 
       String a=br.readLine(); 
       Thread.sleep(2500); 
       Toast.makeText(getApplicationContext(),"Please Enter the result of operation for no "+a,Toast.LENGTH_LONG).show(); 


      } 
      catch(UnknownHostException e) 
      { 
       e.printStackTrace(); 
      } 
      catch (IOException e) { 
       e.printStackTrace(); 
       } 
      catch(InterruptedException e) 
      { 
       e.printStackTrace(); 
      } 

      } 

     }).start(); 

    } 


} 

安全啓動時無法查看烤麪包。我不知道爲什麼?它就像是在oncliklistener中應該是智慧的。無法看到裏面的烤麪包

+0

只從上面的'MainThread'的Toast消息使用'ASyncTask'而不是原始'Thread'。這給你訪問'onPostExecute'這正是你在做什麼 – Blundell

回答

1
runOnUiThread(new Runnable() { 
    public void run() { 
     Toast.makeText(getApplicationContext(),"Please Enter the result of operation for no "+a,Toast.LENGTH_LONG).show(); 
    } 
}); 
+0

好吧,但我也需要運行套接字code.and它只適用於原始Thread.then該怎麼做 – user3283519

+0

你可以在原始線程中調用runOnUiThread()。 –

1

您只能在主UI線程中顯示吐司。

如果您需要從後臺線程在UI線程上執行操作,請查看runOnUiThread()

+0

好吧,但我也需要運行套接字code.and它只適用於原始Thread.then該怎麼辦 – user3283519