-
Android tcp通讯学习用
核心代码: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; }}
- 2014-08-18下载
- 积分:1
-
Android 結束多重頁面最佳方法
Android 當中常常會開起很多頁面,但是要在程式結束的時候。將開過的頁面都結束掉,有多種方法。本程是採用頁面紀錄的方式,每開啟一個頁面就將該頁面紀錄在List中,當要關閉程式的時候,就可以使用function exit 完整的退出程式。
- 2022-07-15 02:14:12下载
- 积分:1
-
Android用Intent实现Video 视频功能
Android用Intent实现Video 视频功能
- 2014-01-19下载
- 积分:1
-
App自动更新
App自动更新
- 2013-10-23下载
- 积分:1
-
qiangtandenglu
《抢滩登陆》属于即时360度全方位射击游戏,相对于同类游戏较为简单,但容量又大于普通的小游戏,玩法非常简单,很容易上手。游戏情节惊心动魄,游戏场景中变化多端的敌军、即时控制的动作都是吸引玩家的亮点。
20世纪60年代,战争的硝烟在整个欧洲大陆上弥漫,盟军在大陆上的作战陷入了胶着状态。为了牵制敌军的行动并最终取得决定性的胜利,玩家的使命就是:尽最大可能消耗敌人的入侵部队,阻止敌人的进攻。面对滩头上潮水一般涌来的敌人,唯一能依靠的就只有身后那门威力无比强大的海防炮台。
android游戏(Landing on a beach belonging to real-time 360-degree shooting game, it has a relatively simple compared to similar games, but the capacity is greater than the ordinary game play is very simple, very easy to use. The game plot thrilling, varied enemy in the game scene, real-time control actions are the highlights to attract players. 1960s, the war on the entire European continent, filled the air, the Allied forces fighting on the mainland into a stalemate. In order to pin down the enemy' s actions and ultimately to achieve a decisive victory, the player' s mission is: to the maximum extent possible consumption of the invading forces of the enemy, to prevent the enemy' s attack. The face of the enemy beachhead came flooding, only able to rely on only behind that door power of powerful coastal defense fort. android game)
- 2012-07-08 08:36:56下载
- 积分:1
-
EX10_04
二维码条形码生成器,在不联网的情况下自动生成(The two-dimensional code bar code generator automatically generate, in the case of non-networked)
- 2012-12-03 13:36:33下载
- 积分:1
-
相册预览效果,模拟系统自带的效果。
应用背景相册预览效果,模拟系统自带的效果,可以直接运行。关键技术1,可以左右滑动预览。2,可以放大缩小预览。3,可以直接导入使用
- 2022-08-12 19:32:36下载
- 积分:1
-
股票报价
此应用程序查询
网站
雅虎金融、
和显示
股票报价报
在网站上
- 2023-01-16 07:25:03下载
- 积分:1
-
共享的偏好
应用程序共享的偏好基本上与 android 的意思,当我们想要存储较小的数据基本在我们的应用程序,就可以使用到它的数据基础。
- 2023-02-24 16:05:04下载
- 积分:1
-
Android游戏源码仿别碰白块儿游戏
模仿本项目是模仿别踩白块项目,第
一个比较完善,这个相对比较简单,源码也不多适合新手学习借鉴。别踩白块儿曾经在app
store里全球40多个国家、地区免费游戏榜登顶榜首,超过一百个国家或地区排名前十。说明还是一款非常经典耐玩广受欢迎的小游戏。
- 2023-07-13 11:50:03下载
- 积分:1