Socket发送信息给远程服务器,Toast显示(线程内外都可以)

来源:互联网 发布:linux基础命令 编辑:程序博客网 时间:2024/04/29 14:22
package com.example.socket;import java.io.BufferedWriter;import java.io.OutputStreamWriter;import java.net.Socket;import android.os.Bundle;import android.os.Looper;import android.os.StrictMode;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class socket extends Activity {long uiId;@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_socket);                StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());//安卓4.0以上必须加这个才能在主线程里访问网络,即使我都创建了线程。//EditText editText1 =(EditText)findViewById(R.id.editText1);//editText1.setText("192.168.1.102");uiId=Thread.currentThread().getId();//获取主线程IDThread thread = new Thread(runnable1);    thread.start();    }        @Override    public boolean onCreateOptionsMenu(Menu menu) {        //getMenuInflater().inflate(R.menu.activity_socket, menu);        return true;    }        Socket client = null;    OutputStreamWriter outputStreamWriter;    BufferedWriter bufferedWriter;        public boolean clientStart(Socket tempsocket,String site,int port)    {    boolean state=true;    try{    tempsocket = new Socket(site,port);        outputStreamWriter = new OutputStreamWriter(tempsocket.getOutputStream());bufferedWriter = new BufferedWriter(outputStreamWriter);        }catch (Exception e){        state=false;            e.printStackTrace();        }    return state;    }    public void show(String str){    if(Thread.currentThread().getId()!=uiId)    {    Looper.prepare();    Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();    Looper.loop();        //非主线程中显示Toast必须写这个才不强退    }else Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();    }    Runnable runnable1 = new Runnable(){@Override    public void run(){    EditText editText1 =(EditText)findViewById(R.id.editText1);    EditText editText2 =(EditText)findViewById(R.id.editText2);    String site=editText1.getText().toString();    int port=Integer.parseInt(editText2.getText().toString());    boolean state=clientStart(client,site,port);                if(state)show("Succeed");            else show("Failed");                }    };    public boolean check(Socket socket){    try {            socket.sendUrgentData(0xFF);          } catch (Exception e) {            e.printStackTrace();             return false;        }    return true;    }    public boolean sendMsg(Socket tempsocket,String msg){    {    boolean state=true;    try{    bufferedWriter.write(msg);    bufferedWriter.flush();        }catch(Exception e){        state=false;            e.printStackTrace();        }    return state;    }    }        Runnable runnable2 = new Runnable(){@Override    public void run(){EditText editText3 =(EditText)findViewById(R.id.editText3);boolean state = sendMsg(client,editText3.getText().toString());            if(state)show("Succeed");            else show("Failed");    }    };    public void connect(View v)    {        show("Connecting");    Thread thread = new Thread(runnable1);    thread.start();    }    public void send(View v)    {    Thread thread = new Thread(runnable2);    thread.start();    }    String cmdstr = "";    Runnable runnable3 = new Runnable(){@Override    public void run(){boolean state = sendMsg(client,cmdstr);            if(state)show("Succeed");            else show("Failed");    }    };    public void cmd(String str)    {    cmdstr=str;    Thread thread = new Thread(runnable3);    thread.start();    }    public void cmd1(View v)    {    cmd("1");    }    public void cmd2(View v)    {    cmd("2");    }    public void cmd3(View v)    {    cmd("3");    }    public void cmd4(View v)    {    cmd("4");    }    public void cmd5(View v)    {    cmd("5");    }}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <EditText        android:id="@+id/editText1"        android:layout_width="240dp"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:ems="10"        android:text="btbu.pw" >        <requestFocus />    </EditText>    <EditText        android:id="@+id/editText2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignRight="@+id/editText1"        android:layout_below="@+id/editText1"        android:ems="10"        android:text="8085" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@+id/editText2"        android:layout_alignParentRight="true"        android:layout_toRightOf="@+id/editText1"        android:onClick="connect"        android:text="Connect" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/editText2"        android:layout_alignBottom="@+id/editText2"        android:layout_alignLeft="@+id/button1"        android:layout_alignParentRight="true"        android:onClick="send"        android:text="Send" />    <EditText        android:id="@+id/editText3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentRight="true"        android:layout_below="@+id/button2"        android:ems="10"        android:text="1" />    <Button        android:id="@+id/button_1"        android:layout_width="180dp"        android:layout_height="135dp"        android:layout_below="@+id/editText3"        android:onClick="cmd1"        android:text="1" />    <Button        android:id="@+id/button_2"        android:layout_width="180dp"        android:layout_height="135dp"        android:layout_alignParentRight="true"        android:layout_below="@+id/editText3"        android:onClick="cmd2"        android:text="2" />    <Button        android:id="@+id/button_3"        android:layout_width="180dp"        android:layout_height="135dp"        android:layout_below="@+id/button_1"        android:onClick="cmd3"        android:text="3" />    <Button        android:id="@+id/button_4"        android:layout_width="180dp"        android:layout_height="135dp"        android:layout_below="@+id/button_2"        android:layout_alignParentRight="true"        android:onClick="cmd4"        android:text="4" />    <Button        android:id="@+id/button_5"        android:layout_width="180dp"        android:layout_height="135dp"        android:layout_below="@+id/button_3"        android:onClick="cmd5"        android:text="5" /></RelativeLayout>

<uses-permission android:name="android.permission.INTERNET"/>


0 0