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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:27:35+00:00 2026-06-11T19:27:35+00:00

I have a class, Recorder, which captures sensor data and writes it to a

  • 0

I have a class, Recorder, which captures sensor data and writes it to a database. All the database handling occurs in Recorder. Here I’m running it in a new thread. mContext comes from getApplicationContext().

        Runnable startRunnable = new Runnable(){
            public void run() {
                Recorder recorder = new Recorder((SensorManager) getSystemService(SENSOR_SERVICE), mContext);
                recorder.start();
            }
        };
        recorderThread = new Thread(startRunnable);
        recorderThread.start();

Here are the relevant bits for Recorder:

public Recorder(SensorManager sensorManager, Context context){
    mSensorManager = sensorManager;
    mContext = context;
}

void start(){               
    List <Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
    for(Sensor sensor : sensorList){
        mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);
    }
}

public void onSensorChanged(SensorEvent event) {
    SensorDatabase dbHelper = new SensorDatabase(mContext);
    mDb = dbHelper.getWritableDatabase();

    if(mDb != null && mDb.isOpen()){
        ContentValues values = new ContentValues();
        values.put(DataColumns.ACCURACY, event.accuracy);
        // more values stuff ... 

        mDb.insert(SensorDatabase.TABLE_NAME, null, values);
        mDb.close();
    }
}

The application records the data, but is getting ANR errors. If I make it so that the database doesn’t get initialized in Recorder, there is no ANR error.

How could this Runnable be blocking the UI thread here? There are no callbacks, and the UI thread never tries to open a database connection, at least it isn’t my intention to do so.

I looked into traces.txt for the ANR, it said thread “main” was executing:

 android.database.sqlite.SQLiteStatement.native_executeSql(Native Method) 

Any pointers? I’m pretty confused.

  • 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-11T19:27:36+00:00Added an answer on June 11, 2026 at 7:27 pm

    How could this Runnable be blocking the UI thread here?

    The Runnable is not blocking the main application thread (a.k.a., “UI thread”). onSensorChanged() is blocking the main application thread, because onSensorChanged() is called on the main application thread, and therefore you are doing disk I/O on the main application thread.

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

Sidebar

Related Questions

I have Class and Student objects. Both have collection of another as property. Which
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
We have class lua. In lua class there is a method registerFunc() which is
I have class like this below shown. which contains the shopping items where the
I have an audio recorder class in my application. It worked fine previously and
I have a class which inherits QAbstractTableModel, and holds some complex structs in a
I have the below code where my Mock interface has a Recorder property which
I basically have this program which fetches orders from a database. My problem is
I have a simple class that looks a bit like this: @protocol Recorder @property(BOOL)
I have a timer class which uses the date / time of the local

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.