答案 0 :(得分:1)
我希望此代码能够提供帮助
package com.exercise.AndroidSensorEventListener;
import java.util.List;
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.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidSensorEventListener extends Activity {
private static SensorManager mySensorManager;
private boolean sersorrunning;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
if(mySensors.size() > 0){
mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);
sersorrunning = true;
Toast.makeText(this, "Start ORIENTATION Sensor", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, "No ORIENTATION Sensor", Toast.LENGTH_LONG).show();
sersorrunning = false;
finish();
}
}
private SensorEventListener mySensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
System.out.println("Azimuth: " + String.valueOf(event.values[0]));
System.out.println("Pitch: " + String.valueOf(event.values[1]));
System.out.println("Roll: " + String.valueOf(event.values[2]));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if(sersorrunning){
mySensorManager.unregisterListener(mySensorEventListener);
Toast.makeText(AndroidSensorEventListener.this, "unregisterListener", Toast.LENGTH_SHORT).show();
}
}
}
答案 1 :(得分:1)
如果您熟悉RxJava和Reactive Programming,则可以使用名为ReactiveSensors的开源库。它是来自Android SDK的SensorManager
的包装器,允许您监视作为可观察流发出的传感器更改。
您可以通过以下方式使用该库:
将以下依赖项添加到build.gradle
文件中:
dependencies {
compile 'com.github.pwittchen:reactivesensors:0.1.2'
}
然后,您可以在代码中使用库:
new ReactiveSensors(this).observeSensor(Sensor.TYPE_ORIENTATION)
.subscribeOn(Schedulers.computation())
.filter(ReactiveSensorEvent.filterSensorChanged())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<ReactiveSensorEvent>() {
@Override public void call(ReactiveSensorEvent reactiveSensorEvent) {
SensorEvent event = reactiveSensorEvent.getSensorEvent();
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
String format = "orientation sensor readings:\n x = %f\n y = %f\n z = %f";
String message = String.format(format, x, y, z);
tvSensor.setText(message);
}
});
在您的情况下,您可以使用Sensor.TYPE_ORIENTATION
,但它适用于Android设备上提供的各种传感器。
有关库的详细说明,源代码和示例工作应用程序,请访问:https://github.com/pwittchen/ReactiveSensors
答案 2 :(得分:0)
您需要像SensorListener
这样的东西private final SensorListener sl = new SensorListener() {
public void onSensorChanged(int sensor, float[] values) {
}
public void onAccuracyChanged(int sensor, int accuracy) {
}
};
然后注册:
// Locate the SensorManager using Activity.getSystemService
SensorManager sm;
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
// Register your SensorListener
sm.registerListener(sl, SensorManager.SENSOR_ORIENTATION SensorManager.SENSOR_DELAY_NORMAL);
除此之外,您将获得所需的信息......
参考:
答案 3 :(得分:0)