为什么我的 SensorEventListener 实现不起作用?没有收到 onSensorChanged 回调
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);
}
}
您需要实际订阅传感器更新,而不是简单地实施 SensorEventListener
。
将此添加到您的 onCreate
方法中:
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
.
我正在尝试制作一个应用来以度数显示手机旋转,但没有调用 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);
}
}
您需要实际订阅传感器更新,而不是简单地实施 SensorEventListener
。
将此添加到您的 onCreate
方法中:
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
.