开发者

Communicate with a Android Service

开发者 https://www.devze.com 2023-01-11 18:00 出处:网络
I need to create a service that reads sensors, do a fast fourier tr开发者_Python百科ansform and save the results in to the db, but I need to communicate 2 values to the service when i start it (how ma

I need to create a service that reads sensors, do a fast fourier tr开发者_Python百科ansform and save the results in to the db, but I need to communicate 2 values to the service when i start it (how many values to take and a string for the db). How can I communicate this to the service?

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

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消