I used this link http://blog.androgames.net/85/android-accelerometer-tutorial/ to create an accelerometer. But I want that this accelerometer work in the background. So I changed the code like this:
public class Accelerometer extends Service implements AccelerometerListener{
private static Context CONTEXT;
@Override
public IBinder onBind(Intent intent) {
// TODO Put your code here
return null;
}
@Override
public void onCreate() {
System.out.println(”start listening”);
// if (AccelerometerManager.isSupported()) {
AccelerometerManager.startListening(this);
// }
}
@Override
public void onDestroy() {
System.out.println(”start listening”);
// if (AccelerometerManager.isListening()) {
AccelerometerManager.stopListening();
// }
}
public static Context getContext() {
return CONTEXT;
}
/**
* onShake callback
*/
public void onShake(float force) {
Toast.makeText(this, “close” + force, 1000).show();
}
/**
* onAccelerationChanged callback
*/
public void onAccelerationChanged(float x, float y, float z) {
System.out.println(”x = “+x+” y = “+y+” z = “+z);
}
}
And I obtained this error:
Java.lang.runtimeexception : Unable to create service com.tam.Accelerometer: java.lang.NullPointerException
Caused by: java;lang.NullPointerException
Can anyone help me?
Android Accelerometer Sensor
You already posted it and got an answer if that is you? You need to set CONTEXT