gpt4 book ai didi

Android SensorEventListener 语法

转载 作者:太空狗 更新时间:2023-10-29 13:42:24 24 4
gpt4 key购买 nike

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



}

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

}

感谢您的帮助。这是我的引用:reference project

最佳答案

就像它说的,你错过了;

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

关于Android SensorEventListener 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4128660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com