gpt4 book ai didi

android - onSensorChanged() 未调用

转载 作者:太空狗 更新时间:2023-10-29 14:13:08 34 4
gpt4 key购买 nike

我正在编写 Android 应用程序,但在使用加速度计时遇到了一些问题。奇怪的是,我的函数 onSensorChanged() 没有被调用,我不知道为什么:/你能帮帮我吗? :) 这是我的代码:

@SuppressLint("NewApi") 
public class Game extends Activity implements SensorEventListener {

SensorManager sensorManager;
ImageView vaisseau;
TextView test;
RelativeLayout rl;

@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_GAME);

setContentView(R.layout.game);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

Display ecran = getWindowManager().getDefaultDisplay();
int largeur= ecran.getWidth();
int hauteur= ecran.getHeight();

Drawable d = getResources().getDrawable(R.drawable.spaceship);
int largeurVaisseau = d.getIntrinsicWidth();

vaisseau = new ImageView(this);
vaisseau.setImageResource(R.drawable.spaceship);
rl = (RelativeLayout)findViewById(R.id.layout_game);
vaisseau.setX(largeur/2 - largeurVaisseau);
vaisseau.setY(hauteur-hauteur*10/100);
rl.addView(vaisseau);

test = new TextView(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
return true;
}

public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
test.setText(Float.toString(event.values[0]));
rl.addView(test);
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_GAME);
}

@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
}

提前致谢:),杰弗里

最佳答案

注册传感器 Sensor.TYPE_ACCELEROMETER 而不是 Sensor.TYPE_ORIENTATION。

关于android - onSensorChanged() 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25095069/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com