Android 如何将传感器方向更改为未弃用的代码并运行此代码
我在一篇教程中找到了这段代码,并做了一些修改。该代码显示方向和加速计传感器,当智能手机改变45度时,它会计算曲线数。它工作得很好。但我看到传感器的方向,传感器加速计被弃用了。如何使用未弃用的常量更改代码并使其继续工作?我是android编程和堆栈溢出的初学者。谢谢大家!Android 如何将传感器方向更改为未弃用的代码并运行此代码,android,orientation,accelerometer,deprecated,Android,Orientation,Accelerometer,Deprecated,我在一篇教程中找到了这段代码,并做了一些修改。该代码显示方向和加速计传感器,当智能手机改变45度时,它会计算曲线数。它工作得很好。但我看到传感器的方向,传感器加速计被弃用了。如何使用未弃用的常量更改代码并使其继续工作?我是android编程和堆栈溢出的初学者。谢谢大家! public class MainActivity extends Activity implements SensorListener { private long lastUpdate=0; private
public class MainActivity extends Activity implements SensorListener {
private long lastUpdate=0;
private float last_x = 0;
final String tag = "IBMEyes";
SensorManager sm = null;
TextView xViewA = null;
TextView yViewA = null;
TextView zViewA = null;
TextView xViewO = null;
TextView yViewO = null;
TextView zViewO = null;
TextView mud=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.activity_main);
xViewA = (TextView) findViewById(R.id.xbox);
yViewA = (TextView) findViewById(R.id.ybox);
zViewA = (TextView) findViewById(R.id.zbox);
xViewO = (TextView) findViewById(R.id.xboxo);
yViewO = (TextView) findViewById(R.id.yboxo);
zViewO = (TextView) findViewById(R.id.zboxo);
mud=(TextView) findViewById(R.id.mud);
}
static int cont =0;
//se (valor_INICIAL - VALOR_ATUAL) > 45
@SuppressWarnings("deprecation")
public void onSensorChanged(int sensor, float[] values) {
synchronized (this) {
Log.d(tag, "onSensorChanged: " + sensor + ", x: " +
values[0] + ", y: " + values[1] + ", z: " + values[2]);
if (sensor == SensorManager.SENSOR_ORIENTATION) {
xViewO.setText("Orientation X: " + values[0]);
yViewO.setText("Orientation Y: " + values[1]);
zViewO.setText("Orientation Z: " + values[2]);
double diffDegree = Math.abs(values[0] - last_x);
if (diffDegree > 45)
{
last_x = values[0];
mud.setText("cont: "+ ++cont);
}
}
}
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
xViewA.setText("Accel X: " + values[0]);
yViewA.setText("Accel Y: " + values[1]);
zViewA.setText("Accel Z: " + values[2]);
}
}
private void setText(string mud) {
// TODO Auto-generated method stub
}
protected void onResume() {
super.onResume();
// register this class as a listener for the orientation and accelerometer sensors
sm.registerListener(this,
SensorManager.SENSOR_ORIENTATION |SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
// unregister listener
sm.unregisterListener(this);
super.onStop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item); }
@Override
public void onAccuracyChanged(int sensor, int accuracy) {
Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
}
}
无论何时要引用传感器,请使用:。例如:Sensor.TYPE\u加速计和Sensor.TYPE\u方向 我不知道是否需要它,但您可能需要检查onResume中的sm是否为null。
此外,您可能希望在onPause而不是onStop中注销SensorEventListener。但也不推荐使用键入方向。Eclipse建议使用getOrientation,但我无法在代码中使用它。无论如何,谢谢你!这对我来说是个新闻。看起来它从api 20开始就被弃用了,而api 20目前几乎没有被使用。您仍然可以使用它或其他类似的传感器,如旋转。你们也可以自己用加速计和磁场来计算