# Thread in einer Service erstellen



## guguli (19. Nov 2014)

Hallo zusammen,

ich habe eine Klasse MyService wo mit ich eine Sercive erstelle.aus der Klasse MyService rufe ich eine methode start() aus der Klasse Modulhandler und von hier aus starte ich mit Accelerometer. Nun möchte ich einen Thread dieser Service in der Klasse CollectData starte, worauf ich dann die Methode  collectDataEveryTenSec() aufrufe. 

```
public class MyService extends Service {


    private static final String TAG = "com.example.ServiceExample";

    private ModuleHandler mModuleHandler;
    private CollectData mCollectData;
    @Override
    public void onCreate() {
        Log.i(TAG, "Service onCreate");
        mModuleHandler = new ModuleHandler(this);
        mModuleHandler.start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "Service onStartCommand");
        return Service.START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        Log.i(TAG, "Service onBind");
        return null;
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "Service onDestroy");
        mModuleHandler.stop();
    }
}
```


```
public class ModuleHandler {
    private Context mContext;
    private final Accelerometer mAccelerometer;
    private boolean mRunning = false;

    public ModuleHandler(Context context) {
        mContext = context;
        mAccelerometer = new Accelerometer(mContext);
    }

    public void start() {
        if (!mRunning) {
            mAccelerometer.start();
            mRunning = true;
        }
    }

    public void stop() {
        if(mRunning) {
            mAccelerometer.stop();
            mRunning = false;
        }
    }
```


```
public Accelerometer(Context context) {
        mContext = context;

        sensorManager = (SensorManager) mContext
                .getSystemService(Context.SENSOR_SERVICE);
        if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {
            // success! we have an accelerometer

            accelerometer = sensorManager
                    .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            vibrateThreshold = accelerometer.getMaximumRange() / 2;
        } else {
            // fai! we dont have an accelerometer!
        }

        // initialize vibration
        v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // clean current values
        MainActivity.displayCleanValues();
        // display the current x,y,z accelerometer values
        MainActivity.displayCurrentValues();

        // get the change of the x,y,z values of the accelerometer
        deltaX = Math.abs(lastX - event.values[0]);
        deltaY = Math.abs(lastY - event.values[1]);
        deltaZ = Math.abs(lastZ - event.values[2]);

        // if the change is below 2, it is just plain noise
        if (deltaX < 2)
            deltaX = 0;
        if (deltaY < 2)
            deltaY = 0;
        if (deltaZ < 2)
            deltaZ = 0;

        // set the last know values of x,y,z
        lastX = event.values[0];
        lastY = event.values[1];
        lastZ = event.values[2];
        vibrate();



    }

    // if the change in the accelerometer value is big enough, then vibrate!
    // our threshold is MaxValue/2
    public void vibrate() {
        if ((deltaX > vibrateThreshold) || (deltaY > vibrateThreshold)
                || (deltaZ > vibrateThreshold)) {
            v.vibrate(50);
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    protected void stop() {
        sensorManager.unregisterListener(this);

    }

    protected void start() {
        sensorManager.registerListener(this, accelerometer,
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    public static float getDeltaX() {
        return deltaX;
    }

    public static float getDeltaY() {
        return deltaY;
    }

    public static float getDeltaZ() {
        return deltaZ;
    }

    public float signalMagnitudeArea() {
        float sma = 0;

        return sma;
    }
```


```
public class CollectData extends Thread {



    public CollectData() {


    }


    public void collectDataEveryTenSec() {

    }
```

ich weiss nicht wie ich das machen kann. kann mir einer bitte dabei helfen???

THX


----------

