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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:23:04+00:00 2026-05-27T05:23:04+00:00

I´m working with accelerometer and the first code is Shake detector: (Code1) if (sensor

  • 0

I´m working with accelerometer and the first code is Shake detector:
(Code1)

if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
        long curTime = System.currentTimeMillis();
        long now = System.currentTimeMillis();

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

            if ((now - mLastTime) > TIME_THRESHOLD) {
              long diff = now - mLastTime;
              float speed = Math.abs(x + y + 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 = x;
              mLastY = y;


              mLastZ = z;

With this i get message, when tho phone is shaked:
(Code2)

  mSensorListener.setOnShakeListener(new OnShakeListener() {

        @Override
        public void onShake() {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Shake!", Toast.LENGTH_SHORT).show();
        }
    });

I have also for loop for saving accelerometer values x,y,z into array every 2 secons:
(Code3)

 if (lastUpdate == -1 || (curTime - lastUpdate) > 2000) {
            lastUpdate = curTime;

            x = values[0];
            y = values[1];
            z = values[2];

                for (int column = 0; column < 3; column++) {
                    if (column == 0) {
                        p[row][column] = values[0];

                    }
                    if (column == 1) {
                        p[row][column] = values[1];
                        //yacc.setText("Os X: " + p[row][column]);
                    }
                    if (column == 2) {
                        p[row][column] = values[2];
                        //zacc.setText("Os X: " + p[row][column]);
                    }}
                    if (row == 0) {
                        xacc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]);
                    }
                    if (row == 1) {
                        yacc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]);
                    }
                    if (row == 2) {
                        zacc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]);
                    }
                    if (row == 3) {
                        z2acc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]);
                    }
                    if (row == 4) {
                        z3acc.setText("Os X: " + p[row][0] + " " + p[row][1] +" " + p[row][2]);
                    }
                    row++;
                if (row == 5) {
                    row = 0;
                }

Code3 is never ending and code1 is shake detector. How can i run it together, maybe with threads (how) or anything else?

  • 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-27T05:23:05+00:00Added an answer on May 27, 2026 at 5:23 am

    if you want to run two loops ( or more ) at the same time , use Threads . just define each loop in one thread, and then start your threads 🙂

    Example :

    First thread :

    public class ThreadForLoopA extends Thread{
      // variables for your Thread ... 
       @Override
       public void run(){
          // your first loop here ...
       }
    }
    

    Second thread :

    public class ThreadForLoopB extends Thread{
      // variables for your Thread ... 
       @Override
       public void run(){
          // your second loop here ...
       }
    }
    

    Start all your threads like this :

    ThreadForLoopA threadA = new ThreadForLoopA();
    ThreadForLoopB threadB = new ThreadForLoopB();
    
    //start threads (the two loops will be executed at the same time)
    threadA.start();
    threadB.start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´m working with sensor accelerometer and i wonder if it´s possible and how to
i am working on an application which is based on accelerometer. i have a
I'm working on an iPhone game that uses the accelerometer for control. I don't
For example. I´m working with accelerometer and i want values x,y,z in another class
I'm working with graphing accelerometer data here, and I'm trying to correct for gravity.
I'm working on Android applications which uses SensorManager for different purposes. During investigation I
I'm working with Apple's Accelerometer Graph Example: http://developer.apple.com/library/ios/#samplecode/AccelerometerGraph/Introduction/Intro.html I'm pushing 2 Graph Views onto
I'm working on an iOS game that primarily uses accelerometer input. Previous programmers set
Working on a program in python to print out the first 1000 prime numbers
I'm working on adding a calibration feature to an accelerometer-driven game. For the calibration,

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.