我們正在嘗試構建一個Android應用程序以充當機器人的遠程控制。我們的代碼已經完成了,但是關於按住鍵還有一個問題。我們使用軟鍵作爲應用程序的基本結構,並對其進行了修改,以便方向鍵可以遠程控制機器人。但是,我們的代碼存在問題。當我們按下(例如)「向上」按鈕時,即使我們釋放了按鍵,應用程序也會繼續發送「向上」信號。一旦我們釋放按鈕,是否有辦法阻止信號?請諮詢,感謝您的幫助提前。軟鍵按鈕
我們的代碼段:
private boolean genericClick(View v, boolean longClick, MotionEvent me) {
if(me != null) {
Toast.makeText(this, "Warning: MotionEvent is broken", Toast.LENGTH_LONG);
}
// send an intent to the main window
int keyid = 0;
Globals app = (Globals)getApplication();
boolean hide = auto_hide;
switch(v.getId()) {
case R.id.home:
Log.v("MyActivity", "Wifi");
try {
s = new Socket("142.58.160.178", 5000);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
writer.write("Connect");
writer.flush();
writer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "Connection Failed", Toast.LENGTH_LONG);
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "Connection Failed", Toast.LENGTH_LONG);
e.printStackTrace();
}
//app.doHomeAction(longClick);
break;
case R.id.extra_center:
keyid = K.KEYID_DPAD_CENTER;
//Log.v("MyActivity", "stop");
mHandler.removeCallbacks(mUpdateTask);
hide = false;
break;
case R.id.extra_up:
keyid = K.KEYID_DPAD_UP;
//Log.v("MyActivity", "up1");
direction=0; //up
mHandler.removeCallbacks(mUpdateTask);
mHandler.postAtTime(mUpdateTask, SystemClock.uptimeMillis() + 50);
hide = false;
break;
case R.id.extra_down:
keyid = K.KEYID_DPAD_DOWN;
//Log.v("MyActivity", "down1");
direction=1;
mHandler.removeCallbacks(mUpdateTask);
mHandler.postAtTime(mUpdateTask, SystemClock.uptimeMillis() + 50);
hide = false;
break;
case R.id.extra_left:
keyid = K.KEYID_DPAD_LEFT;
//Log.v("MyActivity", "left1");
direction=2;
mHandler.removeCallbacks(mUpdateTask);
mHandler.postAtTime(mUpdateTask, SystemClock.uptimeMillis() + 50);
hide = false;
break;
case R.id.extra_right:
keyid = K.KEYID_DPAD_RIGHT;
//Log.v("MyActivity", "right1");
direction=3;
mHandler.removeCallbacks(mUpdateTask);
mHandler.postAtTime(mUpdateTask, SystemClock.uptimeMillis() + 50);
hide = false;
break;
}
}
private Runnable mUpdateTask = new Runnable()
{
public void run()
{
switch(direction){
case 0:
Log.i("repeatBtn", "Up");
if (s!=null){
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
writer.write("U");
writer.flush();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case 1:
// Log.i("repeatBtn", "Down");
if (s!=null){
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
writer.write("D");
writer.flush();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case 2:
// Log.i("repeatBtn", "Left");
if (s!=null){
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
writer.write("L");
writer.flush();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case 3:
// Log.i("repeatBtn", "Right");
if (s!=null){
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
writer.write("R");
writer.flush();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
mHandler.postAtTime(this, SystemClock.uptimeMillis() + 50);
}
};
你是否正在連接到點擊處理程序中的套接字?我想通過'Lindent'運行你的代碼,這樣它更清晰,但我不會打擾,直到我發現是否是一個簡單的粘貼錯誤,或者你實際上連接到一個按鈕點擊一個套接字處理程序。 – sarnold
只是好奇,這是爲FRC? – MrZander