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

  • SEARCH
  • Home
  • 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 6538167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:42:15+00:00 2026-05-25T10:42:15+00:00

I work on a magic eight ball style application. What is the easiest way

  • 0

I work on a magic eight ball style application.

What is the easiest way to simply add a listener to the accelerometer, and when it changes a decent amount (being shaken), to have something trigger?

  • 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-25T10:42:16+00:00Added an answer on May 25, 2026 at 10:42 am

    Here is my code for accelerometer i have require accelerometer play sound clip…..
    I have “gavel_single.wav” ring in asset folder……Its work properly

    I also send some references...
        http://developer.android.com/resources/samples/AccelerometerPlay/index.html
    my code    
        import android.app.Activity;
        import android.content.res.AssetFileDescriptor;
        import android.hardware.Sensor;
        import android.hardware.SensorEvent;
        import android.hardware.SensorEventListener;
        import android.hardware.SensorManager;
        import android.media.MediaPlayer;
        import android.media.MediaPlayer.OnCompletionListener;
        import android.os.Bundle;
    
        public class SensorTest extends Activity implements SensorEventListener {
            private SensorManager sensorManager;
            private boolean color = false;
            private long lastUpdate;
            private MediaPlayer mMediaplayer;
    
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
                sensorManager.registerListener(this, sensorManager
                        .getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                        SensorManager.SENSOR_DELAY_NORMAL);
                lastUpdate = System.currentTimeMillis();
            }
    
            public void onSensorChanged(SensorEvent event) {
                if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                    float[] values = event.values;
                    // Movement
                    float x = values[0];
                    float y = values[1];
                    float z = values[2];
    
                    float accelationSquareRoot = (x * x + y * y + z * z)
                            / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
                    long actualTime = System.currentTimeMillis();
                    if (accelationSquareRoot >= 2) //
                    {
                        if (actualTime - lastUpdate < 200) {
                            return;
                        }
                        lastUpdate = actualTime;
    
                        if (color) {
    
                        } else {
                            try {
    
                                AssetFileDescriptor afd = getAssets().openFd(
                                        "gavel_single.wav");
                                mMediaplayer = new MediaPlayer();
                                mMediaplayer.setDataSource(afd.getFileDescriptor(), afd
                                        .getStartOffset(), afd.getLength());
                                afd.close();
                                mMediaplayer.prepare();
                                mMediaplayer.start();
    
                                mMediaplayer
                                        .setOnCompletionListener(new OnCompletionListener() {
    
                                            public void onCompletion(MediaPlayer mp) {
                                                mp.stop();
                                                mp.release();
    
                                            }
                                        });
    
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
    
                        }
    
                    }
    
                }
    
            }
    
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            protected void onResume() {
                super.onResume();
                // register this class as a listener for the orientation and
                // accelerometer sensors
                sensorManager.registerListener(this, sensorManager
                        .getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                        SensorManager.SENSOR_DELAY_NORMAL);
            }
    
            @Override
            protected void onPause() {
                // unregister listener
                sensorManager.unregisterListener(this);
                super.onStop();
            }
    
            @Override
            protected void onDestroy() {
    
                super.onDestroy();
                if (mMediaplayer != null) {
                    mMediaplayer.release();
                    mMediaplayer = null;
                }
    
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have configured my application to work with multiple databases. The magic works just
Work on this small test application to learn threading/locking. I have the following code,
In code at work, we have many uses of magic strings like the following
Here's me fiddle: http://jsfiddle.net/6yU6N/10/ I want to work some CSS magic on the table
In our application we work with DataTable s a lot. This is dictated by
I can't seem to find the magic combination to make the HeaderStringFormat work for
This needs only work in g++. I want a function template<typename T> std::string magic();
I work on an application that uses DCOM to communicate between what are essentially
I'm trying to make markitup! work with rails 3.1, the magic happens inside my
why does this work.. <script type=text/javascript> <!-- function myAlert(){ alert('magic!!!'); } if(document.addEventListener){ myForm.addEventListener('submit',myAlert,false); }else{

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.