-1

I have an android service (in Java) that currently invokes a function that does things like getting geolocation coordinates when the user shakes the phone repeatedly.

However, to reduce false-positives, I would like to be able to listen to a combination of events -- a hardware button press (Volume down, for instance) along with a repeated shaking, which would then invoke the same function as above.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
chidelta
  • 7
  • 1
  • Welcome to the party! Your question is too broad. SO is about solving problems to **specific** programming questions – already implemented, yet for some reason not working code in your case. To convince people to help you, you should **show** some effort solving the question yourself. Some code is usually a good start, namely a [minimal, complete and verifiable example](http://stackoverflow.com/help/mcve). Please take the [introductory tour](http://www.stackoverflow.com/tour). – Markus W Mahlberg Apr 26 '15 at 18:45
  • You might want to read [How do I ask a good question](http://stackoverflow.com/help/how-to-ask), which enhances the probability for getting a useful answer _drastically_. You might find [ESR](https://en.m.wikipedia.org/wiki/Eric_S._Raymond)'s excellent essay [How To Ask Questions The Smart Way](http://catb.org/~esr/faqs/smart-questions.html) helpful, too. – Markus W Mahlberg Apr 26 '15 at 18:45

2 Answers2

0

You write a listener to a hardware key press (look at onKeyDown and onKeyUp). You write your shake detector. Have each of those set a boolean flag when its detected, and clear the flag when its over (for the shake you'd want to clear it either when the shake ends, or after some amount of time). Have each listener call getDataIfNeeded(). Have that function check if both flags are true, and if so do whatever it is you wanted to do.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

You can have booleans like @gabe suggested for both events and in onReceive of both of them you can check if both booleans are true do something. also create a timer that sets those booleans to false in ever one second or so. That will keep track of both event happening together.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44