2

我尝试创建单独的加速度计类来保存加速度计的值,并且我可以随时从任何其他类中访问它们,但它似乎不起作用。

这是我的加速度计课程:

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;

public class Accelero implements SensorEventListener {

    private float xAxis;
    private float yAxis;
    private float zAxis;

    public float getX(){
        return this.xAxis;
    }

    public float getY(){
        return this.yAxis;
    }

    public float getZ(){
        return this.zAxis;
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    public void onSensorChanged(SensorEvent event) {
            xAxis = event.values[0];
            yAxis = event.values[1];
            zAxis = event.values[2];
    }

}

如您所见,我正在尝试通过get*();我创建的方法获取值。但似乎总是要回来0, 0, 0。知道我的代码有什么问题吗?

这是使用它的活动:

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.EditText;

public class MainActivity extends Activity {

    private EditText output;
    private static int DIS = 1;
    Accelero acc;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        output =(EditText) findViewById(R.id.editText1);
        handler.sendEmptyMessageDelayed(DIS, 1);
        acc = new Accelero(this);

    }

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == DIS){
                refresh();
                handler.sendEmptyMessageDelayed(DIS, 1);     
            }
        }
    };

    public void refresh(){
        output.setText("X:"+acc.getX()+"\nY:"+acc.getY()+"\nZ:"+acc.getZ());
    }
}

我在清单中也有这个前提:

<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />
4

2 回答 2

1

您需要在活动中将传感器侦听器注册到传感器管理器

public class Main extends Activity implements SensorEventListener {
    private float mLastX, mLastY, mLastZ;
    private boolean mInitialized;

    private SensorManager mSensorManager;

    private Sensor mAccelerometer;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mSensorManager.registerListener(new Accelero(), mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }
}

有关详细教程,请参见此处(代码取自本教程)

于 2012-08-12T12:18:29.243 回答
1

您必须注册您正在使用的传感器,在本例中为加速度计。我猜你想要所有与“Accelero”类中的加速度计相关的东西。

要从该类注册侦听器,您必须传递对您的活动的引用。所以你的构造函数应该是这样的:

SensorManager manager;
Sensor accelerometer;
Activity foo;

public Accelero(Activity foo) {
    this.foo = foo;
    manager = (SensorManager) this.foo.getSystemService(Context.SENSOR_SERVICE);
    accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
    manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
 }

当您创建一个Accelero对象时,您只需将this其作为参数传递。

我希望这有帮助。

于 2012-08-12T12:26:13.490 回答