java - 在非 Activity 类中访问 Android 传感器

标签 java android

我正在开发一个 Android 应用程序,该应用程序有一个主要 Activity ,其中包含一些 TextView ,用于显示传感器数据(读取和显示加速度和 giro 数据)。在 onCreate 方法中,我调用了一个名为 SensorModule 的普通 java 调用,并将连接作为此类的参数。

SensorModule 类实现了用于监听传感器事件的 SensorEventListener。该对象是在主要 Activity 的 onCreate 方法中创建的(我有一些打印件,我在 Logcat 中看到了它们)。

但是没有调用 onSensorChanged 方法。 (当我将 SensorModule 代码放在主要 Activity 中时,它工作正常)。

我认为我在初始化 SensorModule 和它的 sensorEventlistener 时遇到了问题,但我不知道问题出在哪里。有人有想法吗?

下面我在主要 Activity 的onCreate方法中初始化SensorModule:

SensorM = new SensorModule(this);

这是 SensorClass,我认为哪里出了问题,LogCat 中没有打印 SensorOnChange 中的打印内容:

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

关于java - 在非 Activity 类中访问 Android 传感器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20048646/

相关文章:

android - 我们可以在 Lollipop 设备中显示旧式时间选择器(Pre Lollipop Time Picker)吗

java - 更改层次结构父级

java - LibGDX - 如何减慢 update() 方法?

java - 应用购买中的 Android 磨损

java - 在 git update-index --assume-unchanged 之后 merge 的问题

定义了数据报源的 Java MulticastSocket

java - Cassandra 3 中的 org.apache.cassandra.auth.Auth 去了哪里?

android - Android GCM 推送通知 App 中的服务

android - 跟踪 View 不起作用

android - 如何仅在房间数据库中按日期和月份过滤数据并忽略年份