我对这个加速度计编码有问题。我一直从加速度计得到0作为我的返回值。首先忽略cal_displace部分,因为现在我无法从加速度计中获得任何值。非常感谢你的帮助。
package com.example.suntracking;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class Accelerometer extends Activity implements SensorEventListener{
Sensor accelerometer;
SensorManager sm;
int samintval;
float ax,ay,axtemp ,aytemp ,sx,sy;
TextView showax,showay,showaxtemp,showaytemp;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.accelerometer);
loadsavedPref();
initialize();
cal_displace();
display();
savePreferences();
}
private void display() {
// TODO Auto-generated method stub
showax.setText(""+ax);
showay.setText(""+ay);
showaxtemp.setText(""+axtemp);
showaytemp.setText(""+aytemp);
}
@Override
protected void onResume()
{
super.onResume();
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
sm.unregisterListener(this);
}
private void initialize() {
// TODO Auto-generated method stub
showax=(TextView)findViewById(R.id.tvax);
showay=(TextView)findViewById(R.id.tvay);
showaxtemp=(TextView)findViewById(R.id.tvaxtemp);
showaytemp=(TextView)findViewById(R.id.tvaytemp);
sm=(SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
private void loadsavedPref() {
// TODO Auto-generated method stub
SharedPreferences savedata= getSharedPreferences("savealldata",0);
samintval = savedata.getInt("samintval", 1000);
}
private void cal_displace() {
// TODO Auto-generated method stub
sx = (float) (0.5 * ((ax - axtemp)/(samintval/1000)) * (samintval / 1000)*(samintval / 1000));
sy = (float) (0.5 * ((ay - aytemp)/(samintval/1000)) * (samintval / 1000)*(samintval / 1000));
//axtemp = ax;
//aytemp = ay;
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;
ax = event.values[0];
ay = event.values[1];
}
private void savePreferences() {
// TODO Auto-generated method stub
SharedPreferences savedata = getSharedPreferences("savealldata",0);
Editor editor=savedata.edit();
editor.putFloat("ax", ax);
editor.putFloat("ay", ay);
editor.putFloat("axtemp", axtemp);
editor.putFloat("aytemp", aytemp);
editor.commit();
}
}
答案 0 :(得分:1)
您正在初始化()和onResume()中注册传感器侦听器两次 - 仅在onResume()中执行。
您正在获取值,但不会更新TextView。修改你的onSensorChanged():
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;
ax = event.values[0];
ay = event.values[1];
//update TextViews
display();
}