http://www.cnblogs.com/yangfengwu/p/8776712.html
先把源码和资料链接放到这里
链接:https://pan.baidu.com/s/10MxI8-Q33-M_R2WEHqEi1A密码:j1sz
先做手机的,然后做C#的
详细点的可以看我这篇文章,请参考着这篇看这篇文章,这篇文章会解决一些细节问题
http://www.cnblogs.com/yangfengwu/p/5212570.html
咱们不做很复杂的直接越简单越好,就做成这样
先编译一下
不用管,后期的话咱会用一下
最后做成这样子
发现还是有点大..
把像素低的放到像素高的里面图片显示出来会缩小,把像素高的放到像素低的里面图片显示出来会放大
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageViewLamp = (ImageView) findViewById(R.id.imageView1);
switchLamp = (Switch) findViewById(R.id.switch1);
switchLamp.setOnCheckedChangeListener(switchLamplistener);//设置SWITCH的状态改变事件
}
privateOnCheckedChangeListener switchLamplistener =new OnCheckedChangeListener() {
@Override
publicvoid onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub// Toast.makeText(getApplicationContext(), isChecked+"", 500).show();if (isChecked) {//切换图片
imageViewLamp.setImageResource(R.drawable.ledon);
}
else {
imageViewLamp.setImageResource(R.drawable.ledoff);
}
}
};
现在做点击连接按钮就连接服务器
刚看到...............
有点迫不及待的想试一试腾讯云了,
下面做的是:点击连接按钮,连接TCP服务器,连接上以后启动数据接收任务,因为数据接收任务可以判断是不是和服务器断开了连接
然后按钮显示"断开",如果意外断开了连接也显示断开....
publicclass MainActivity extends Activity {
ImageView imageViewLamp;//灯的图片Switch switchLamp;//灯的控制开关EditText editTextIPAdress,editTextPort;//ip地址和端口号的编辑框Button buttonConnect;//连接按钮Socket socket;//cocketboolean ConnectFlage =false;//连接标志,控制按钮显示连接和断开ThreadConnectService threadConnectService =newThreadConnectService();//建立一个连接任务的变量InputStream inputStream;//获取输入流,可以用来判断有没有断开连接ThreadReadData threadReadData =newThreadReadData();//接收数据的任务的变量boolean threadReadDataFlage =false;//接收数据任务一直运行控制byte[] ReadBuffer =newbyte[1024];//存储接收到的数据intReadBufferLengh =0;//接收到的数据个数
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextIPAdress = (EditText) findViewById(R.id.editText1);
editTextPort = (EditText) findViewById(R.id.editText2);
buttonConnect = (Button) findViewById(R.id.button1);
buttonConnect.setOnClickListener(buttonConnectClick);
imageViewLamp = (ImageView) findViewById(R.id.imageView1);
switchLamp = (Switch) findViewById(R.id.switch1);
switchLamp.setOnCheckedChangeListener(switchLamplistener);
}
/*指示灯控制开关*/privateOnCheckedChangeListener switchLamplistener =new OnCheckedChangeListener() {
@Override
publicvoid onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
imageViewLamp.setImageResource(R.drawable.ledon);
}
else {
imageViewLamp.setImageResource(R.drawable.ledoff);
}
}
};
/*按钮点击连接事件*/privateOnClickListener buttonConnectClick =new OnClickListener() {
@Override
publicvoid onClick(View v) {
if (ConnectFlage)
{
try {
threadConnectService.start();//启动连接任务 }
catch(Exception e)//预防任务还没关闭呢又点击开始 {
threadConnectService.run();
}
}
else
{
ConnectFlage =true;
buttonConnect.setText("连接");
try
{
socket.close();//关闭socketinputStream.close();//关闭数据流}catch (Exception e) {
// TODO: handle exception }
}
}
};
/**
* 连接服务器的任务
* @author yang
*
*/class ThreadConnectService extends Thread
{
publicvoid run()
{
InetAddress ipAddress;
try
{
ipAddress = InetAddress.getByName(editTextIPAdress.getText().toString());//获取IP地址intport =Integer.valueOf(editTextPort.getText().toString());//获取端口号 socket =newSocket(ipAddress, port);//创建连接地址和端口inputStream = socket.getInputStream();//获得通道的数据流变量threadReadDataFlage =true;//一直接收数据try
{
threadReadData.start();
}
catch(Exception e) {//预防任务还没关闭呢又点击开始 threadReadData.run();
}
runOnUiThread(newRunnable() {//修改界面的UI最好用Handle,这里力求简单,下几节再用publicvoid run() {
ConnectFlage =false;
buttonConnect.setText("断开");
Toast.makeText(getApplicationContext(), "连接成功",500).show();
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
* 接收数据的任务
* @author yang
*
*/class ThreadReadData extends Thread
{
publicvoid run()
{
while (threadReadDataFlage)
{
try
{
ReadBufferLengh = inputStream.read(ReadBuffer);//服务器断开会返回-1if(ReadBufferLengh == -1) {
threadReadDataFlage =false;
runOnUiThread(newRunnable() {//修改界面的UI最好用Handle,这里力求简单,下几节再用publicvoid run() {
ConnectFlage =true;
buttonConnect.setText("连接");
Toast.makeText(getApplicationContext(), "与服务器断开连接",500).show();
}
});
}
}
catch (Exception e)
{
// TODO Auto-generated catch blockLog.e("error", ReadBufferLengh+"");
e.printStackTrace();
}
}
}
}
/** 当活动(界面)不再可见时调用 */ @Override
protectedvoid onStop()
{
threadReadDataFlage =false;//结束接收数据任务 super.onStop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
intid = item.getItemId();
if(id == R.id.action_settings) {
returntrue;
}
return super.onOptionsItemSelected(item);
}
}
忘了说一件事情....加权限
"
android:minSdkVersion="17" android:targetSdkVersion="21"/>
android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
我把上面做的打包了
好现在接着写发数据(控制灯的亮灭)
publicclass MainActivity extends Activity {
ImageView imageViewLamp;//灯的图片Switch switchLamp;//灯的控制开关EditText editTextIPAdress,editTextPort;//ip地址和端口号的编辑框Button buttonConnect;//连接按钮Socket socket;//cocketboolean ConnectFlage =true;//连接标志,控制按钮显示连接和断开ThreadConnectService threadConnectService =newThreadConnectService();//建立一个连接任务的变量InputStream inputStream;//获取输入流,可以用来判断有没有断开连接OutputStream outputStream;//获得输出流ThreadReadData threadReadData =newThreadReadData();//接收数据的任务ThreadSendData threadSendData =newThreadSendData();//发送数据的任务boolean threadReadDataFlage =false;//接收数据任务一直运行控制boolean threadSendDataFlage =false;//接收数据任务一直运行控制byte[] ReadBuffer =newbyte[1024];//存储接收到的数据byte[] SendBuffer =newbyte[1024];//存储发送的数据intReadBufferLengh =0;
intSendDataCnt =0;//控制发送数据的个数 @Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextIPAdress = (EditText) findViewById(R.id.editText1);
editTextPort = (EditText) findViewById(R.id.editText2);
buttonConnect = (Button) findViewById(R.id.button1);
buttonConnect.setOnClickListener(buttonConnectClick);
imageViewLamp = (ImageView) findViewById(R.id.imageView1);
switchLamp = (Switch) findViewById(R.id.switch1);
switchLamp.setOnCheckedChangeListener(switchLamplistener);
}
/*指示灯控制开关*/privateOnCheckedChangeListener switchLamplistener =new OnCheckedChangeListener() {
@Override
publicvoid onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
imageViewLamp.setImageResource(R.drawable.ledon);
SendBuffer[0] = (byte)0xaa;
SendBuffer[1] =0x55;
SendBuffer[2] =0x02;
SendBuffer[3] = (byte)0xff;
}
else {
imageViewLamp.setImageResource(R.drawable.ledoff);
SendBuffer[0] = (byte)0xaa;
SendBuffer[1] =0x55;
SendBuffer[2] =0x02;
SendBuffer[3] =0x00;
}
SendDataCnt =4;//控制发送数据的个数 }
};
/*按钮点击连接事件*/privateOnClickListener buttonConnectClick =new OnClickListener() {
@Override
publicvoid onClick(View v) {
if (ConnectFlage)
{
try
{
threadConnectService.start();//启动连接任务 }
catch(Exception e)//预防任务还没关闭呢又点击开始 {
threadConnectService.run();
}
}
else
{
ConnectFlage =true;
threadSendDataFlage =false;//关掉发送任务,预防产生多的任务threadReadDataFlage =false;//关掉接收任务,预防产生多的任务buttonConnect.setText("连接");
try
{
socket.close();//关闭socketinputStream.close();//关闭数据流}catch (Exception e) {
// TODO: handle exception }
}
}
};
/**
* 连接服务器的任务
* @author yang
*
*/class ThreadConnectService extends Thread
{
publicvoid run()
{
InetAddress ipAddress;
try
{
ipAddress = InetAddress.getByName(editTextIPAdress.getText().toString());//获取IP地址intport =Integer.valueOf(editTextPort.getText().toString());//获取端口号 socket =newSocket(ipAddress, port);//创建连接地址和端口inputStream = socket.getInputStream();//获得通道的数据流outputStream = socket.getOutputStream();//获得通道的输出流threadReadDataFlage =true;//一直接收数据threadSendDataFlage =true;//一直循环的判断是否发送数据try
{
threadReadData.start();
}
catch(Exception e) {//预防任务还没关闭呢又点击开始 threadReadData.run();
}
try {
threadSendData.start();
} catch (Exception e) {
threadSendData.run();
}
runOnUiThread(newRunnable() {//修改界面的UI最好用Handle,这里力求简单,下几节再用publicvoid run() {
ConnectFlage =false;
buttonConnect.setText("断开");
Toast.makeText(getApplicationContext(), "连接成功",500).show();
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
* 接收数据的任务
* @author yang
*
*/class ThreadReadData extends Thread
{
publicvoid run()
{
while (threadReadDataFlage)
{
try
{
ReadBufferLengh = inputStream.read(ReadBuffer);//服务器断开会返回-1if(ReadBufferLengh == -1) {
threadSendDataFlage =false;//关掉发送任务,预防产生多的任务threadReadDataFlage =false;//关掉接收任务,预防产生多的任务SendDataCnt =0;//清零发送的个数ConnectFlage =true;
runOnUiThread(newRunnable() {//修改界面的UI最好用Handle,这里力求简单,下几节再用publicvoid run() {
buttonConnect.setText("连接");
Toast.makeText(getApplicationContext(), "与服务器断开连接",500).show();
}
});
}
}
catch (Exception e)
{
// TODO Auto-generated catch blockLog.e("error", ReadBufferLengh+"");
e.printStackTrace();
runOnUiThread(newRunnable() {//修改界面的UI最好用Handle,这里力求简单,下几节再用publicvoid run() {
buttonConnect.setText("连接");
Toast.makeText(getApplicationContext(), "与服务器断开连接",500).show();
}
});
ConnectFlage =true;
threadSendDataFlage =false;//关掉发送任务,预防产生多的任务threadReadDataFlage =false;//关掉接收任务,预防产生多的任务SendDataCnt =0;//清零发送的个数 }
}
}
}
/**
* 发送数据任务
* @author yang
*
*/class ThreadSendData extends Thread
{
publicvoid run()
{
while (threadSendDataFlage)
{
if(SendDataCnt>0)//要发送的数据个数大于0 {
try
{
outputStream.write(SendBuffer,0,SendDataCnt);//发送数据SendDataCnt =0;//清零发送的个数 }
catch (Exception e)
{
runOnUiThread(newRunnable() {//修改界面的UI最好用Handle,这里力求简单,下几节再用publicvoid run() {
buttonConnect.setText("连接");
Toast.makeText(getApplicationContext(), "与服务器断开连接",500).show();
}
});
ConnectFlage =true;
threadSendDataFlage =false;//关掉发送任务,预防产生多的任务threadReadDataFlage =false;//关掉接收任务,预防产生多的任务SendDataCnt =0;
}
}
}
}
}
/** 当活动(界面)不再可见时调用 */ @Override
protectedvoid onStop()
{
threadReadDataFlage =false;//结束接收数据任务threadSendDataFlage =false;//结束发送数据任务SendDataCnt =0;
super.onStop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
intid = item.getItemId();
if(id == R.id.action_settings) {
returntrue;
}
return super.onOptionsItemSelected(item);
}
}
发送数据是写在了一个任务里面
整体的源码
本来想这一节也写好C#的,不过感觉写的够多的了,所以C#的放到下一节
下一篇
网友评论