I'm having a hard time finding this out properly. All I want is that when I shake my color changes to something else or text changes to something. Doesn't have to be random tho. Can't find anything usefull that doesn't include useless stuff.
Asked
Active
Viewed 1,808 times
2
-
Go through this. This will help you to do. http://stackoverflow.com/questions/2317428/android-i-want-to-shake-it – Satheeshkumar Mar 27 '13 at 12:08
2 Answers
2
Not even 20 seconds of googling gives you already a probably perfectly fine working copy-paste solution with an easy call-back where you can do anything you want inside your Activity...
http://android.hlidskialf.com/blog/code/android-shake-detection-listener

Stefan de Bruijn
- 6,289
- 2
- 23
- 31
0
Use the below class and when creating a new instance of it call the setCallBack method on that instance you created to receive callbacks ,pass that instance when you register a listener through the sensor manager.
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
public class ShakeListener implements SensorEventListener {
private ShakeCallback mShakeCallback;
private int totalMovement;
private long timeMilies;
private float lastX, lastY, lastZ = -1.0f;
private int shakeIntervalMilies = 2000;
ShakeListener() {
}
public static ShakeListener newInstance() {
return new ShakeListener();
}
public ShakeListener setCallBack(ShakeCallback shakeCallback) {
this.mShakeCallback = shakeCallback;
return this;
}
public ShakeListener setShakingInterval(int intervalMilies) {
this.shakeIntervalMilies = intervalMilies;
return this;
}
@Override
public void onSensorChanged(SensorEvent event) {
if (mShakeCallback != null) {
totalMovement = (int) ((event.values[0] + event.values[1] + event.values[2]) - (lastX
+ lastY + lastZ));
if (totalMovement > 20
&& (System.currentTimeMillis() - timeMilies >= shakeIntervalMilies)) {
mShakeCallback.onShake();
timeMilies = System.currentTimeMillis();
}
lastX = event.values[0];
lastY = event.values[1];
lastZ = event.values[2];
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public static interface ShakeCallback {
public void onShake();
}
}