摇晃不工作超过一定的价值
Posted
技术标签:
【中文标题】摇晃不工作超过一定的价值【英文标题】:shake not working beyond certain value 【发布时间】:2012-09-19 05:04:18 【问题描述】:我想测量一次大震动(例如:测量自行车或汽车发生事故时的情况。我想测量手机震动)。当我的手机摇晃时,我有播放音频的代码。但是当我将 SHAKE_THRESHOLD 值更改为高水平时,它不起作用。 是否可以测量大震动。
我的代码:
public class ShakeActivity extends Activity implements SensorListener
// For shake motion detection.
private MediaPlayer mPlayer1;
private SensorManager sensorMgr;
private long lastUpdate = -1;
private float x, y, z;
private float last_x, last_y, last_z;
private static final int SHAKE_THRESHOLD = 8000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
// other initializations
// start motion detection
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
boolean accelSupported = sensorMgr.registerListener(this,
SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_GAME);
if (!accelSupported)
// on accelerometer on this device
sensorMgr.unregisterListener(this,
SensorManager.SENSOR_ACCELEROMETER);
protected void onPause()
if (sensorMgr != null)
sensorMgr.unregisterListener(this,
SensorManager.SENSOR_ACCELEROMETER);
sensorMgr = null;
super.onPause();
@Override
public void onAccuracyChanged(int arg0, int arg1)
// TODO Auto-generated method stub
@Override
public void onSensorChanged(int sensor, float[] values)
// TODO Auto-generated method stub
if (sensor == SensorManager.SENSOR_ACCELEROMETER)
long curTime = System.currentTimeMillis();
// only allow one update every 100ms.
if ((curTime - lastUpdate)> 100)
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;
x = values[SensorManager.DATA_X];
y = values[SensorManager.DATA_Y];
z = values[SensorManager.DATA_Z];
float speed = Math.abs(x+y+z - last_x - last_y - last_z)
/ diffTime * 10000;
if (speed > SHAKE_THRESHOLD)
// yes, this is a shake action! Do something about it!
MediaPlayer mPlayer1 = MediaPlayer.create(this, R.raw.hello);
try
mPlayer1.prepare();
catch (IllegalStateException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
mPlayer1.start();
last_x = x;
last_y = y;
last_z = z;
【问题讨论】:
【参考方案1】:转到 API 演示 应用程序 -> 操作系统 -> 传感器那里有一个图表。摇晃您的设备,看看哪个传感器显示您的设备变化更好并满足您的需求。
【讨论】:
以上是关于摇晃不工作超过一定的价值的主要内容,如果未能解决你的问题,请参考以下文章
2022-07-23:给定N件物品,每个物品有重量(w[i])有价值(v[i]), 只能最多选两件商品,重量不超过bag,返回价值最大能是多少? N <= 10^5, w[i] <= 10^5, v