简体   繁体   中英

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

Did you look atSensorManager ?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM