Hi i'm working on a service that collect data from sensor and save a fast fourier trasform in to the db, but when i press the red phone button (to make the screen black) it don't save anything. Any idea?
In addition i noticed that when i stop the service, it continues to read sensors, how should I do?
This is the code that i use to connect and start the service:
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
myService = (IMyService) service;
myService.set(toRec,CAMPIONI_FFT);
}
public void onServiceDisconnected(ComponentName className) {
}
};
void doBindService() {
开发者_如何转开发 bindService(new Intent(SensorsState.this,
SensorService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
unbindService(mConnection);
mIsBound = false;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
doUnbindService();
}
And this is my Service (public recClass recc; is the class that manage the db):
public class SensorService extends Service implements SensorEventListener {
private static final String TAG = "MyService";
private MyServiceBinder myServiceBinder = new MyServiceBinder();
private SensorManager mSensorManager;
public float[] dataBuffer;
public boolean mIsStarted = false;
public FourierClass fft;
public recClass recc;
public String toRec;
public int camp;
MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
return myServiceBinder; // object of the class that implements Service interface.
}
public class MyServiceBinder extends Binder implements IMyService {
public void set(String tr, int cp) {
toRec = tr;
camp = cp;
fft = new FourierClass(camp);
mIsStarted = true;
}
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
// Get the SensorManager
mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);
mSensorManager.registerListener(this,
mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0),
SensorManager.SENSOR_DELAY_NORMAL);
recc = new recClass(this);
recc.open();
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
mIsStarted = false;
recc.close();
}
@Override
public void onStart(Intent intent, int startid) {
Log.d(TAG, "onStart");
mIsStarted = true;
}
@Override
public void onSensorChanged(SensorEvent event) { // SensorEventListener
Sensor sens = event.sensor;
if ((sens.getType() == Sensor.TYPE_ACCELEROMETER) && mIsStarted){
fft.add((float)Math.sqrt((event.values[0]*event.values[0])+(event.values[1]*event.values[1])+(event.values[2]*event.values[2]))); // Add value to the fft
dataBuffer = fft.calculate();
if (dataBuffer != null){
for (int i=0; i<fft.camp;i++){
if (toRec != getString(R.string.nuovo))
recc.addValue(toRec, dataBuffer[i]);
}
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
Thanks
Valerio
You need a WakeLock see this tutorial for details
精彩评论