简体   繁体   English

如何检测设备的运动?

[英]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.我想检测像 Moo Box 这样的运动,我将手机倒转,当我把它转回来时,它会触发一个动作……对于 Android。 What is the best way什么是最好的方法

It is possible to custom an event listenner可以自定义事件监听器

Did you look atSensorManager ?你看过SensorManager吗?

There's a very good tutorial here: http://www.ibm.com/developerworks/opensource/library/os-android-sensor/这里有一个很好的教程: 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: SensorEventListener 是一个interface ,而不是class ,因此您必须在两个回调中实现您的代码:

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.收到更改后,传感器值将显示在 TextView 小部件的屏幕上。 Figure 1 shows the application in action.图 1 显示了正在运行的应用程序。

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.您想搜索使用 Android 加速度计的示例。 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.是一个起点,但它侧重于检测设备的抖动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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