Android watch is not detecting touchevents on GridViewPager - java

I tried to implement a DismissOverlay for my Android Watch application, but it didn't work. So I tried to tear the problem down and implemented the most basic thing related to touch gestures I was aware about :
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
switch (action) {
case (MotionEvent.ACTION_DOWN):
Log.d(DEBUG_TAG, "Action was DOWN");
return true;
case (MotionEvent.ACTION_MOVE):
Log.d(DEBUG_TAG, "Action was MOVE");
return true;
case (MotionEvent.ACTION_UP):
Log.d(DEBUG_TAG, "Action was UP");
return true;
case (MotionEvent.ACTION_CANCEL):
Log.d(DEBUG_TAG, "Action was CANCEL");
return true;
case (MotionEvent.ACTION_OUTSIDE):
Log.d(DEBUG_TAG, "Movement occurred outside bounds " +
"of current screen element");
return true;
default:
return super.onTouchEvent(event);
}
}
But this dooesn't recognize anything.
So I'm curious if there is something simple I did wrong, or some special stuff related to the watch implementation I have to take care about?

The problem in my case was, that I had a GridViewPager and I tried to get the touchEvents directly from that. But you actually have to implement the touch stuff for the GridViewPagerItem.

Related

Android studio java onKeyUp getting called on down, not release

I'm trying to code simple wasd controls in android studio. I was trying to use onKeyDown and onKeyUp to move my character while the key is pressed by having onKeyDown set a boolean for my movement to true, and then having onKeyUp set it to false. Right now though when I press and hold a key they both just get called over and over, and nothing happens when I release the key. I know onKeyDown is supposed to behave that way. But why is onKeyUp doing the same thing? Here's an example of my simplified onKeyUp function I'm using for testing:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.d(TAG, "onKeyUp: keycode=" + keyCode + ", event=" + event);
switch (keyCode) {
case KeyEvent.KEYCODE_A:
Log.d(TAG, "up key a");
break;
case KeyEvent.KEYCODE_D:
Log.d(TAG, "up key d");
break;
default:
return super.onKeyUp(keyCode, event);
}
return true;
}
How do I catch when the key is actually released?

Listening to key press events blocks the keys from working

I'm new to android and I'm making an app that will listen for a consecutive volume up and volume down keypress and vibrate a pattern.
I've tried using dispatchKeyEvent() and it detects volume up and down fine but it blocks the user from changing the volume.
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
System.out.println("UP"); // I know i can use Log but this is quicker to type
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
System.out.println("DOWN");
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
Additionally I'd like to be able to do this when the screen is off (and app in background) but aparently services can't interact with key events.
I've found very conflicting answers all over the place; an app I used before played a sound when it was in the background, screen turned off and the power button was pressed 3 times in short succession, however many questions here have answers akin to "not possible".
I wouldn't mind using power button instead of volume keys (infact both would be nice) but that seems harder to implement.
TLDR:
How to stop listeners blocking the keys they're listening.
2. How to run this in the background.
Any help would be greatly appreciated!
Edit: regarding no2 I found a way to do this with power button by registering a broadcast receiver in a service and listening to screen off and screen on intents.
overriding the onKeyDown() of Activity is easier; because it's always KeyEvent.ACTION_DOWN:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP: {
System.out.println("UP");
}
case KeyEvent.KEYCODE_VOLUME_DOWN: {
System.out.println("DOWN");
}
}
return super.onKeyDown(keyCode, event);
}
How to stop listeners blocking the keys they're listening.
As you could see from android docs, if you don't need to block your keys, just call super.dispatchKeyEvent(event); otherwise if you return true; it means that you consumed event, nobody will know about event.
How to run this in the background.
I think, there is impossible. Android has service, which could do background job, but this not way to handle system button clicks. Maybe this topic will help you.

motionEvents didn't work Android Java

i can't get message in Logcat from events : MotionEvent.ACTION_MOVE and MotionEvent.ACTION_UP. I couldn't find solution for it so i ask it here.
I have this overrided method in my class:
#Override
public boolean onTouchEvent(MotionEvent event){
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_MOVE:
Log.d("action","Action was MOVE");
break;
case MotionEvent.ACTION_DOWN:
Log.d("action","Action was DOWN");
break;
case MotionEvent.ACTION_UP:
Log.d("action","Action was UP");
break;
default:
break;
}
}
return super.onTouchEvent(event);
}
And in LogCat i can see just message from
MotionEvent.ACTION_DOWN : "action" "Action was DOWN" . No messages from other actions.
I try it in my phone and in AVD.
Thanks for any response.
Answer:
return must be set to true and not for super constructor.
Just Set return to True and not for super constructor :)

How to disable android hiding an activity on keycode_button_... events?

I'm using a rooted tabled with an USB port in host mode to read the value of an xbox-controller-joystick (with onJoystickMotion).
Now, I also want to track the buttons keycode_button_a, keycode_button_b, keycode_button_x, keycode_button_y and keycode_button_select. The tracking is working, but pressing these buttons will hide the activity.
Is there any way to disable android hiding an activity on this keycode_button_... events?
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
InputDeviceState state = getInputDeviceState(event);
if (state != null) {
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
if (state.onKeyDown(event)) {
//Do my thing
}
break;
case KeyEvent.ACTION_UP:
if (state.onKeyUp(event)) {
//Do my thing
}
break;
}
return true;
}
return super.dispatchKeyEvent(event);
}
Yeah :D

issue with if/else and switch statements

the following code is to handle ACTION_DOWN and ACTION_UP events for a button named clash. the idea is that once if/else determines that the onTouch event was caused by clash and the switch statement then determines what to do based on the action. i don't know if the problem is that the switch statements are not returning true and that may be related to the problem. when i add a return, eclipse says that the code is unreachable which i don't understand. i was under the impression that you can't break out of a switch without break.
what's actually happening is that the first sound will loop but the code never seems to detect the action up when the button is released and so the sound plays forever. any help would be appreciated.
public boolean onTouch(View v, MotionEvent event) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.clash);
if (v.getId() == R.id.clash){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
mp.pause();
break;
}
}
return true;
}
});
//Add the following line in the code before your setOnTouchListener()
MediaPlayer mp;
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.clash){
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp = MediaPlayer.create(getBaseContext(), R.raw.clash);
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
if(mp != null)
{
mp.pause();
mp.release();
}
break;
}
}
}
// I'm assuming this was from the onTouchListener()? -> });
Just a thought.

Categories

Resources