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 7753097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:58:03+00:00 2026-06-01T11:58:03+00:00

There is a dice game in android games that when you move up and

  • 0

There is a dice game in android games that when you move up and down your device like a Ping-pong racket, it throws dices in the screen.

I want to detect this movement of device. How can I do that?

Thanks,

  • 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-06-01T11:58:05+00:00Added an answer on June 1, 2026 at 11:58 am
    package org.mabna.order.utils;
    
    public interface OnDeviceShakeListener {
        public abstract void onDeviceShaked();
    }
    

    import java.util.ArrayList;
    
    import android.content.Context;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    
    public class SensorListener implements SensorEventListener {
    
        float[] xVals = new float[] { 0, 0, 0 };
        float[] yVals = new float[] { 0, 0, 0 };
        long prevTime = 0;
    
        final float angle = 10;
        final long time = 100 * 1000 * 1000; // in nanosecond
    
        public SensorListener()
        {
    
        }
    
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onSensorChanged(SensorEvent event) {
            synchronized (this) {
    
                if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
                    if (event.timestamp - prevTime > time)
                    {
                        prevTime = event.timestamp;
    
                        xVals[0] = xVals[1];
                        xVals[1] = xVals[2];
                        xVals[2] = event.values[1];
    
                        yVals[0] = yVals[1];
                        yVals[1] = yVals[2];
                        yVals[2] = event.values[2];
    
                        if (degreeChanged(xVals) ||
                                degreeChanged(yVals))
                        {
                            OnDeviceShaked();
                        }
                    }
    
                }
    
            }
        }
    
        private boolean degreeChanged(float[] V) {
    
            float v0 = Math.abs(V[0]);
            float v1 = Math.abs(V[1]);
            float v2 = Math.abs(V[2]);
    
            boolean hasAngle = ((v1 - v0) > angle && (v1 - v2) > angle);
    
            return hasAngle;
        }
    
        // DeviceShakeListener ----------------------------------------------------
    
        ArrayList<OnDeviceShakeListener> arrOnDeviceShakeListener =
                new ArrayList<OnDeviceShakeListener>();
    
        /**
         * Listens if device is shaken like a Ping-pong racket.
         * 
         * @param listener
         */
        public void setOnDeviceShakeListener(
                OnDeviceShakeListener listener) {
            arrOnDeviceShakeListener.add(listener);
        }
    
        public void clearOnDeviceShakeListener(
                OnDeviceShakeListener listener) {
            arrOnDeviceShakeListener.remove(listener);
        }
    
        // This function is called after the new point received
        private void OnDeviceShaked() {
            // Check if the Listener was set, otherwise we'll get an Exception when
            // we try to call it
            if (arrOnDeviceShakeListener != null) {
                // Only trigger the event, when we have any listener
    
                for (int i = arrOnDeviceShakeListener.size() - 1; i >= 0; i--) {
                    arrOnDeviceShakeListener.get(i).onDeviceShaked();
                }
            }
        }
    
    }
    

    In Activity:

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
                sensorHandler = new SensorListener();
                sensorHandler.setOnDeviceShakeListener(new OnDeviceShakeListener() {
                    @Override
                    public void onDeviceShaked() {
                        //....
                    }
                });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that generates/'rolls' two dice. I would like to output these
I'm making a dice game. There are 5 dice in which I have all
So I am trying to create a real simple dice game...more like a dice
There is a conversion process that is needed when migrating Visual Studio 2005 web
There are a few ways to get class-like behavior in javascript, the most common
I have a sequence of integers representing dice in F#. In the game in
Is there a better way to do this: perms = product(range(1,7),range(1,7),range(1,7)) so that I
Is there a web site (or something else) that has information on folders and
I'm making a program that, in part, rolls four dice and subtracts the lowest
I am writing a dice game simulator in Python. I represent a roll by

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.