我用的setontouchlistener,但是只要一点击前进就会一直走,而不会停下来。并且我再按其他的按钮也没有反应。而我只有把程序关闭掉重启才能继续点击按钮并控制,这是怎么回事啊?把代码附上来
package hanry;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import bb.baibin.R;
public class Main extends Activity
{
private static final OnClickListener Forward = null;
EditText edAlias,edIP,edPort,edUser,edPW;
TextView t;
private Button ForWard;
private Button BackWard;
private Button TurnLeft;
private Button TurnRight;
private Button ButtonStop;
URL videoUrl;
Bitmap bmp;
private boolean isConnecting = false;
private Thread mThreadClient = null;
private Socket mSocketClient = null;
static PrintWriter mPrintWriterClient = null;
static BufferedReader mBufferedReaderClient = null;
public static String CameraIp;
MySurfaceView r;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//隐去标题(应用的名字必须要写在setContentView之前,否则会有异常)
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
ForWard= (Button)findViewById(R.id.ForWard);
TurnLeft= (Button)findViewById(R.id.TurnLeft);
TurnRight= (Button)findViewById(R.id.TurnRight);
BackWard= (Button)findViewById(R.id.BackWard);
ButtonStop=(Button)findViewById(R.id.ButtonStop);
r=(MySurfaceView)findViewById(R.id.mySurfaceView1);
CameraIp ="http://192.168.1.1:8080/?action=snapshot";
final byte[] buf1 = {(byte)0xff,(byte)0x00,(byte)0x04,(byte)0x00,(byte)0xff};
final byte[] buf2= {(byte)0xff,(byte)0x00,(byte)0x03,(byte)0x00,(byte)0xff};
final byte[] buf3 = {(byte)0xff,(byte)0x00,(byte)0x01,(byte)0x00,(byte)0xff};
final byte[] buf4 = {(byte)0xff,(byte)0x00,(byte)0x02,(byte)0x00,(byte)0xff};
final byte[] buf5 = {(byte)0xff,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0xff};
r.GetCameraIP(CameraIp);
mThreadClient = new Thread(mRunnable);
mThreadClient.start();
ForWard.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(event.getAction()==MotionEvent.ACTION_MOVE){
sendMessage_btye(buf1);
Log.e("1111111111","1111111111");
}else if(event.getAction()==MotionEvent.ACTION_UP){
sendMessage_btye(buf5);
Log.e("222222222222","222222222");
}
return false;
}
}
);
BackWard.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(event.getAction()==MotionEvent.ACTION_DOWN){
sendMessage_btye(buf2);
}else if(event.getAction()==MotionEvent.ACTION_UP){
sendMessage_btye(buf5);
}
return false;
}
}
);
TurnRight.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(event.getAction()==MotionEvent.ACTION_DOWN){
sendMessage_btye(buf3);
}else if(event.getAction()==MotionEvent.ACTION_UP){
sendMessage_btye(buf5);
}
return false;
}
}
);
TurnLeft.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
sendMessage_btye(buf4);
break;
case MotionEvent.ACTION_UP:
sendMessage_btye(buf5);
}
return false;
}
}
);
ButtonStop.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
sendMessage_btye(buf5);
break;
case MotionEvent.ACTION_UP:
sendMessage_btye(buf5);
}
return false;
}
}
);
}
private Runnable mRunnable = new Runnable()
{
public void run()
{
String msgText ="192.168.1.1:2001";
int start = msgText.indexOf(":");
String sIP = msgText.substring(0, start);
String sPort = msgText.substring(start+1);
int port = Integer.parseInt(sPort);
Log.d("gjz", "IP:"+ sIP + ":" + port);
try
{
//连接服务器
mSocketClient = new Socket(sIP, port); //portnum
//取得输入、输出流
mBufferedReaderClient = new BufferedReader(new InputStreamReader(mSocketClient.getInputStream()));
mPrintWriterClient = new PrintWriter(mSocketClient.getOutputStream(), true);
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
//break;
}
catch (Exception e)
{
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
return;
}
char[] buffer = new char[256];
while (isConnecting)
{
try
{
//if ( (recvMessageClient = mBufferedReaderClient.readLine()) != null )
if((mBufferedReaderClient.read(buffer))>0)
{
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
}
}
catch (Exception e)
{
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
}
}
}
};
Handler mHandler = new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
}
};
public void sendMessage_btye(byte[] data) {
try {
OutputStream out = mSocketClient.getOutputStream();
out.write(data);
out.flush();
} catch (Exception e) {
Log.i("Socket", e.getMessage() != null ? e.getMessage().toString() : "sendCommand error!");
}
}
public void onDestroy() {
super.onDestroy();
if (isConnecting)
{
isConnecting = false;
try {
if(mSocketClient!=null)
{
mSocketClient.close();
mSocketClient = null;
mPrintWriterClient.close();
mPrintWriterClient = null;
}
} catch (IOException e) {
e.printStackTrace();
}
mThreadClient.interrupt();
}
}
}
|