与Android服务通信

时间:2010-08-17 12:31:50

标签: android service

我需要创建一个读取传感器的服务,进行快速傅里叶变换并将结果保存到数据库中,但是当我启动它时,我需要将2个值传递给服务(需要多少个值和一个字符串对于db)。我该如何与服务沟通?

此外,我需要每次启动服务。

编辑:这里有一个很好的解释 - > http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-iii-android-services/

2 个答案:

答案 0 :(得分:0)

您可以绑定它并在ServiceConnection上的onServiceConnected中调用服务上的方法

答案 1 :(得分:0)

我在我的应用上执行此操作,但我无法将变量传递给Rec和camp:

private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mBoundService = ((SensorService.LocalBinder)service).getService();
            Toast.makeText(SensorsState.this, R.string.local_service_connected,
                    Toast.LENGTH_SHORT).show();
            mBoundService.toRec=toRec;
            mBoundService.camp=CAMPIONI_FFT;
        }

        public void onServiceDisconnected(ComponentName className) {
            mBoundService = null;
            Toast.makeText(SensorsState.this, R.string.local_service_disconnected,
                    Toast.LENGTH_SHORT).show();
        }
    };

    void doBindService() {
        bindService(new Intent(SensorsState.this, 
                SensorService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
    }

    void doUnbindService() {
        if (mIsBound) {
            unbindService(mConnection);
            mIsBound = false;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        doUnbindService();
    }

TNK的

相关问题