by fundurianfred » Fri May 22, 2009 7:24 am
public class fishmain extends Activity implements SensorListener {
private SensorManager sensorMgr;
private TextView accuracyLabel;
private TextView xLabel, yLabel, zLabel;
private float x, y, z;
// deltas for calibration
private float cx, cy, cz;
private long lastUpdate = -1;
myview myview1;
Timer myTimer;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myview1 = new myview(this);
myTimer = new Timer();
this.setContentView(myview1);
myTimer.schedule( new myTimerTask(myview1),40,40 );
}
public void onSensorChanged(int sensor, float[] values) {
if (sensor == SENSOR_ORIENTATION) {
long curTime = System.currentTimeMillis();
// only allow one update every 100ms, otherwise updates
// come way too fast and the phone gets bogged down
// with garbage collection
if (lastUpdate == -1 || (curTime - lastUpdate) > 40) {
lastUpdate = curTime;
float x = values[1];
float y = values[2];
float z = values[0];
myview1.fishmove(-x/4.0f, -y/4.0f);
}
}
}
@Override
protected void onPause() {
super.onPause();
sensorMgr.unregisterListener(this, SENSOR_ORIENTATION);
sensorMgr = null;
cx = 0;
cy = 0;
cz = 0;
}
@Override
protected void onResume() {
super.onResume();
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
boolean accelSupported = sensorMgr.registerListener(this,
SENSOR_ORIENTATION,
SENSOR_DELAY_UI);
if (!accelSupported) {
// on accelerometer on this device
sensorMgr.unregisterListener(this, SENSOR_ORIENTATION);
Log.d("ddd","no acc");
}
}
}
This is the code I used, notice all the SENSOR_ORIENTATION, before that I used SENSOR_ACCELEROMETER but user complain, afterward, i change it all to SENSOR_ORIENTATION, but like you said, it did not work either. what should i do, I dont have device to test it out. Can you give me a hand please