Android SensorEventListener语法

时间:2010-11-08 22:19:14

标签: android instantiation sensor

我准备对此感到羞辱,但我不确定是什么问题。我正在玩Android开发,我只是制作一个简单的应用程序,将加速度计打印到TextView。

创建SensorEventListener时,出现语法错误

(说;需要完成陈述)

有问题的确切代码:

    private SensorEventListener listener = new SensorEventListener() {
    public void onSensorChanged(SensorEvent e) {
        if (e.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
            xAxis = e.values[0];
            yAxis = e.values[1];
            zAxis = e.values[2];


            if (xThreshold > xAxis || yThreshold > yAxis || zThreshold > zAxis) {
                alertUser(values);
            }
            else {
                nonAlert(values);
            }
        }
    }
}

如果需要,这里是完整的代码

    import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class TiltMeter {
    private SensorManager mgr = null;
    private double xAxis = 0;
    private double yAxis = 0;
    private double zAxis = 0;
    private double xThreshold = 0;
    private double yThreshold = 0;
    private double zThreshold = 0;

    public TiltMeter(Context context, double xThreshold, double yThreshold, double zThreshold, TiltMeter.Callback cb) {
        this.xThreshold = xThreshold;
        this.yThreshold = yThreshold;
        this.zThreshold = zThreshold;


        mgr = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
        mgr.registerListener(listener, mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
    }

    private SensorEventListener listener = new SensorEventListener() {
        public void onSensorChanged(SensorEvent e) {
            if (e.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
                xAxis = e.values[0];
                yAxis = e.values[1];
                zAxis = e.values[2];


                if (xThreshold > xAxis || yThreshold > yAxis || zThreshold > zAxis) {
                    alertUser(values);
                }
                else {
                    nonAlert(values);
                }
            }
        }
    }

    public void close() {
        mgr.unregisterListener(listener);
    }

    public interface Callback {
        double[] alertUser(double[] data);
        double[] nonAlert(double[] data);
    }



}

这是活动:

   import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TiltMeterActivity extends Activity  
    implements TiltMeter.Callback {
    /** Called when the activity is first created. */

    TiltMeter tiltMeter = null;

    TextView tvXAxis;
    TextView tvYAxis;
    TextView tvZAxis;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        tvXAxis = (TextView)findViewById(R.id.txtXAxisValue);
        tvYAxis = (TextView)findViewById(R.id.txtYAxisValue);
        tvZAxis = (TextView)findViewById(R.id.txtZAxisValue);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        tiltMeter.close();
    }


    public double[] alertUser(double[] data) {
        tvXAxis.setText(Double.toString(data[0]));
        tvYAxis.setText(Double.toString(data[1]));
        tvZAxis.setText(Double.toString(data[3]));
        return null;
    }

    public double[] nonAlert(double[] data) {
        tvXAxis.setText(Double.toString(data[0]));
        tvYAxis.setText(Double.toString(data[1]));
        tvZAxis.setText(Double.toString(data[3]));
        return null;
    }

}

感谢您的帮助。这是我的参考:reference project

1 个答案:

答案 0 :(得分:2)

就像它说的那样,你错过了一个;

private SensorEventListener listener = new SensorEventListener() {
   public void onSensorChanged(SensorEvent e) {
     if (e.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
        xAxis = e.values[0];
        yAxis = e.values[1];
        zAxis = e.values[2];


        if (xThreshold > xAxis || yThreshold > yAxis || zThreshold > zAxis) {
            alertUser(values);
        }
        else {
            nonAlert(values);
        }
     }
   }
};