Call SensorManager from AsyncTask?
<p>How do I call the SensorManager from inside -</p>

<pre><code>protected String doInBackground(String... params) {
.....
}
</code></pre>

<p>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!!</p>

<p>This is the update... The file written is empty...How do I start the logging?</p>

<pre><code>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&lt;String, Void, String&gt; {
    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();
    }
}
}
</code></pre>
