• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

how to run code when button is released

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i made a remote for my soundbar and want to keep incresing the vol when button is pressed and stop when it is released the code i am trying to do this with is


when i press down the code works but when i release the button the volume keeps going down to zero so action up is not working how do i fix this thanks
volcontdown method is:

thanks
 
Marshal
Posts: 5327
324
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does your volcontdown(boolean b) method ever exit? When called with true can b ever become false?
 
john bean
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
k so i reworte the code and removed the method but it still is not getting the action up has my head wrecked this should be simple and obvs im doing something stupid here but it never gets action up to break from loop
 
Tim Cooke
Marshal
Posts: 5327
324
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although you have removed the method you have re-implemented the same problem inline instead.

It's a tough one for me to figure out the pattern to use here as I'm not familiar with Android event listeners. What I guess is happening here is that the onTouch() method of your listener is called when the button is pressed, and then it's called again when you release the button. The problem you have is that the onTouch() method when called for ACTION_DOWN never exits which could mean the listener is stuck and is no longer listening.

You need to figure out a way to offload the volume management elsewhere so that ACTION_DOWN calls a "start volume down" function, and ACTION_UP calls a "stop volume down" function. Now the trick is that those volume start and stop methods need to return immediately to avoid blocking the listener.

It's a fun one. See how you get on.
 
Marshal
Posts: 75707
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:Although you have removed the method you have re-implemented the same problem inline instead. . . .

Actually, I think the new code is slightly worse because it uses the error‑prone construct == true.
Never write == true or == false please.
Write while (b) ... or while (!b) ... please.
 
I can't take it! You are too smart for me! Here is the tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic