2011. 3. 17. 17:24 안드로이드

긁어온 데이타  http://www.clingmarks.com/?p=25

// Need to implement SensorListener
public class ShakeActivity extends Activity implements SensorListener {
    // For shake motion detection.
    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 = 800;
 
    protected 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();
    }
 
    public void onAccuracyChanged(int arg0, int arg1) {
	// TODO Auto-generated method stub
    }
 
    public void onSensorChanged(int sensor, float[] values) {
	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!
		}
		last_x = x;
		last_y = y;
		last_z = z;
	    }
	}
    }
}

'안드로이드' 카테고리의 다른 글

Alert 다이얼로그  (0) 2011.04.21
Intent  (0) 2011.04.21
이펙트 사운드 다운로드 사이트  (0) 2010.12.21
adb failed  (0) 2010.12.20
안드로이드 canvas두개쓰기  (0) 2010.10.25
posted by 욱이다