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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:04:54+00:00 2026-06-18T14:04:54+00:00

I’m collecting data in my Android app like follows (this is a stripped down

  • 0

I’m collecting data in my Android app like follows (this is a stripped down version):

public class Main extends Activity {

    ...

    public void log5secs(){
        try {
            CollectData collectData = new CollectData(this);
            // collect data for five seconds in the background
            Thread.sleep(5000);
            Log.d("unregister", "gyro from Main: "+collectData.gyroscopeResults.size());
            fingerprint.finish();
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }        

}

public class CollectData implements SensorEventListener  {

    ArrayList<SensorEvent> gyroscopeResults = new ArrayList<SensorEvent>();

    public CollectData(Context context){
        sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        sensorManager.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void onSensorChanged(SensorEvent event) {
        gyroscopeResults.add(event); 
        Log.d("SensorChanged", "gyro results size: "+gyroscopeResults.size());
    }

    public void finish(){
        unregister();
    }

    private void unregister() {
        Log.d("Unregister", "gyro results size before unregister: "+gyroscopeResults.size());
        sensorManager.unregisterListener(this);
    }
}

So, the log messages show that every time the onSensorChanged event is called, the size of the ArrayList increases (gets to 26 in five seconds).

However, when the size is requested from Main or due to unregister() being called, the size of the ArrayList is zero. In the debugger, I can’t see where it changes.

What happened to my data???

I’m running it on an HTC Sensation XL.

UPDATE: Got some more info! Out of curiosity, I made the ArrayList gyroscopeResults a static field in the Main class. The effect was interesting – the SECOND time the recording was done, the results of the first were visible (but not the first time). I guess what this means is that the primary thread is finishing before the sensor data is being recorded – some sort of buffering issue???

  • 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-18T14:04:55+00:00Added an answer on June 18, 2026 at 2:04 pm

    Your data is not lost, the problem in Thread.sleep() that halts the program for 5 seconds (including sensor collection) then it executes the line fingerprint.finish(); after this it starts the sensor collection.

    So when you call Log.d("Unregister", "gyro results size before unregister: "+gyroscopeResults.size()); you get 0 because no sensor data were recorded while the program was sleeping.

    Here is a few suggestions to make your program working as you expect

    To collect the data for 5 seconds, you can use a Timer (here I am cancelling the timer after 5 seconds, but you can continue collection after clearing the ArrayList to not run out of memory)

    final Timer timer = new Timer();
    final CollectData collectData = new CollectData(MainActivity.this);
    
    timer.schedule(new TimerTask() {
    boolean started = false;
    @Override
    public void run() {
      if (!started) {
        Log.d("unregister", "BEFORE] gyro from Main: " + collectData.gyroscopeResults.size());
        started = true;
      } else {
        collectData.finish();
        Log.d("unregister", "AFTER] gyro from Main: " + collectData.gyroscopeResults.size());
        timer.cancel();
      }
        }
    }, 0, 5000);
    

    Another solution, might be simpler, if you want to keep Thread.sleep() is to run log5secs() within a Thread

    new Thread() {
        public void run() {
            log5secs();
        }
     }.start();
    

    However using Timer will be more appropriate if you want continuous sensor logging which you do something with the data every 5 seconds (e.g. log to file and/or clear ArrayList)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

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.