Android SensorEventListener 语法

发布于 2024-10-01 02:04:30 字数 3962 浏览 8 评论 0原文

我已经做好了被羞辱的准备,但我不知道哪里出了问题。我只是在玩 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;
    }

}

感谢您的帮助。这是我的参考: 参考项目

I am prepared to be humiliated on this, but I am not sure what is wrong. I am just playing around with Android development and am just making a simple application that will print out accelerometer to a TextView.

When creating the SensorEventListener, I get a syntax error

(says ; is needed to finish statement)

exact code in question:

    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);
            }
        }
    }
}

here is full code if needed

    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);
    }



}

and here is the activity:

   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;
    }

}

thanks for any help. this is my reference: reference project

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扬花落满肩 2024-10-08 02:04:30

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

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);
        }
     }
   }
};

Like it says, you are missing a ;

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);
        }
     }
   }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文