我正在开发一款Android应用,其主要活动包含一些用于显示传感器数据的文本视图(读取和显示加速度和转换数据)。在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;
}
}
欢迎任何建议和反馈!
答案 0 :(得分:1)
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) {
}
}