tags:

views:

28

answers:

2
public void onSensorChanged(SensorEvent e) {
    if (e.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {

        if(e.values[0] >= 8)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        else
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

When the method is called setRequestedOrientation() - error. Create layout-land and placed there a copy of the main.xml file.

A: 

try this::

SensorManager sensorMgr;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensorMgr.registerListener(this,
            SensorManager.SENSOR_ACCELEROMETER,
            SensorManager.SENSOR_DELAY_GAME);
}


public void onSensorChanged(int sensor, float[] values){
    if (sensor==Sensor.TYPE_ACCELEROMETER) {
        if(values[0] >= 8)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        else
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}
Jorgesys
A: 

Thanks to all the problem was solved