如何在摇动设备时刷新应用程序?
我需要添加一个摇动功能来刷新我的 Android 应用程序。
我找到的所有文档都涉及实现 SensorListener
,但 Eclipse 告诉我它已被弃用并建议使用 SensorEventListener
。
有人对我如何创建这个摇动控制器
有很好的指导吗?
I need to add a shake feature that will refresh my Android application.
All I find of documentation involves implementing the SensorListener
, but Eclipse tells me it's deprecated and suggest SensorEventListener
.
Anybody that has a nice guide to how I go about creating this shake controller
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
您应该作为
SensorEventListener
进行订阅,并获取加速度计
数据。一旦获得它,您应该监视某个轴上加速度方向(符号)的突然变化。这将是设备
“摇动”
移动的良好指示。You should subscribe as a
SensorEventListener
, and get theaccelerometer
data.Once you have it, you should monitor for sudden change in direction (sign) of acceleration on a certain axis. It would be a good indication for the
'shake'
movement of device.和我一起工作v.good
参考
Working with me v.good
Reference
您可能想尝试开源 tinybus。有了它,震动检测就这么简单。
它使用 seismic 进行震动检测。
You might want to try open source tinybus. With it shake detection is as easy as this.
It uses seismic for shake detection.
我修改了@peceps的答案并制作了它的kotlin版本。我还添加了 LifecycleOwner 参数以使其具有生命周期感知能力。
在您的活动中,添加以下代码以使其检测摇动事件:
I modify @peceps's answer and make kotlin version of it. And I also add LifecycleOwner parameter to make it lifecycle-aware.
In your activity, add this code to make it detecting shake event:
这是一个示例代码。
将其放入您的活动类中:
并将其添加到您的 onCreate 方法中:
然后,您可以在应用程序中的任何位置询问“mAccel”当前加速度,该加速度与轴无关,并且不受重力等静态加速度的影响。
大约是。如果没有移动,则为 0;如果设备摇晃,则为 >2。
基于评论 - 对此进行测试:
注意:
应在 onPause 停用加速度计并激活 onResume 以节省资源(CPU、电池)。
该代码假设我们位于地球上;-) 并初始化地球重力加速度。否则,当应用程序启动并从自由落体“撞击”地面时,您会感到强烈的“震动”。然而,由于低切滤波器,代码会适应引力,一旦初始化,它也可以在其他行星或自由空间中工作。
(你永远不知道你的应用程序将使用多长时间......;-)
Here is an example code.
Put this into your activity class:
And add this to your onCreate method:
You can then ask "mAccel" wherever you want in your application for the current acceleration, independent from the axis and cleaned from static acceleration such as gravity.
It will be approx. 0 if there is no movement, and, lets say >2 if the device is shaked.
Based on the comments - to test this:
Notes:
The accelometer should be deactivated onPause and activated onResume to save resources (CPU, Battery).
The code assumes we are on planet Earth ;-) and initializes the acceleration to earth gravity. Otherwise you would get a strong "shake" when the application starts and "hits" the ground from free-fall. However, the code gets used to the gravitation due to the low-cut filter and would work also on other planets or in free space, once it is initialized.
(you never know how long your application will be in use...;-)
这是我的摇动手势检测代码:
在您的活动中添加此代码:
...
在 onCreate() 添加:
和:
Here is my code for shake gesture detection:
Add this in your activity:
...
in onCreate() add:
and:
这是另一个基于此处的一些技巧以及 Android 开发人员网站中的代码构建的实现。
MainActivity.java
ShakeDetector.java
Here's yet another implementation that builds on some of the tips in here as well as the code from the Android developer site.
MainActivity.java
ShakeDetector.java
我真的很喜欢 Peterdk 的回答。我亲自对他的代码进行了一些调整。
文件:ShakeDetector.java
另外,不要忘记您需要向 SensorManager 注册 ShakeDetector 的实例。
I really liked Peterdk's answer. I took it upon myself to make a coulpe of tweaks to his code .
file: ShakeDetector.java
Also, don't forget that you need to register an instance of the ShakeDetector with the SensorManager.
我正在为我的大学项目开发一个运动检测和震动检测应用程序。
除了应用程序的原始目标之外,我还将库部分(负责运动和震动检测)从应用程序中分离出来。该代码是免费的,可在 SourceForge 上使用,项目名称为“BenderCatch”。我正在制作的文档将于九月中旬左右准备就绪。
http://sf.net/projects/bendercatch
它使用更精确的方式来检测抖动:同时观看当您摇动时,SensorEvents 之间的力差以及 X 轴和 Y 轴上存在的振荡。它甚至可以在每次摇晃时发出声音(或振动)。
请随时通过电子邮件向我询问更多信息:raffaele [at] terzigno [dot] com
I am developing a motion-detection and shake-detection app for my university project.
Besides the original target of the application, I am splitting the library part (responsible for motion and shake detection) from the app. The code is free, available on SourceForge with the project name "BenderCatch". Documentation I am producing will be ready around mid-september.
http://sf.net/projects/bendercatch
It uses a more precise way to detect shake: watches BOTH the difference of force between SensorEvents AND the oscillations present in X and Y axis when you perform a shake. It can even make a sound (or vibrate) on each oscillation of the shake.
Feel free to ask me more by e-mail at raffaele [at] terzigno [dot] com
我编写了一个小示例,用于检测垂直和水平抖动并显示
Toast
。I have written a small example for detecting vertical and horizontal shakes and showing a
Toast
.您可以使用seismic。可以找到一个示例
You can use seismic. An example can be found here.
我尝试了几种实现,但想分享我自己的。
它使用
G-force
作为阈值计算的单位。它使人们更容易理解正在发生的事情,并且还可以设置一个好的阈值。它只是记录 G 力的增加,并在超过阈值时触发侦听器。它不使用任何方向阈值,因为如果您只想记录良好的震动,则实际上并不需要它。
当然,您需要在
Activity
中对该侦听器进行标准注册和取消注册。另外,要检查您需要什么阈值,我推荐以下应用< /a> (我没有以任何方式连接到该应用程序)
I have tried several implementations, but would like to share my own.
It uses
G-force
as unit for the threshold calculation. It makes it a bit easier to understand what is going on, and also with setting a good threshold.It simply registers a increase in G force and triggers the listener if it exceeds the threshold. It doesn't use any direction thresholds, cause you don't really need that if you just want to register a good shake.
Of-course you need the standard registering and UN-registering of this listener in the
Activity
.Also, to check what threshold you need, I recommend the following app (I am not in any way connected to that app)
这是另一个代码:
Here is another code for this:
Shaker.java
MainActivity.java
玩得开心。
Shaker.java
MainActivity.java
Have fun.