views:

36

answers:

2

I, i need to create a service that reads sensors, do a fast fourier transform and save the results in to the db, but i need to comunicate 2 values to the service when i start it (how many values to take and a string for the db). How can i comunicate this to the service?

In addition i need to start the service every a time.

Tnk's Valerio

EDIT: Here there is a good explanation -> http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-iii-android-services/

A: 

You can bind it and call a method on your service in onServiceConnected from the ServiceConnection

fedj
But with this can i communicate with the service wile is running?
Skatephone
Yes, with bindService and your ServiceCommunication object and your binder
fedj
I managed to comunicate with the service but now i have a new problem: if i try the program normaly it's ok, but if a press the red phone button to make the screen black than it doesn't register the values of my sensors? any idea?
Skatephone
A: 

I'm doing this on my app but i don't manage to pass the variables toRec and 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's

Skatephone