一、MainActivity.java
package com.example.OneMore;
import com.example.OneMore.net.NetUtls;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Handler handler;
Handler handler1;
Button btn;
TextView tx;
TextView tx1;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NetUtls.ClientInit(); //客服端初始化,建立连接
// new NetUtils("111").readMessage(); //客服端接收
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
tx = (TextView) findViewById(R.id.fire);
tx1 = (TextView) findViewById(R.id.infrared);
btn = (Button) findViewById(R.id.button1);
handler =new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Bundle b = msg.getData();
String string = b.getString("msg");
tx.setText(string);
}
};
NetUtls.readMessage(handler);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
NetUtls.sendMessage("Love u \r\n");
}
});
}
}
二、NetUtls.java
package com.example.OneMore.net;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.Bundle;
import android.os.Handler;
public class NetUtls {
private static Socket client =null;
public static void ClientInit() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
client = new Socket("192.168.1.13",8091);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
public static void readMessage(final Handler h) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
String str = new String();
byte[] data = new byte[500];
int len = 0;
java.io.InputStream in = client.getInputStream();
while(true) {
len = in.read(data);
str = new String(data,0,len);
android.os.Message msg = new android.os.Message();
Bundle b = new Bundle();
b.putString("msg", str);
msg.setData(b);
h.sendMessage(msg);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
public static void sendMessage(final String command) {
new Thread(new Runnable() {
@Override
public void run() {
try {
OutputStream out = client.getOutputStream();
out.write(command.getBytes());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}
三、activity_main.xml
<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:background="#D8BFD8"
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" >
<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>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="火灾监测:"
android:textSize="20sp" />
<TextView
android:id="@+id/fire"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:textSize="20sp" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="人体红外监测:"
android:textSize="20sp" />
<TextView
android:id="@+id/infrared"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:textSize="20sp" />
</TableRow>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableLayout>
</RelativeLayout>