啥也不说了, 直接上代码, run 一下就理解了。
/** Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.helloactivity;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class HelloActivity extends Activity implements View.OnClickListener {
private static String TAG = "HelloActivity";
private LocalSocket receiver;
private LocalServerSocket localSocket;
private LocalSocket sender;
private static final int BUFFER_SIZE = 4;
private boolean running;
private int i = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the layout for this activity. You can find it
// in res/layout/hello_activity.xml
View view = getLayoutInflater().inflate(R.layout.hello_activity, null);
setContentView(view);
Button mButton_1 = (Button) view.findViewById(R.id.one);
Button mButton_2 = (Button) view.findViewById(R.id.two);
mButton_1.setText("send and receve");
mButton_2.setText("cancel");
mButton_1.setOnClickListener(this);
mButton_2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d(TAG, "V ==" + v.getId());
switch (v.getId()) {
case R.id.one: {
sendMessage();
break;
}
case R.id.two: {
running = false;
try {
lss.close();
} catch (Exception e) {
// TODO: handle exception
}
try {
if (receiver.isConnected()) {
receiver.close();
}
} catch (Exception e) {
// TODO: handle exception
}
break;
}
}
}
private int sendMessage() {
receiver = new LocalSocket();
try {
lss = new LocalServerSocket("Local_Socket");
receiver.connect(new LocalSocketAddress("Local_Socket"));
receiver.setReceiveBufferSize(BUFFER_SIZE);
receiver.setSendBufferSize(BUFFER_SIZE);
sender = lss.accept();
sender.setReceiveBufferSize(BUFFER_SIZE);
sender.setSendBufferSize(BUFFER_SIZE);
// 将控制器running设置为true
running = true;
// 启动发送接受线程
new Thread(local_send).start();
new Thread(local_receive).start();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
Thread local_receive = new Thread() {
public void run() {
InputStream m_Rece = null;
try {
m_Rece = receiver.getInputStream();
byte[] data;
int receiveLen = 0;
while (running) {
receiveLen = receiver.getReceiveBufferSize();
/**
* the 4 can replaced by receiveLen
*/
data = new byte[4];
m_Rece.read(data);
Log.i(TAG, "receiver.getReceiveBufferSize()"
+ new String(data).toString());
Thread.sleep(1000);
// 将i设为0
i = 0;
}
m_Rece.close();
receiver.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread local_send = new Thread() {
// 线程运行函数
public void run() {
OutputStream m_Send = null;
try {
m_Send = sender.getOutputStream();
while (running) {
byte[] data = "123".getBytes();
sender.setSendBufferSize(data.length);
sender.setReceiveBufferSize(data.length);
m_Send.write(data);
Log.i(TAG, "local_send.data=" + new String(data));
m_Send.flush();
Thread.sleep(100);
i++;
}
m_Send.close();
sender.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}