SensorUtil.java
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;
import java.util.ArrayList;
import java.util.List;
public class SensorUtil {
private static SensorUtil instance = null;
public static SensorUtil getInstance() {
if (instance == null) {
synchronized (SensorUtil.class) {
if (instance == null) {
instance = new SensorUtil();
}
}
}
return instance;
}
public static SensorUtil newInstance(Context activityContext) {
SensorUtil sensorUtil = new SensorUtil();
sensorUtil.init(activityContext);
return sensorUtil;
}
private SensorUtil() {
}
private Context context = null;
private SensorManager sensorManager = null;
private float[] gravity = null;
private float[] geomagnetic = null;
private int surfaceRotation = Surface.ROTATION_0;
private int axisX = SensorManager.AXIS_Y;
private int axisY = SensorManager.AXIS_X;
private float orientation = -1;
private float roll = -1;
private float tilt = -1;
private List<SensorListener> sensorListeners = new ArrayList<>();
public void init(Context activityContext) {
if (activityContext == null) {
return;
}
this.context = activityContext;
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensorManager.reg