偶然希望两个安卓手机能交互一下,所以写了两个安卓之间交互的程序,好吧我承认是从java socket套用过来的,但是,还是有点改变。。。。。。。
两个手机,一个做server,一个做client
首先是第一个server的:
主类:
public class MainActivity extends Activity { TextView qwe = null; static Toast tt = null; PrintWriter out ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); qwe = (TextView)findViewById(R.id.qwe); /*静态的吐司,方便收到信息后其他线程调用*/ tt = Toast.makeText(MainActivity.this, "qe", Toast.LENGTH_SHORT); OpenConnection(); } public void OpenConnection(){ try { ServerSocket s = new ServerSocket(6666); //System.out.println("服务器端------监听中....."); Socket socket = s.accept(); //System.out.println("开始:" + socket); GetMessage gm = new GetMessage(socket,this); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); Thread gt = new Thread(gm); gt.start(); } catch (IOException e1) { e1.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0,0,0,"PRINT"); return true; } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { out.println("asdasd");//当点击的时候,就会发送出去字符串 out.flush(); return super.onMenuItemSelected(featureId, item); } }
发信的线程类:
public class SendMessage implements Runnable { PrintWriter out; BufferedReader is; public SendMessage(Socket socket) throws IOException { out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); is = new BufferedReader(new InputStreamReader(System.in)); } public void run() { String input = new String(); while (true) { try { input = is.readLine().trim(); } catch (IOException e) { e.printStackTrace(); } out.println(input); System.out.print("服务器说:"); out.flush(); } } }
收信的线程类:
package com.example.serve; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; public class GetMessage implements Runnable{ BufferedReader in; MainActivity context; public GetMessage(Socket socket,MainActivity context) { try { this.context = context; in = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (IOException e) { e.printStackTrace(); } } public void run() { String str = ""; while (true) { try { str = in.readLine(); } catch (IOException e) { e.printStackTrace(); } if (str.equals("q")) { break; } if(str!=null) { System.out.print("客户端回应说:" + str); MainActivity.tt.show(); } } } }
然后是第二个client的手机程序:
public class MainActivity extends Activity { TextView tv = null; CGetMessage gm; CSendMessage sm; PrintWriter out ; static Toast tt = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView)findViewById(R.id.asd); tt = Toast.makeText(MainActivity.this, "qe", Toast.LENGTH_SHORT); } public void Open(){ try { System.out.println("客户端!!" ); InetAddress addr = InetAddress.getByName("192.168.43.xx");//ip地址自己个性化 Socket socket = new Socket(addr, 6666); System.out.println("客户端发出socket=" + socket); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); gm = new CGetMessage(socket); System.out.println("你好"); sm = new CSendMessage(socket); Thread gt = new Thread(gm); gt.start(); } catch (Exception e) { e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0,0,0,"yiqi"); menu.add(0,1,1,"ni"); return true; } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { if(item.getItemId() == 0){ out.println("yiqi"); out.flush(); } else{ Open(); //out.println("ni"); //out.flush(); } return super.onMenuItemSelected(featureId, item); } }
然后同样是收信的类:
public class CGetMessage implements Runnable { private Socket socket; BufferedReader in; public CGetMessage(Socket socket) { this.socket = socket; } public void run() { BufferedReader in; String str = ""; while (true) { try { in = new BufferedReader(new InputStreamReader(socket.getInputStream())); str = in.readLine(); } catch (IOException e) { e.printStackTrace(); } if(str!=null){ System.out.println("服务器回应说:" + str); MainActivity.tt.show(); } } } }
然后是发信的类:
public class CSendMessage implements Runnable { PrintWriter out; BufferedReader is; public CSendMessage(Socket socket) throws IOException { out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); //is是读入的字符串 //is = new BufferedReader(new InputStreamReader(System.in)); } public void run() { String input = new String(); while (true) { try { input = is.readLine().trim(); } catch (IOException e) { e.printStackTrace(); } out.println(input); System.out.print("客户端说:"); out.flush(); } } }
注意,两个程序需要安装在两个手机上(话说没人会傻到安到一个手机上吧),然后互相链接热点。。。今天的更新完毕