我有一项获取加速计数据的活动。然后我将其转换为Service类。但它没有用。任何人都可以说,这是什么错误
服务类
`
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
}
} `
这是我的Activity类
`
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);
}
});
}
}
`
谢谢
答案 0 :(得分:0)
至少你应该改变设置意图的方式:
public void onClick(View arg0) {
Intent myIntent = new Intent();
myIntent.setClass(this, GfosrceService.clas);
startService(myIntent);
}