Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3937380
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:03:43+00:00 2026-05-20T00:03:43+00:00

I’m trying to detect shake events in my application so that I would be

  • 0

I’m trying to detect shake events in my application so that I would be able to do something when user shakes the device. But the problem is; ShakeListener which I used, detects orientation changes as a shake event.(When I move the phone from landscape to portrait, it detects a shake)

I tried changing the force threshold but at that time it can’t detect any shake event.

Here is the ShakeListener code which i took from this site :

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class ShakeListener implements SensorEventListener {
private static final int FORCE_THRESHOLD = 500;
private static final int TIME_THRESHOLD = 100;
private static final int SHAKE_TIMEOUT = 500;
private static final int SHAKE_DURATION = 1000;
private static final int SHAKE_COUNT = 3;

private SensorManager mSensorMgr;
private float mLastX = -1.0f, mLastY = -1.0f, mLastZ = -1.0f;
private long mLastTime;
private OnShakeListener mShakeListener;
private Context mContext;
private int mShakeCount = 0;
private long mLastShake;
private long mLastForce;

public interface OnShakeListener {
    public void onShake();
}

public ShakeListener(Context context) {
    mContext = context;
}

public void setOnShakeListener(OnShakeListener listener) {
    mShakeListener = listener;
}

public void resume(OnShakeListener listener) {
    this.setOnShakeListener(listener);
    mSensorMgr = (SensorManager) mContext
            .getSystemService(Context.SENSOR_SERVICE);
    if (mSensorMgr == null) {
        throw new UnsupportedOperationException("Sensors not supported");
    }

    boolean supported = mSensorMgr.registerListener(this,
            mSensorMgr.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_UI);

    if (!supported) {
        mSensorMgr.unregisterListener(this,
                mSensorMgr.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER));
        throw new UnsupportedOperationException(
                "Accelerometer not supported");
    }
}

public void pause() {
    mShakeListener = null;
    if (mSensorMgr != null) {
        mSensorMgr.unregisterListener(this,
                mSensorMgr.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER));
        mSensorMgr = null;
    }
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() != SensorManager.SENSOR_ACCELEROMETER)
        return;
    long now = System.currentTimeMillis();

    if ((now - mLastForce) > SHAKE_TIMEOUT) {
        mShakeCount = 0;
    }

    if ((now - mLastTime) > TIME_THRESHOLD) {
        long diff = now - mLastTime;
        float speed = Math.abs(event.values[SensorManager.DATA_X]
                + event.values[SensorManager.DATA_Y]
                + event.values[SensorManager.DATA_Z] - mLastX - mLastY - mLastZ)
                / diff * 10000;
        if (speed > FORCE_THRESHOLD) {
            if ((++mShakeCount >= SHAKE_COUNT)
                    && (now - mLastShake > SHAKE_DURATION)) {
                mLastShake = now;
                mShakeCount = 0;
                if (mShakeListener != null) {
                    mShakeListener.onShake();
                }
            }
            mLastForce = now;
        }
        mLastTime = now;
        mLastX = event.values[SensorManager.DATA_X];
        mLastY = event.values[SensorManager.DATA_Y];
        mLastZ = event.values[SensorManager.DATA_Z];
    }

}
}

I want to detect only shake movements, not orientation changes.

Any suggestion?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:03:44+00:00Added an answer on May 20, 2026 at 12:03 am

    You will probably have to unregister your sensor listener followed by zeroing/nulling/reinitializing the values you use to determine a “shake event.” Then re-register your listener.

    An orienatation change triggers onDestroy followed by onCreate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.