/*声明变量*/
private TextView mTextView01;
private ImageView mImageView01;
private SensorManager mSensorManager01;
private float velocityFinal = 0;
/*声明媒体播放器*/
private MediaPlayer mMediaPlayer01;
private MediaPlayer mMediaPlayer02;
private MediaPlayer mMediaPlayer03;
private Date lastUpdateTime;
private boolean bIfMakeSound = true;
private int godanswer = 0;
/*声明常数*/
private final int saint=1;
private final int laugh=2;
private final int bad=3;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*取得目前系统时间*/
lastUpdateTime = new Date(System.currentTimeMillis());
/*通过findViewById构造器创建TextView与ImageView对象*/
mTextView01 = (TextView)findViewById(R.id.myTextView1);
mImageView01 = (ImageView)findViewById(R.id.myImageView1);
/*取得系统的加速度感测装备*/
mSensorManager01 =
(SensorManager)getSystemService(Context.SENSOR_SERVICE);
/*通过MediaPlayer.create()来取得raw底下音乐文件*/
mMediaPlayer01 = new MediaPlayer();
mMediaPlayer01 = MediaPlayer.create(SensorManager1.this, R.raw.saint);
mMediaPlayer02 = new MediaPlayer();
mMediaPlayer02 = MediaPlayer.create(SensorManager1.this, R.raw.laugh);
mMediaPlayer03 = new MediaPlayer();
mMediaPlayer03 = MediaPlayer.create(SensorManager1.this, R.raw.bad);
}
/*覆盖onResuem方法*/
@Override
protected void onResume()
{
// TODO Auto-generated method stub
/*注册SensorListener与设置参数*/
mSensorManager01.registerListener
(
mSensorLisener,
SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_UI
);
super.onResume();
}
/*覆盖onPause方法*/
@Override
protected void onPause()
{
/*解除SensorListener的设置*/
// TODO Auto-generated method stub
mSensorManager01.unregisterListener(mSensorLisener);
super.onPause();
}
/*创建SensorListener对象*/
private final SensorListener mSensorLisener = new SensorListener()
{
/*声明变量*/
private float previousAcceleration;
/*覆盖onSensorChanged事件来侦测手机投掷事件*/
public void onSensorChanged(int sensor, float[] values)
{
// TODO Auto-generated method stub
/*加速度是否存在*/
if(sensor == SensorManager.SENSOR_ACCELEROMETER)
{
float x = values[SensorManager.DATA_X];
float y = values[SensorManager.DATA_Y];
float z = values[SensorManager.DATA_Z];
/*手机是否落下的速度标准*/
double forceThreshHold = 1.5f;
double appliedAcceleration = 0.0f;
/* SensorManager.GRAVITY_EARTH = 9.8m/s2 */
appliedAcceleration +=
Math.pow(x/SensorManager.GRAVITY_EARTH, 2.0);
appliedAcceleration +=
Math.pow(y/SensorManager.GRAVITY_EARTH, 2.0);
appliedAcceleration +=
Math.pow(z/SensorManager.GRAVITY_EARTH, 2.0);
appliedAcceleration = Math.sqrt(appliedAcceleration);
/*判断手机使否落下的判断式
* 落地当时速度需小于1.5f, 落地前速度必须大于1.5f*/
if((appliedAcceleration < forceThreshHold) &&
(previousAcceleration > forceThreshHold))
{
/* Shake手机事件触发 */
Date timeNow = new Date(System.currentTimeMillis());
/* 从初速度到末速度所经过的时间 */
long timePeriod =
(long)timeNow.getTime() - (long)lastUpdateTime.getTime();
/* (v) = a*t */
velocityFinal =
(float) (appliedAcceleration * ((float)timePeriod/1000));
Log.i("V=",Float.toString(velocityFinal));
if(bIfMakeSound==true)
{
/*Random Number生成器*/
Random generator=new Random();
/*掷筊*/
godanswer = generator.nextInt(3) + 1;
switch (godanswer)
{
/*圣筊*/
case saint:
mTextView01.setText
(
getResources().getText(R.string.str_saint).toString()
);
mImageView01.setImageDrawable(getResources().
getDrawable(R.drawable.saint));
if (mMediaPlayer01 != null)
{
try
{
mMediaPlayer01.seekTo(0);
mMediaPlayer01.stop();
mMediaPlayer01.prepare();
mMediaPlayer01.setVolume(10, 10);
mMediaPlayer01.start();
}
catch(Exception e)
{
mTextView01.setText(e.toString());
e.printStackTrace();
}
}
else
{
try
{
mMediaPlayer01.prepare();
mMediaPlayer01.setVolume(10, 10);
mMediaPlayer01.start();
}
catch(Exception e)
{
mTextView01.setText(e.toString());
e.printStackTrace();
}
}
break;
/*笑筊*/
case laugh:
mTextView01.setText
(
getResources().getText(R.string.str_laugh).toString()
);
mImageView01.setImageDrawable(getResources().
getDrawable(R.drawable.laugh));
if (mMediaPlayer02 != null)
{
try
{
mMediaPlayer02.seekTo(0);
mMediaPlayer02.stop();
mMediaPlayer02.prepare();
mMediaPlayer02.setVolume(10, 10);
mMediaPlayer02.start();
}
catch(Exception e)
{
mTextView01.setText(e.toString());
e.printStackTrace();
}
}
else
{
try
{
mMediaPlayer02.prepare();
mMediaPlayer02.setVolume(10, 10);
mMediaPlayer02.start();
}
catch(Exception e)
{
mTextView01.setText(e.toString());
e.printStackTrace();
}
}
break;
/*阴筊*/
case bad:
mTextView01.setText
(
getResources().getText(R.string.str_bad).toString()
);
mImageView01.setImageDrawable(getResources().
getDrawable(R.drawable.bad));
if (mMediaPlayer03 != null)
{
try
{
mMediaPlayer03.seekTo(0);
mMediaPlayer03.stop();
mMediaPlayer03.prepare();
mMediaPlayer03.setVolume(10, 10);
mMediaPlayer03.start();
}
catch(Exception e)
{
mTextView01.setText(e.toString());
e.printStackTrace();
}
}
else
{
try
{
mMediaPlayer03.prepare();
mMediaPlayer03.setVolume(10, 10);
mMediaPlayer03.start();
}
catch(Exception e)
{
mTextView01.setText(e.toString());
e.printStackTrace();
}
}
break;
}
}
}
else
{
/* 末速度=0 */
/* 更新lastUpdateTime为现在 */
Date timeNow = new Date(System.currentTimeMillis());
lastUpdateTime.setTime(timeNow.getTime());
}
previousAcceleration = (float) appliedAcceleration;
}
}
public void onAccuracyChanged(int sensor, int accuracy)
{
// TODO Auto-generated method stub
}
};
}
分享到:
相关推荐
我们将深入探讨Android中的SensorListener以及如何实现摇晃检测。 首先,我们需要了解Android的传感器系统。Android系统提供了SensorManager服务,它允许开发者访问设备的各种传感器数据,如加速度、陀螺仪等。要...
private SensorListener mySensorListener = new SensorListener(){ @Override public void onAccuracyChanged(int sensor, int accuracy) {} //重写onAccuracyChanged方法 @Override public void ...
private SensorListener mySensorListener = new SensorListener(){ @Override public void onAccuracyChanged(int sensor, int accuracy) {}//重写onAccuracyChanged方法 @Override public void ...
3. `public void addSensorListener(SensorListener objSensorListener)`:添加一个监听器,用于监听设备方向的变化。这样,实现`SensorListener`接口的类可以接收到实时的设备方向更新。 下面是一个简单的使用示例...
sensorManager.registerListener(sensorListener, gravitySensor, SensorManager.SENSOR_DELAY_NORMAL); ``` 在onSensorChanged方法中,我们接收到SensorEvent对象,从中可以获取到重力传感器的X、Y、Z轴的加速度值...
sensorManager.registerListener(sensorListener, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); ``` 当不再需要传感器数据时,记得取消注册监听器: ```java sensorManager.unregisterListener...
sensorManager.registerListener(sensorListener, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); ``` 在`onSensorChanged`方法中,`SensorEvent.values`数组会返回一个包含x、y、z三个轴向的加速度值,单位...
sensorManager.registerListener(sensorListener, gravitySensor, SensorManager.SENSOR_DELAY_NORMAL); ``` 在`onSensorChanged`回调中,我们可以获取到实时的重力值,通常是三元组 `(x, y, z)`,其中每个值范围在...
sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); ``` 在上述代码中,我们注册了一个监听器来跟踪加速度计的...
这里,`sensorListener`是实现了`SensorEventListener`接口的对象,`SENSOR_DELAY_NORMAL`定义了数据更新的频率。 在`SensorEventListener`的`onSensorChanged(SensorEvent event)`方法中,我们会接收到设备在三个...
sensorManager.registerListener(sensorListener, sensors.get(0), SensorManager.SENSOR_DELAY_NORMAL); ``` 6. **权限声明** 别忘了在AndroidManifest.xml中添加使用传感器的权限: ```xml ``` 7. **释放资源...
sensorManager.registerListener(sensorListener, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); ``` 这里,sensorListener是你实现的SensorEventListener接口,SENSOR_DELAY_NORMAL决定了数据更新的频率...
sensorManager.registerListener(sensorListener, gravitySensor, SensorManager.SENSOR_DELAY_NORMAL); ``` 在这个示例中,`Sensor.TYPE_GRAVITY`代表重力传感器,`onSensorChanged`方法会在传感器数据变化时被...
sensorManager.registerListener(sensorListener, sensor, SensorManager.SENSOR_DELAY_NORMAL); ``` 别忘了在不再需要监听传感器时,应该注销监听器以节省资源: ```java sensorManager.unregisterListener...
sensorManager.registerListener(sensorListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { super.onPause(); sensorManager.unregisterListener...
sensorManager.registerListener(sensorListener, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); ``` 这里的SensorEventListener是一个接口,你需要实现它的onSensorChanged方法,该方法会在传感器数据发生...
sensorManager.registerListener(sensorListener, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { super.onPause(); sensorManager.unregisterListener...
mySensorListener, //监听器SensorListener对象 SensorManager.SENSOR_TEMPERATURE,//传感器的类型为温度 SensorManager.SENSOR_DELAY_UI//传感器事件传递的频度 ); super.onResume();
在这里,`sensorListener`是一个实现了SensorEventListener接口的实例,该接口有两个关键方法:`onSensorChanged(SensorEvent event)`和`onAccuracyChanged(Sensor sensor, int accuracy)`。`onSensorChanged`会在...
sensorManager.registerListener(sensorListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL); ``` 在`onSensorChanged()`方法中,你可以访问到实时的加速度值,这些值表示在X、Y、Z轴上的加速度。...