detect Shake in android
Asked Answered
C

3

3

I am trying to hit an API when user shake a device 10 times. I have tried many git sample and stack overflow solution but non of them did solve my problem. Some of them detecting shake before 10 times or after 10 times. I have tried Seiamic and ShakeDetector libraries. Please give me some valuable solution.

Cyruscyst answered 28/7, 2016 at 10:36 Comment(1)
It's not that simple with multiple consecutive shakes. I believe you'll need to define what a shake is and what's the timeout between two shakes by yourself in order to be satisfied with the solutionCartoon
E
3

I have done that using this library :

  1. Add the dependecy in your build.gridle file

    allprojects {
      repositories {
        ...
        maven { url 'https://jitpack.io' }
      }
    }
    
    dependencies {
      compile 'com.github.safetysystemtechnology:android-shake-detector:v1.2'
    }
    
  2. Give the permission to your app manifest file

    <uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />
    

    if you will run in background, register your broadcast receiver

    <receiver android:name=".ShakeReceiver">
        <intent-filter>
            <action android:name="shake.detector" />
        </intent-filter>
    </receiver>
    
  3. start that in onCreate method Like this :

     private ShakeDetector shakeDetector;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
    
         buildView();
    
         ShakeOptions options = new ShakeOptions()
                 .background(true)
                 .interval(1000)
                 .shakeCount(2)
                 .sensibility(2.0f);
    
         this.shakeDetector = new ShakeDetector(options).start(this, new ShakeCallback() {
             @Override
             public void onShake() {
                 Log.d("event", "onShake");
             }
         });
    
         //IF YOU WANT JUST IN BACKGROUND
         //this.shakeDetector = new ShakeDetector(options).start(this);
     }
    
  4. override onStop method and stop that

     @Override
     protected void onStop() {
         super.onStop();
         shakeDetector.stopShakeDetector(getBaseContext());
     }
    
  5. override onDestroy method and destroy like this :

     @Override
     protected void onDestroy() {
         shakeDetector.destroy(getBaseContext());
         super.onDestroy();
     }
    

(*) Optional step : if you will run in background, create your broadcast receiver

public class ShakeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (null != intent && intent.getAction().equals("shake.detector")) {
            ...
        }
    }
}
Esprit answered 18/2, 2019 at 10:50 Comment(0)
A
1

By Using SensorListener

Please check this ticked answer...

You have to adjust SHAKE_THRESHOLD value to achieve this

How to detect shake event with android?

Thank you!

Allegiance answered 28/7, 2016 at 10:40 Comment(0)
R
1

have a static variable that gets incremented every time a shake is detected.

static int count = 0;
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
    long curTime = System.currentTimeMillis();
    if ((curTime - mLastShakeTime) > MIN_TIME_BETWEEN_SHAKES_MILLISECS) {

        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        double acceleration = Math.sqrt(Math.pow(x, 2) +
                Math.pow(y, 2) +
                Math.pow(z, 2)) - SensorManager.GRAVITY_EARTH;

        if (acceleration > SHAKE_THRESHOLD) {
            mLastShakeTime = curTime;
            count++;
            if(count==10){
                //your code goes here
            }
        }
    }
}
Restrictive answered 28/7, 2016 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.