为什么我的SensorEventListener实现不起作用?不接收onSensorChanged回调。

huangapple go评论58阅读模式
英文:

Why doesn't my SensorEventListener implementation work? Not receiving onSensorChanged callbacks

问题

我正在尝试制作一个显示手机旋转角度的应用程序,但onSensorChanged没有被调用。textView中的文本没有更改,我不知道为什么。它没有给出任何编译错误。以下是代码。

package com.alex.location360;

import androidx.appcompat.app.AppCompatActivity;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SensorEventListener  {

    TextView textView;
    float roll=0;
    @Override
    public void onSensorChanged(SensorEvent event) {
        float[] mGravity = new float[0];
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
            mGravity = event.values;

        float[] mGeomagnetic = new float[0];
        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
            mGeomagnetic = event.values;

        if (mGravity != null && mGeomagnetic != null) {
            float R[] = new float[9];
            float I[] = new float[9];

            boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
            if (success) {
                float orientation[] = new float[3];
                SensorManager.getOrientation(R, orientation);
                float azimuth = orientation[0]; // orientation contains: azimuth, pitch and roll
                float pitch = orientation[1];
                roll = orientation[2];
                roll= (float) Math.toDegrees(roll);
                textView.setText(String.valueOf(roll));
                Log.e("#",String.valueOf(roll));
            }
        }
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.textView);
    }
}
英文:

I'm trying to make an app to display phones roation in degrees, but onSensorChanged is not getting called. The text in the textView isn't changing and I don't know why. It doesn't give any compile error. Here is the code.

package com.alex.location360;
import androidx.appcompat.app.AppCompatActivity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements SensorEventListener  {
TextView textView;
float roll=0;
@Override
public void onSensorChanged(SensorEvent event) {
float[] mGravity = new float[0];
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
float[] mGeomagnetic = new float[0];
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
float azimuth = orientation[0]; // orientation contains: azimuth, pitch and roll
float pitch = orientation[1];
roll = orientation[2];
roll= (float) Math.toDegrees(roll);
textView.setText(String.valueOf(roll));
Log.e("#",String.valueOf(roll));
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView);
}
}

答案1

得分: 4

请将以下内容添加到您的onCreate方法中以实际订阅传感器更新,而不仅仅实现SensorEventListener

SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magneticFieldSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensorManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, magneticFieldSensor, SensorManager.SENSOR_DELAY_NORMAL);

您可能还希望在onDestroy中取消这些监听器的订阅(或者如果您只希望在Activity可见时接收更新,可以使用onStart/onStop)。

英文:

You need to actually subscribe to sensor updates, rather than simply implementing SensorEventListener.

Add this to your onCreate method:

SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magneticFieldSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensorManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, magneticFieldSensor, SensorManager.SENSOR_DELAY_NORMAL);

You'll probably also want to unsubscribe from the listeners in onDestroy (or use onStart/onStop if you only want updates while the Activity is visible.

huangapple
  • 本文由 发表于 2020年7月31日 16:14:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63188169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定