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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:18:37+00:00 2026-05-31T06:18:37+00:00

How do I call the SensorManager from inside – protected String doInBackground(String… params) {

  • 0

How do I call the SensorManager from inside –

protected String doInBackground(String... params) {
.....
}

I have to log the acclerometer data on to a file. This application runs in the background. Hence I am using AsyncTask to implement it. I need to start and stop the logging by pressing the button (onClick). Any pointers would be helpful!!

This is the update… The file written is empty…How do I start the logging?

public class AsyncTaskActivity extends Activity implements SensorEventListener{
Button btn;
File root = Environment.getExternalStorageDirectory();
StringBuilder builder = new StringBuilder();
File gpxfile = new File(root, "TestFile.txt");
String fileText = "Data";
FileWriter gpxwriter; 
BufferedWriter out;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            new LongOperation(new AsyncTaskActivity()).execute("");
        }
    });
}




private class LongOperation extends AsyncTask<String, Void, String> {
    private AsyncTaskActivity mActivity;
    public LongOperation (AsyncTaskActivity activity) {
        mActivity = activity;
    }
      @Override
      protected String doInBackground(String... params) {
          try {
                gpxwriter = new FileWriter(gpxfile);
                out = new BufferedWriter(gpxwriter);
                if (root.canWrite()){
                    SensorManager manager = (SensorManager)      mActivity.getSystemService(Context.SENSOR_SERVICE);
                    Sensor accelerometer = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
                    if(!manager.registerListener((SensorEventListener) this, accelerometer, SensorManager.SENSOR_DELAY_GAME)){                              
                            System.out.println("Could not start!!!!!!!!!");
                    }
                    out.write("Start");
                    out.newLine();                                  
                    out.close();
                }
            } catch (IOException e) {
                Log.e("doIn","Could not write file " + e.getMessage());
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }             
            return null;
      }      

      @Override
      protected void onPostExecute(String result) {  
          TextView txt = (TextView) findViewById(R.id.output);
          txt.setText("File written successfully!!!");
      }

      @Override
      protected void onPreExecute() {

      }

      @Override
      protected void onProgressUpdate(Void... values) {
      }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    builder.setLength(0);
    builder.append(event.values[0]+","+event.values[1]+","+event.values[2]);
    try {
        out.append(builder.toString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}
  • 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-31T06:18:39+00:00Added an answer on May 31, 2026 at 6:18 am

    Pass the Activity Instance to the constructor of the AsyncTask and then access the Sensor Manager

    class MyTask extends AsyncTask<URL, Integer, List> {
        private MainActivity mActivity;
        public MyTask(MainActivity activity) {
            mActivity = activity;
        }
    
        protected List doInBackground(URL... url) {
            SensorManager mSensorManager = (SensorManager)
                    mActivity.getSystemService(SENSOR_SERVICE)
            ....
            return ..;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I call a javascript function from a textbox by using OnKeyPress=clickSearchButton() Here is my
I call a certain div from another page with jquery to be loaded into
Call like: mAsyncRunner.request(me, params,new AsyncFacebookRunner.RequestListener() returns me all info for user /according to permisions/.
I call a template like this from my view: return render_to_response('mytemplate.html', context_instance=RequestContext(request)) I'm trying
Function call: $trdata .= $this->table_td($tddata, 1, $td); Function: public function table_td($data = '', $parameters
call = https://aaa@api.twilio.com/2010-04-01/Accounts/sasa/Calls.xml fields = { To : +12321434, From : req.body.from } request.post
I call some cloud that allows to execute groovy scripts. I return data as
Call this from a QWidget: Parser->xmlParser(_layout, xmlPath, comingFrom, mapCount); , but get two No
call from main matrix_multiplication(arrayA,arrayB,row1,col1,col2); function definition float matrix__multiplication(float **arrayA,float **arrayB,int row1,int col1,int col2) {
Call it OCD or an just obsessive need to separate logic from presentation, but

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.