Christophe Debove
Christophe Debove

Reputation: 6286

How to detect The movement of the device?

I want to detect a movement like a Moo Box, I reverse the phone and when I turn it back it fire an action... For Android. What is the best way

It is possible to custom an event listenner

Upvotes: 3

Views: 7497

Answers (2)

Aleadam
Aleadam

Reputation: 40381

Did you look at SensorManager?

There's a very good tutorial here: http://www.ibm.com/developerworks/opensource/library/os-android-sensor/

Update after question editing:

SensorEventListener is an interface, not a class, so you are forced to implement your code in the two callbacks:

abstract void   onAccuracyChanged(Sensor sensor, int accuracy) // Called when the accuracy of a sensor has changed.
abstract void   onSensorChanged(SensorEvent event) // Called when sensor values have changed. 

You will be more interested in the second one.

Please, take a look at the tutorial I posted above. Half-way through there is an accelerometer example:

Sensor example

The sample application simply monitors changes to the orientation and accelerometer sensors (see Download for the source code). When changes are received, the sensor values are displayed on the screen in TextView widgets. Figure 1 shows the application in action.

You will have to analyze the movement pattern you want to detect and create a listener that reacts accordingly.

Upvotes: 6

mah
mah

Reputation: 39797

You want to search for examples of using the Android accelerometer. This is how motion events are established and delivered to requesting apps.

Here is a starting point but it focuses on detecting shakes of the device.

Upvotes: 0

Related Questions