private SensorManager sensorManager = null;
private SensorEventListener mListener = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mListener = new SensorEventListener()
{
@Override
public void onSensorChanged(SensorEvent event)
{
// TODO Auto-generated method stub
float[] values = event.values;
float x = values[0];
//float y = values[1];
float z = values[2];
int medumValue = 15;
//一般摇一摇动作是其位置在空间的X轴和Z轴上的变动
if (Math.abs(x) > medumValue /*&& Math.abs(y) > medumValue*/ && Math.abs(z) > medumValue)
{
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
// TODO Auto-generated method stub
}
};
}
@Override
protected void onResume()
{
super.onResume();
//注册监听器
if(sensorManager!= null)
sensorManager.registerListener(mListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),sensorManager.SENSOR_DELAY_GAME );
}
@Override
protected void onStop()
{
super.onStop();
//取消监听器
sensorManager.unregisterListener(mListener);
}
转载于:https://my.oschina.net/u/2364449/blog/418911