我正在创建一个名为CompassApp的应用程序。我创建了两个名为Compass.java和Rose.java的类。此应用程序是根据网站中描述的教程创建的,它使用不推荐使用的SensorListener版本。 Compass.java类使用的是不推荐使用的版本。当我使用已弃用的版本时,应用程序运行正常。然后我想将我的应用程序转换为当前版本的SensorEventListener。但罗盘的旋转停止了!我被困住了,不知道该怎么办。请帮帮我。
Compass.java,不推荐使用SensorListener接口
public class Compass extends Activity implements SensorListener {
SensorManager sensorManager;
static final int sensor = SensorManager.SENSOR_ORIENTATION;
MyRose rose;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
rose = new MyRose(this);
setContentView(rose);
// get sensor manager
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
// register to listen to sensors
@Override
public void onResume() {
super.onResume();
sensorManager.registerListener(this, sensor);
}
// unregister
@Override
public void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
// Ignore for now
public void onAccuracyChanged(int sensor, int accuracy) {
}
// Listen to sensor and provide output
public void onSensorChanged(int sensor, float[] values) {
if (sensor != Compass.sensor)
return;
int orientation = (int) values[0];
rose.setDirection(orientation);
}
}
Compass.java与新的SensorEventListener
public class Compass extends Activity{
SensorManager sensorManager;
SensorEventListener listener ;
static final int sensor = SensorManager.SENSOR_ORIENTATION;
Rose rose;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
listener = new SensorEventListener()
{
@Override
public void onSensorChanged(SensorEvent e)
{
int orientation = (int) e.values[0];
rose.setDirection(orientation);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
};
setContentView(rose);
// get sensor manager
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(listener, sensorManager.getDefaultSensor(sensor),SensorManager.SENSOR_MAGNETIC_FIELD);
}
// register to listen to sensors
@Override
public void onResume() {
super.onResume();
sensorManager.registerListener(listener, sensorManager.getDefaultSensor(sensor), SensorManager.SENSOR_MAGNETIC_FIELD);
}
// unregister
@Override
public void onPause() {
super.onPause();
sensorManager.unregisterListener(listener);
}
}
答案 0 :(得分:4)
我认为这就是你所需要的。
public class Compass extends Activity implements SensorEventListener {
SensorManager sensorManager;
static final int sensor = SensorManager.SENSOR_ORIENTATION;
MyRose rose;
private Sensor myCompassSensor;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
rose = new MyRose(this);
setContentView(rose);
// get sensor manager
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
// get compass sensor (ie magnetic field)
myCompassSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}
// register to listen to sensors
@Override
public void onResume() {
super.onResume();
sensorManager.registerListener(this, myCompassSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
// unregister
@Override
public void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
// Ignore for now
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
public void onSensorChanged(SensorEvent event) {
// this check is unnecessary with only one registered sensor
// but it's useful to know in case you need to add more sensors
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
int orientation = (int) event.values[0];
rose.setDirection(orientation);
}
}
}
编辑:添加检查以确定传感器类型