Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将我的Android服务与db连接_Android_Database_Service - Fatal编程技术网

将我的Android服务与db连接

将我的Android服务与db连接,android,database,service,Android,Database,Service,您好,我正在开发一项服务,该服务从传感器收集数据,并将快速傅立叶变换保存到数据库中,但当我按下红色电话按钮(使屏幕变黑)时,它不会保存任何内容。有什么想法吗 此外,我注意到,当我停止服务时,它会继续读取传感器,我该怎么办 这是我用来连接和启动服务的代码: private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName cla

您好,我正在开发一项服务,该服务从传感器收集数据,并将快速傅立叶变换保存到数据库中,但当我按下红色电话按钮(使屏幕变黑)时,它不会保存任何内容。有什么想法吗

此外,我注意到,当我停止服务时,它会继续读取传感器,我该怎么办

这是我用来连接和启动服务的代码:

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();
    }
这是我的服务(public recClass recc;是管理数据库的类):

公共类SensorService扩展服务实现SensorEventListener{
私有静态最终字符串TAG=“MyService”;
私有MyServiceBinder MyServiceBinder=新的MyServiceBinder();
私人传感器管理器;
公共浮动[]数据缓冲;
公共布尔值启动错误=false;
公共类fft;
公共recc类recc;
公共字符串toRec;
公共营地;
媒体播放器;
@凌驾
公共IBinder onBind(意向){
返回myServiceBinder;//实现服务接口的类的对象。
}
公共类MyServiceBinder扩展了Binder实现了IMyService{
公共无效集(字符串tr,int cp){
toRec=tr;
camp=cp;
fft=新的Fourier类(camp);
错误开始=正确;
}
}
@凌驾
public void onCreate(){
Log.d(标记为“onCreate”);
//获取传感器管理器
msSensorManager=(SensorManager)getSystemService(传感器服务);
mSensorManager.registerListener(此,
MSSensorManager.getSensorList(传感器类型\加速计).get(0),
传感器管理器。传感器延迟(正常);
recc=新的recClass(本);
recc.open();
}
@凌驾
公共空间{
Log.d(标签“onDestroy”);
错误启动=错误;
recc.close();
}
@凌驾
公共无效启动(Intent Intent,int startid){
Log.d(标签“onStart”);
错误开始=正确;
}
@凌驾
SensorChanged(SensorEvent事件)上的公共无效{//SensorEventListener
传感器传感器=事件传感器;
if((sens.getType()==Sensor.TYPE\u加速计)&启动错误){
fft.add((float)Math.sqrt((event.values[0]*event.values[0])+(event.values[1]*event.values[1])+(event.values[2]*event.values[2]);//向fft添加值
dataBuffer=fft.calculate();
if(dataBuffer!=null){

对于(int i=0;i您需要WakeLock查看详细信息,但如果我尝试播放屏幕为黑色时播放的歌曲,为什么它不保存到db,而是播放歌曲?我尝试了以下方法:pm=(PowerManager)getSystemService(Context.POWER_SERVICE);wl=pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,“wlTag”);wl.acquire();但它在wl.acquire()上崩溃;PS:我在清单上添加了它,它没有崩溃,但它没有将数据保存到数据库
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

}