Hello I am learning a bit of droid programming, specifically for class I am trying to make a game. I am trying to use the orientation sensor to get input to use in the game, the problem is that I never get an onchange event to happen. I started running the debugger and found out that register listener always fails and spits back false, this leaves my sensor event listener null, and me confused. since I have been working on this for literally 13 hours I suppose I’ll swallow some pride and ask you guys for help. Here is the code, I have stripped basically everything out from it. I just don’t get sensors.
Thank you all for any help I recieve
package edu.hiram.cpsc172;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
@SuppressWarnings("deprecation")
public class GraphView extends View implements SensorEventListener
{
private Canvas screen;
private boolean t = false;
private float headingAngle;
private float pitchAngle;
private float rollAngle;
private SensorManager mSensorManager;
private Sensor mGyro;
private SensorEventListener glisten;
private Context context; // I have no idea what this actually means, but hey why not try? whats the worst that can happen?
public GraphView(Context con)
{
super(con);
this.context = con; // so I have a context outside the constructor, it is needed for stuff.
// I find it amusing that although I have no clue what this stuff does I am
// recognizing patterns in how context is used.
//Annoyingly I have to do a few initializations in on draw, I dislike that.
}
// Called back to draw the view. Also called by invalidate()
@Override
protected void onDraw(Canvas canvas)
{
//gives me a canvas object to play with if I want, and draws my background
screen = canvas;
{
mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
mGyro = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
boolean x = true;
x = mSensorManager.registerListener(glisten, mGyro, Sensor.TYPE_ORIENTATION); //this always returns false, and glisten stays null
x = x;
x=x; //I realize this does nothing, but it gives me places to set breakpoints, and check the line output
x = x;
}
try {
Thread.sleep(10);
}
catch (InterruptedException e)
{
}
invalidate();
}
@Override
public void onSensorChanged(SensorEvent event)
{
{
headingAngle=event.values[0];
pitchAngle= event.values[1];
rollAngle=event.values[2];
t= true;
}
Paint paint = new Paint();
paint.setColor(Color.WHITE);
screen.drawText("sensors responded", 100, 100, paint);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
// TODO Auto-generated method stub
}
}
After changing the line the listener gets registered but onSensorChange never fires. I hope editing to ask is right, or is this a new question? Thanks again
You never initialized
glisten. But, what you really want to do, since you implement the interface, is to just usethis:See the sample.