我正在开发一个Android应用程序,该应用程序的主要活动是带有一些用于显示传感器数据(读取和显示加速度和Giro数据)的文本视图。在onCreate方法中,我正在调用一个称为SensorModule的常规Java调用,并且我将contect作为此类的参数。
SensorModule类实现用于侦听传感器事件的SensorEventListener。该对象是在主要活动onCreate方法中创建的(我有一些打印件,并且在Logcat中可以看到它们)。
但是没有调用onSensorChanged方法。 (当我将SensorModule代码放入主要活动中时,它工作正常)。
我认为我在初始化SensorModule及其sensorEventlistener时遇到问题,但我不知道可能是什么问题。有人有主意吗?
下面我在主要活动的onCreate方法中初始化SensorModule:
SensorM = new SensorModule(this);
这是SensorClass,其中我认为出了点问题,SensorOnChange中的打印未在LogCat中打印:
package com.example.SensorModuleClass;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.content.Context;
public class SensorModule implements SensorEventListener {
private boolean alphaStatic = true;
Context AppContext;
// Constants for the low-pass filters
private float timeConstant = 0.18f;
private float alpha = 0.1f;
private float dt = 0;
// Timestamps for the low-pass filters
private float timestamp = System.nanoTime();
private float timestampOld = System.nanoTime();
private int count = 0;
// Gravity and linear accelerations components for the
// Wikipedia low-pass filter
private float[] gravity = new float[]
{ 0, 0, 0 };
private float[] linearAcceleration = new float[]
{ 0, 0, 0 };
// Raw accelerometer data
private float[] input = new float[]
{ 0, 0, 0 };
// device sensor manager
public static SensorManager mSensorManager;
public static SensorEventListener mSensorListener;
// Outputs for the acceleration and LPFs
public float[] AccelerationData = new float[3];
public float[] AccelerationResult = new float[3];
public SensorModule(Context Context){
System.out.println("#### Constructor");
AppContext = Context;
// initialize your android device sensor capabilities
//mSensorManager = (SensorManager) AppContext.getSystemService(Context.SENSOR_SERVICE);
SensorManager mSensorManager = (SensorManager) AppContext.getSystemService(Context.SENSOR_SERVICE);
System.out.println("#### Constructor done");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
System.out.println("=======On changed called=====");
// Get a local copy of the sensor values
System.arraycopy(event.values, 0, AccelerationData, 0, event.values.length);
AccelerationData[0] = AccelerationData[0] / SensorManager.GRAVITY_EARTH;
AccelerationData[1] = AccelerationData[1] / SensorManager.GRAVITY_EARTH;
AccelerationData[2] = AccelerationData[2] / SensorManager.GRAVITY_EARTH;
// Get result
AccelerationResult = GetAcceleration(AccelerationData);
}
public float[] GetAcceleration(float[] AccelData){
// Get a local copy of the sensor values
System.arraycopy(AccelData, 0, this.input, 0, AccelData.length);
//
count++;
if (count > 5)
{
// Update the Wikipedia filter
// y[i] = y[i] + alpha * (x[i] - y[i])
// Get gravity values
gravity[0] = gravity[0] + alpha * (this.input[0] - gravity[0]);
gravity[1] = gravity[1] + alpha * (this.input[1] - gravity[1]);
gravity[2] = gravity[2] + alpha * (this.input[2] - gravity[2]);
// Use gravity values to get the acceleration by substracting the
// gravity from the input signel(the raw acceleration data)
linearAcceleration[0] = input[0] - gravity[0];
linearAcceleration[1] = input[1] - gravity[1];
linearAcceleration[2] = input[2] - gravity[2];
}
// Return the acceleration values of the x, y and z axis
return linearAcceleration;
}
}
欢迎任何建议和反馈!
最佳答案
SensorM = new SensorModule(getApplicationContext);
这样使用
否则尝试像这样更改您的代码
public class SensorActivity extends Activity, implements SensorEventListener {
private final SensorManager mSensorManager;
private final Sensor mAccelerometer;
public SensorActivity() {
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
}
}