Android加速度计仅适用于虚拟仿真器

时间:2017-04-24 08:58:33

标签: android android-sensors

我正在测试加速计传感器。所有这些都与Android Studio模拟的虚拟设备完美配合。此外,当我在真正的智能手机上试用我的代码时,日志中没有任何反应。 请看下面的代码:

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

private SensorManager senSensorManager;
private Sensor senAccelerometer;
private long lastUpdate = 0;
private float last_x, last_y, last_z;
private long diffTime;
private static final int SHAKE_THRESHOLD = 600;
ArrayList numbersGenerated = new ArrayList();
private ArrayList<Row> rows;

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

    senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void onSensorChanged(SensorEvent sensorEvent) {

    Sensor mySensor = sensorEvent.sensor;

    if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        ArrayList<Row> rows = new ArrayList<>();
        Row row;


        float x = sensorEvent.values[0];
        float y = sensorEvent.values[1];
        float z = sensorEvent.values[2];

        long curTime = System.currentTimeMillis();

        if ((curTime - lastUpdate) > 100) {
           long diffTime = (curTime - lastUpdate);
            lastUpdate = curTime;
        }
        float speed = Math.abs(x + y + z - last_x - last_y - last_z)/ diffTime * 10000;

        if (speed > SHAKE_THRESHOLD) {
            System.out.println("X\t Y\tZ");
            last_x = x;
            last_y = y;
            last_z = z;
            row = new Row(x,y,z);
            rows.add(row);
        }

        System.out.println("X\t Y\tZ");
        System.out.println("----------------------");
        for (Row printRow : rows)
        {
            System.out.println(
                    printRow.getX() + "\t " +
                            printRow.getY() + "\t" +
                            printRow.getZ());

    }


    }
else{
        System.out.println("no accelerometer");
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

protected void onPause() {
    super.onPause();
    senSensorManager.unregisterListener(this);
}

protected void onResume() {
    super.onResume();
    senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}

class Row
{

    // REMEMBER: each attribute is a column
    //
    private final float x;
    private final float y;
    private final float z;

    public Row(float x, float y, float z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public float getX()
    {
        return x;
    }

    public float getY()
    {
        return y;
    }

    public float getZ()
    {
        return z;
    }

}

你知道为什么它不起作用吗?我的智能手机上安装了Android 4,并且设置了正确的版本。 谢谢!

1 个答案:

答案 0 :(得分:0)

也许您手机的加速度计无法正常工作? System.out.println不是一种好的记录方式,而是使用LOG命令。 尝试登录到应用程序屏幕,这会改变什么吗?