我们正在尝试构建一个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);
}
};
答案 0 :(得分:0)
据我所知,除了按中心键外,没有任何代码能够一劳永逸地删除回调。在mUpdateTask结束时,回调会再次将自身注入Handler队列,这解释了重复。
如果您希望能够在按下按钮时发送事件,则可以使用OnTouchListener作为按钮,因为它可以对按下和释放做出反应。 OnClickListener只能对按下+释放做出反应。