I'm trying to figure out how long the volume up and volume down buttons are pressed, but so far, all I've found is this answer here: Android - Getting volume button long clicks
I can't find anything less abstract than this so I have no idea how to go about implementing it; could someone point me to an example? Any help would be greatly appreciated.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event){
int action = event.getAction();
if(keyCode==KeyEvent.KEYCODE_VOLUME_UP){
if (action == KeyEvent.ACTION_DOWN) {
volUpPressed = System.currentTimeMillis();
}
else if (action == KeyEvent.ACTION_UP) {
totalTimePressed = System.currentTimeMillis() - volUpPressed;
}
}
return true;
}
Related
In an Android app, I have a feature to record audio. The idea is to have a button that has 2 types of actions.
I can click on button and start recording, and when I click again it stops recording.
I can hold the button and while its being held the app records, when I release it, the app stops recording.
I tried with a OnTouchListener
private static int CLICK_ACTION_THRESHHOLD = 250;
public long lastTouchDown;
public boolean isClick;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()){
case R.id.main_record_button:
case R.id.main_record2:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
lastTouchDown = System.currentTimeMillis();
//... do stuff
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
if (System.currentTimeMillis() - lastTouchDown < CLICK_ACTION_THRESHHOLD) {
isClick = true;
//...do other stuff
}
Whats the best way to achieve this ?
OnTouchListener is not very intuitive to work with.
I have used GestureDetector to track userbehavior. I think that is what you should use. Check out the implementation and callbacks here: https://developer.android.com/training/gestures/detector.html
You should go through this library to understand whats he is doing or you can use it too.
Also the this Thread.
I am currently trying to detect an ongoing touch event in my Android app.
In detail I want to recognize within a fragment whether the user touches any part of the app's screen.
Android's OnTouchListener works as expected except if the touch event lasts longer than a few seconds without moving.
For example the first 1-2 seconds of touch are being detected but everything after won't.
Is there something like an "OnPressListener" or a workaround?
If it's a well defined gesture you are trying to detect, have a look at GestureDetector.
http://developer.android.com/reference/android/view/GestureDetector.html
You can use
aButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
Toast.makeText(getApplicationContext(), "Long Clicked " ,
Toast.LENGTH_SHORT).show();
return true; // <- set to true
}
});
on your aButton and if you are using API < 19 you have to add
android:longClickable="true"
Attribute to your aButton in layout xml.
I finally found it out.
The solution is to use a simple OnTouchListener:
private boolean pressed = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
pressed = true;
} else if ((action == MotionEvent.ACTION_UP)
|| (action == MotionEvent.ACTION_CANCEL)) {
pressed = false;
}
return true;
}
I need to create a service that would be running in the background, displaying an icon in the notification bar, listen to the volume buttons being presses and prevent them from changing the volume, so that annoying beep won't be heard.
What do you think, is that possible? And if yes, where should I start?
Thanks!
edit:
found this snippet that works great for an activity, now will have to try it with a service.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
Toast.makeText(this, "UP", 1500).show();
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
Toast.makeText(this, "DOWN", 1500).show();
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
edit2:
found a nice tutorial over here http://android.kgmoney.net/2010/05/08/creating-a-simple-android-service-for-background-processing/
created a service, but the above code doesn't seem to fit into it... hmm...
What do you think, is that possible?
No. You cannot listen for volume button presses in a service.
I have a button widget. I want to play sound when the button is pressed and when the user releases the button (takes his finger off the button) the audio playback should be stopped. I have used the following code but it doesn't work.
public boolean onTouch(View v, MotionEvent me) {
int action = me.getAction();
if(action == MotionEvent.ACTION_DOWN) {
playSound();
} else if (action == MotionEvent.ACTION_UP) {
stopSound();
}
return false;
}
Is stopSound() being called? I think you need to return true in the action == MotionEvent.ACTION_DOWN block to tell the system that you handled the event.
Im using seek bar to control volume of the device. Im able to change the volume of the device using thumb of the seek bar just by dragging it on touch pad.
But when user presses volume(side) keys i need to set seek bar thumb position accordingly.
Hw i can do this please let me know
Thanks
I got solution, by overriding onkeydown event.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
{
int index = seekbar.getProgress();
seekbar.setProgress(index + 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
int index = seekbar.getProgress();
seekbar.setProgress(index - 1);
return true;
}
return super.onKeyDown(keyCode, event);
}
Edited for further reference