onSensorChanged Android example

Sensor, getSystemService(), getSensorList(), getDefaultDisplay(), WindowManager,
Surface.ROTATION_0, onAccuracyChanged() Android sample.

public class _MotionActivity extends Activity implements SensorEventListener {
    private float mSensorX;
    private float mSensorY;
    private Display mDisplay;
    private SensorManager sm;
    private PowerManager mPowerManager;
    private WindowManager mWindowManager;
 
    TextView textview;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get an instance of the SensorManager
        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0){
        	Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        	sm.registerListener(this,s, SensorManager.SENSOR_DELAY_NORMAL);
        }
 
        // Get an instance of the PowerManager
        mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
 
        // Get an instance of the WindowManager
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mDisplay = mWindowManager.getDefaultDisplay();
 
 
 
        setContentView(R.layout.main);
        textview = (TextView)findViewById(R.id.textView1);
    }
 
    @Override
    public void onSensorChanged(SensorEvent event) {
 
 
        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
            return;
 
        switch (mDisplay.getRotation()) {
            case Surface.ROTATION_0:
                mSensorX = event.values[0];
                mSensorY = event.values[1];
                textview.setText(String.valueOf( mSensorX));
                break;
            case Surface.ROTATION_90:
                mSensorX = -event.values[1];
                mSensorY = event.values[0];
                textview.setText(String.valueOf( mSensorX));
                break;
            case Surface.ROTATION_180:
                mSensorX = -event.values[0];
                mSensorY = -event.values[1];
                textview.setText(String.valueOf( mSensorX));
                break;
            case Surface.ROTATION_270:
                mSensorX = event.values[1];
                mSensorY = -event.values[0];
                textview.setText(String.valueOf( mSensorX));
                break;
        }
 
    }
 
	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
 
	}
	@Override
	protected void onResume() {
		super.onResume();
 
             if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0){
        	Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        	sm.registerListener(this,s, SensorManager.SENSOR_DELAY_NORMAL);
             }
	}
 
	@Override
	protected void onPause() {
 
		sm.unregisterListener(this);
		super.onStop();
	}       
 
}
This entry was posted in Android Examples Code and tagged , , . Bookmark the permalink.


Comments are closed.

23,176 views