Read tilt sensor from live wallpaper
I am making my first live wallpaper. I started with the code in the example CubeWallpaper and modified it. So far so good, I’m now drawing my own, completely different animation graphics.
I now want my graphics to respond to the tilt of my phone. I previously implemented the tilt sensor in a non-wallpaper app. However, when I try to include my skew code into the wallpaper, the onSensorChanged()
method never seems to be called – at least my log messages never appear in logcat.
One thing I’m not sure about is where to attach implements SensorEventListener
. Should it be my class that extends the WallpaperService
or a subclass of it that extends the engine
?.. I’ve actually tried both – but neither seems to work. Is there something extra needed to get the tilt to work?
EDIT: Currently I’m guessing that implements SensorEventListener
attached to the class that extends Engine
, so the gist of my code is as follows:
EDIT: Is it related to the thread running the SensorEventListener? I read somewhere that it needs to be on the UI thread.
public class MicksWallPaper extends WallpaperService
{
blah blah
class MyWallPaper extends Engine implements SharedPreferences.OnSharedPreferenceChangeListener , SensorEventListener
{
SensorManager sensorManager = null;
onCreate()
{
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
public void onSensorChanged(SensorEvent event)
{
Log.i("wp:","onSensorChanged"); I never see this message
synchronized (this)
{
switch (event.sensor.getType())
{
case Sensor.TYPE_ORIENTATION:
pitch = event.values[1];
Log.i("pitch=",""+pitch);
break;
}
}
}
}
}
EDIT: The issue has been resolved. It’s embarrassing – I just forgot to call registerListener for my sensorManager.
Solution
(on the advice of the ogre…)
The problem is solved. It’s embarrassing – I just forgot to call registerListener for my sensorManager.