代码
package com.example.socket;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessageHandler()
{
try {
Socket client = new Socket("192.168.22.1",8080);
OutputStream out = client.getOutputStream();
String message = "message from android";
out.write(message.getBytes());
}
catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage(View v){
new Thread(new Runnable(){
@Override
public void run() {
sendMessageHandler();
}
}).start();
}
}
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="发送" />
</RelativeLayout>
优化后的
package com.example.socket;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import com.example.socket.nets.NetUtils;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View v){
switch(v.getId()){
case R.id.kct:
NetUtils.sendMessageHandler("kctd");
break;
case R.id.kws:
NetUtils.sendMessageHandler("kwsd");
break;
case R.id.gct:
NetUtils.sendMessageHandler("gctd");
break;
case R.id.gel:
NetUtils.sendMessageHandler("geld");
break;
case R.id.gws:
NetUtils.sendMessageHandler("gwsd");
break;
case R.id.gys:
NetUtils.sendMessageHandler("gysd");
break;
case R.id.kel:
NetUtils.sendMessageHandler("keld");
break;
case R.id.kys:
NetUtils.sendMessageHandler("kysd");
break;
case R.id.ljms:
NetUtils.sendMessageHandler("gqbd");
break;
}
}
}
package com.example.socket.nets;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class NetUtils {
public static void sendMessageHandler(final String command)
{
new Thread(new Runnable(){
@Override
public void run() {
try {
Socket client = new Socket("192.168.129.72",8085);
OutputStream out = client.getOutputStream();
out.write(command.getBytes());
}
catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#D8BFD8"
tools:context=".MainActivity" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#D8BFD8"
android:stretchColumns="0,1" >
<TableRow>
<Button
android:id="@+id/kct"
android:layout_width="match_parent"
android:layout_height="40dp"
android:onClick="sendMessage"
android:text="开餐厅灯" />
<Button
android:id="@+id/gct"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:onClick="sendMessage"
android:text="关餐厅灯" />
</TableRow>
<TableRow>
<Button
android:id="@+id/kws"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:onClick="sendMessage"
android:text="开卧室灯" />
<Button
android:id="@+id/gws"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:onClick="sendMessage"
android:text="关卧室灯" />
</TableRow>
<TableRow>
<Button
android:id="@+id/kel"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:onClick="sendMessage"
android:text="开二楼灯" />
<Button
android:id="@+id/gel"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:onClick="sendMessage"
android:text="关二楼灯" />
</TableRow>
<TableRow>
<Button
android:id="@+id/gys"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:onClick="sendMessage"
android:text="开浴室灯" />
<Button
android:id="@+id/kys"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:onClick="sendMessage"
android:text="关浴室灯" />
</TableRow>
<Button
android:id="@+id/ljms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="离家模式" />
</TableLayout>
</RelativeLayout>
|