我正在使用特定的中间件(不重要)在Android上开发电视遥控模拟器应用程序。
在音量按钮(Volume+和Volume-)的情况下,我想做的是在按住其按钮时重复发送“音量上升”信号。
这是我最后尝试的代码(其中一个按钮的代码,另一个必须完全相同,除了名称)。
在音量按钮(Volume+和Volume-)的情况下,我想做的是在按住其按钮时重复发送“音量上升”信号。
这是我最后尝试的代码(其中一个按钮的代码,另一个必须完全相同,除了名称)。
Declared a boolean variable
boolean pressedUp = false;
Declared a inner class with the following AsyncTask:
class SendVolumeUpTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... arg0) { while(pressedUp) { SendVolumeUpSignal(); } return null; }
}
Add the listener to the button:
final Button buttonVolumeUp = (Button) findViewById(R.id.volup); buttonVolumeUp.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if(pressedUp == false){ pressedUp = true; new SendVolumeUpTask().execute(); } case MotionEvent.ACTION_UP: pressedUp = false; } return true; } });
我也尝试了使用简单的线程,例如 增加-减少计数器 但都没有成功。该应用程序在其他按钮(频道等)上运行良好,但是完全忽略了音量更改。
while(pressedUp){ SendVolumeUpSignal(); Thread.sleep(100);}
- Tas Morf