title: How to get accelerometer data within a Service class in android?
body: <p>I have an activity that gets accelerometer data. Then I convert it into a Service class. But it is not working. Can any one say, what is the wrong with this</p>
  
  <p>Service class</p>
  
  <p>`          </p>
  
  <pre><code>   package com.test.testgforce;
  
     import java.security.PublicKey;
     import java.text.DecimalFormat;
     import java.util.Timer;
     import java.util.TimerTask;
  
  
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.IBinder;
  
    public class GforceService extends Service implements SensorEventListener {
  
  SensorManager sensorManager;
  Sensor accelerometer;
  
  private double currentAccel= 0.0f;
      private static DecimalFormat REAL_FORMATTER = new DecimalFormat("0.####");
      double calibration = SensorManager.STANDARD_GRAVITY;
  
  
  
  @Override
  public IBinder onBind(Intent intent1) {
  
      return null;
  }
  
  
  @Override
  public void onCreate() {
  
      super.onCreate();
      sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
      accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
      sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_FASTEST);
  
  
  
  
  
  
  }
  
  
  @Override
  public void onSensorChanged(SensorEvent event) {
       double x = event.values[0];
       double y = event.values[1];
       double z = event.values[2];
  
       double a = Math.round(Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)));
       currentAccel = ((float)(a-calibration));
       double currentG = currentAccel / SensorManager.STANDARD_GRAVITY ;
       WebServiceCaller obj=new WebServiceCaller();
       String acl=Double.toString((Double)currentG);
       String result=obj.sendAccelerationData(acl);
  
  }
  
  
  
  
  
  
  @Override
  public void onDestroy() {
  
  
      sensorManager.unregisterListener(this);
      super.onDestroy();
  
  }
  
  
  @Override
  public void onStart(Intent intent, int startId) {
  
      sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
      accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
      sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_FASTEST);
      super.onStart(intent, startId);
  }
  
  
  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
      // TODO Auto-generated method stub
  
  }
  </code></pre>
  
  <p>}
            `</p>
  
  <p>This is my Activity class</p>
  
  <p>` </p>
  
  <pre><code>    package com.test.testgforce;
  
      import android.app.Activity;
      import android.content.Intent;
      import android.os.Bundle;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
  
      public class TestGforceActivity extends Activity {
       /** Called when the activity is first created. */
  Button gobtn;
      @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
  
      gobtn=(Button)findViewById(R.id.go);
  
  
              gobtn.setOnClickListener(new OnClickListener() {
  
                  @Override
                  public void onClick(View arg0) {
                      Intent myIntent = new Intent();
                        myIntent.setAction("com.test.testgforce.GforceService");
                          startService(myIntent);
  
                  }
              });
  
                            }
                        }
  </code></pre>
  
  <p>`</p>
  
  <p>Thank you</p>