0
嗨,我正在從傳感器收集數據並將一個快速傅立葉變換保存到數據庫的服務,但是當我按下紅色電話按鈕(使屏幕變成黑色)時不要保存任何東西。任何想法?將我的Android服務與db連接
另外我注意到,當我停止服務時,它會繼續讀取傳感器,我該怎麼辦?
這是我用來連接和啓動服務的代碼:
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();
}
這是我的服務(公共recClass RECC;是管理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
}
}
感謝
瓦萊里奧
但如果我嘗試播放一首歌曲是邊玩邊屏幕是黑的,爲什麼它不保存到數據庫,但它播放的歌曲? – Skatephone 2010-08-23 12:42:23
我試過這個: pm =(PowerManager)getSystemService(Context.POWER_SERVICE); wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,「wlTag」); wl.acquire(); 但它崩潰wl.acquire(); – Skatephone 2010-08-23 16:01:56
PS:我在我的清單中添加了 並且它不會崩潰,但它不會將數據保存到db –
Skatephone
2010-08-23 16:43:00