Skip to content Skip to sidebar Skip to footer

Android Accelerometer Sensor

I am trying to work with Accelerometer Sensor. So i tried this example: http://blog.androgames.net/85/android-accelerometer-tutorial/ It work perfectly. But when i change Accelerom

Solution 1:

For CONTEXT try initializing it as

this.getApplicationContext()

Solution 2:

The Above code had NULLPointerException in case of CONTEXT. Thats why the application was crashing. While showing toast done use this. Use getApplicationContext(). Hope this will solve your problem.

Modified Code:

classAccelerometerextendsServiceimplementsAccelerometerListener{ 

@Override
public IBinder onBind(Intent intent) {
// TODO Put your code herereturnnull;
}

@Override
publicvoidonCreate() {
System.out.println(”start listening”);
// if (AccelerometerManager.isSupported()) { AccelerometerManager.startListening(this);// }
}

@Override
publicvoidonDestroy() {
System.out.println(”stop listening”);
// if (AccelerometerManager.isListening()) { AccelerometerManager.stopListening();// }
}

/**
* onShake callback
*/publicvoidonShake(float force) {
Toast.makeText(getApplicationContext(), “Phone shaked niktilha omha ya 3ammi el7ag: ” + String.valueOf(force), 1000).show(); }

/**
* onAccelerationChanged callback
*/publicvoidonAccelerationChanged(float x, float y, float z) { System.out.println(”x = “+x+” y = “+y+” z = “+z); }

}

Post a Comment for "Android Accelerometer Sensor"