资源简介
核心代码:
package com.Test; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.app.Activity; import android.view.Menu; import android.widget.Button; import android.view.View; import android.view.View.OnClickListener; import java.io.BufferedInputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.PrintWriter; import java.io.Reader; import java.net.Socket; import java.net.ServerSocket; import java.net.UnknownHostException; import android.view.TextureView; import android.widget.EditText; import java.lang.String; import java.lang.Thread; public class MainActivity extends Activity implements OnClickListener{ private Button m_btn; private Button m_sendbtn; private ServerSocket mySerSocket; private Socket clientSocket; private EditText m_edit; private String line; boolean conn = true; private Socket accSocket; private static final String Host = "10.0.2.2"; private static final int Port = 12000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); m_btn = (Button)findViewById(R.id.button1); m_sendbtn = (Button)findViewById(R.id.button2); m_edit = (EditText)findViewById(R.id.editText1); //m_btn.setOnClickListener(this); m_btn.setOnClickListener(new ReceiverListener()); m_sendbtn.setOnClickListener(this); //Socket clientSocket = new Socket(Host, Port); } class ReceiverListener implements OnClickListener { private ReceiveThread mReceiveThread = null; private boolean stop = true; private Handler mHandler = null; public void onClick(View v) { try { if(clientSocket == null) { clientSocket = new Socket(Host, Port); } // InputStream myInputStream = clientSocket.getInputStream(); // // //BufferedReader in = new BufferedReader(new InputStreamReader(myInputStream)); // // byte [] buffer = new byte[myInputStream.available()]; //int temp = 0; //String strMsg = in.readLine(); // myInputStream.read(buffer); // // // String strMsg = new String(buffer); // // m_edit.setText(strMsg); // while ((temp = myInputStream.read(buffer)) != -1) // { // m_edit.setText(new String(buffer, 0, temp)); // // } mReceiveThread = new ReceiveThread(clientSocket); stop = false; mReceiveThread.start(); } catch (IOException e) { e.printStackTrace(); } //消息处理 mHandler = new Handler() { public void handleMessage(Message msg) { m_edit.setText((msg.obj).toString()); } }; } private class ReceiveThread extends Thread { private InputStream inStream = null; private byte[] buf; private String str = null; ReceiveThread(Socket s) { try { this.inStream = s.getInputStream(); } catch(IOException e) { e.printStackTrace(); } } public void run() { while(!stop) { this.buf = new byte[512]; try { this.inStream.read(this.buf); } catch(IOException e) { e.printStackTrace(); } try { this.str = new String(this.buf, "GB2312").trim(); } catch(UnsupportedEncodingException e) { e.printStackTrace(); } Message msg = new Message(); msg.obj = this.str; mHandler.sendMessage(msg); } } } } public void onClick(View v) { // if (v.getId() == R.id.button1) // { // new Thread() // { // public void run() // { // try // { // if(clientSocket == null) // { // clientSocket = new Socket(Host, Port); // } // InputStream myInputStream = clientSocket.getInputStream(); // // BufferedReader in = new BufferedReader(new InputStreamReader(myInputStream)); // // byte [] buffer = new byte[1024 * 4]; // // int temp = 0; // // String strMsg = in.readLine(); // // //myInputStream.read(buffer); // // //String strMsg = new String(buffer); // // m_edit.setText(strMsg); // //// while ((temp = myInputStream.read(buffer)) != -1) //// { //// m_edit.setText(new String(buffer, 0, temp)); //// //// } // // } // catch (IOException e) // { // e.printStackTrace(); // } // } // }.start(); // // } if(v.getId() == R.id.button2) { new Thread() { public void run() { try { if(clientSocket == null) { clientSocket = new Socket(Host, Port); } //inputStream clientInputStream = clientSocket. String msg = "RESET-SHOW"; //InputStream clientInputStream = clientSocket.getInputStream(); //BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); //BufferedReader br = new BufferedReader(); //line = br.readLine(); OutputStream clientOutputStream = clientSocket.getOutputStream(); byte buffer[]=msg.getBytes(); clientOutputStream.write(buffer); clientOutputStream.flush(); //int temp = 0; // PrintWriter out = new PrintWriter(clientOutputStream); // // out.println("Serer Message: " msg); } catch(IOException e) { e.printStackTrace(); } } }.start(); } } private Reader InputStreamReader() { // TODO Auto-generated method stub return null; } // private Handler handler = new Handler() // { // public void handleMessage(Message msg) // { // super.handleMessage(msg); // //处理UI // m_edit.setText("Finish"); // Log.i("PDA", "Finish"); // // } // }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
代码片段和文件信息
package com.Test;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.view.View;
import android.view.View.onclickListener;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import android.view.TextureView;
import android.widget.EditText;
import java.lang.String;
import java.lang.Thread;
相关资源
- android 对话框
- android 自定义Toast颜色
- android 前面部分可以编辑后面部分不可
- android win8界面风格扁平化设计点击动
- android 蓝牙连接打印机
- android UPNP
- android 计算自己手机的屏幕宽高,de
- android 一键锁屏,android4.2测试通过
- android 滑动解锁源码
- android spinner intent 参数传递
- android widget小部件开发
- android 定时进入飞行模式 例子源码下
- Android调用谷歌STMP发送邮件
- android 简单的截屏
- android 下拉滑动刷新List
- android textview 展示图片
- Android 基于author2.0的新浪微博 源码,
- android 树状ListView 自定义列表 源码
- Android移动开发一本就够源码
- 监听android应用被卸载后 再使用libcu
- Android中禁止某软件的安装
- Android BLE程序
- android 白天黑夜模式切换源码
- Android 图片加上水印效果
- android 判断双SD卡_CheckDoubleSDCard 源码
- android 创建桌面快捷方式源代码
- Android Launcher 源码修改可编译 源码
- Android Launcher 桌面分页滑动代码
- 安卓 EyeRoom 完整
- android系统 ContactsProvider模块
评论
共有 条评论