オンボードの加速度計を使用するアプリケーションを作成しようとしています。
センサーをセットアップしているときに、TYPE_MAGNETIC_FIELD を使用すると、「TYPE_MAGNETIC_FIELD を変数に解決できません」というエラー メッセージが表示されます。TYPE_ACCELERATOR は問題なく動作します。
これが私のコードです:
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sensor);
    sensMan = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    magFieldSens = sensMan.getDefaultSensor(TYPE_MAGNETIC_FIELD);
    accelerometer = sensMan.getDefaultSensor(TYPE_ACCELEROMETER);
    sensListen = new MySensorEventListener();
    orientationView = (TextView) findViewById(R.id.orientationView);
}
これらは私のメインアクティビティで定義されています:
SensorManager sensMan;
Sensor accelerometer, magFieldSens;
SensorEventListener sensListen;
    TextView orientationView;
このクラスでは、TYPE_MAGNETIC_FIELD を期待どおりに使用できます。
    class MySensorEventListener implements SensorEventListener
{
    /* 
     * @see android.hardware.SensorEventListener#onAccuracyChanged(android.hardware.Sensor, int)
     */
    @Override
    public void onAccuracyChanged(Sensor sensor, int acc)
    {
        // Edit this method, macke
        if(acc <= 1)
            Toast.makeText(SensorActivity.this, "Shake in a figure eight pattern ", Toast.LENGTH_LONG).show();
    }
    @Override
    public void onSensorChanged(SensorEvent sensorEvent)
    {
        int sensorEventType = sensorEvent.sensor.getType();
        if (sensorEventType == Sensor.TYPE_ACCELEROMETER)
            System.arraycopy(sensorEvent.values, 0, gravVals, 0, 3);
        else if(sensorEventType == Sensor.TYPE_MAGNETIC_FIELD)
            System.arraycopy(sensorEvent.values, 0, geoMagnetVals, 0, 3);
        else
            return;
        if(SensorManager.getRotationMatrix(rotMatrix, null, gravVals, geoMagnetVals))
        {
            SensorManager.getOrientation(rotMatrix, orientation);
            orientationView.setText("X: " + orientation[0] + 
                                    "\nY: " + orientation[1] + 
                                    "\nZ: " + orientation[2]);
        }
    }
}
乾杯/M