-2

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

Service class

`

   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

}

} `

This is my Activity class

`

    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);

                }
            });

                          }
                      }

`

Thank you

4

1 回答 1

0

至少您应该更改设置意图的方式,例如:

public void onClick(View arg0) {
   Intent myIntent = new Intent();
   myIntent.setClass(this, GfosrceService.clas);
   startService(myIntent);
}
于 2012-06-04T10:44:09.020 回答