我编写了代码来获取加速度计返回的坐标,并在单击按钮时将其打印在TextViews上。但是,我只获得0.0作为所有标签上的值。 我还在onSensorChange()方法中添加了setText部分,以检查每次移动设备稍微动摇时值是否会改变。我也在Manifest中添加了必要的权限。请帮我完成这项工作。
代码:
package com.example.lol;
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.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener{
private SensorManager senSensorManager;
private Sensor senAccelerometer;
private long lastUpdate = 0;
private float last_x, last_y, last_z;
public float dummy_x, dummy_y,dummy_z;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(onClickListener);
senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
senSensorManager.unregisterListener(this);
}
protected void onResume() {
super.onResume();
senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
public void onSensorChange(SensorEvent sensorEvent) {
Sensor mySensor = sensorEvent.sensor;
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
float z = sensorEvent.values[2];
dummy_x=x;
dummy_y=y;
dummy_z=z;
TextView textView1 = (TextView)findViewById(R.id.textView1);
TextView textView2 = (TextView)findViewById(R.id.textView2);
TextView textView3 = (TextView)findViewById(R.id.textView3);
textView1.setText(Float.toString(x));
textView2.setText(Float.toString(y));
textView3.setText(Float.toString(z));
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@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;
}
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.button1:
TextView textView1 = (TextView)findViewById(R.id.textView1);
TextView textView2 = (TextView)findViewById(R.id.textView2);
TextView textView3 = (TextView)findViewById(R.id.textView3);
textView1.setText(Float.toString(dummy_x));
textView2.setText(Float.toString(dummy_y));
textView3.setText(Float.toString(dummy_z));
break;
}
}
};
@Override
public void onSensorChanged(SensorEvent event) {
}
}
答案 0 :(得分:1)
您有2个OnSensorChange函数。第一个包含所有TextView代码的内容不会覆盖任何内容。底部的那个(空)是值将来的那个。