# Android Service läuft nicht in Sandby weiter



## Excess (29. Jul 2018)

Moin,

Ich schreibe gerade eine kleine App, um den LKuftdruck lpermanent zu messen und alle 10 Minuten zu speichern.
Das habe ich mit Hilfe eines Service umgesetzt ... nur, wenn ich das Tablet in den Standby schalte ( also den Bildschirm ausstelle) dann läuft der Service nicht weiter 

Kann mir da jemand einen Tip geben ?

Vielen Dank im Voraus

```
]import android.app.Service;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class recordBarService extends Service {

    private SensorManager  sensorManager;
    private Sensor         pressureSensor;
    private SQLiteDatabase database;
    private float          actualValue;

    private SensorEventListener sensorEventListener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            actualValue = sensorEvent.values[0];
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int i) {
        }
    };
    private Handler             handler;

    @Override
    public void onCreate() {
        super.onCreate();

        handler = new Handler(Looper.getMainLooper());

        database = getBaseContext().openOrCreateDatabase("barTable", MODE_PRIVATE, null);
        database.execSQL("CREATE TABLE IF NOT EXISTS barTable (bar REAL, date TEXT)");

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        pressureSensor = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);

        sensorManager.registerListener(sensorEventListener, pressureSensor, SensorManager.SENSOR_DELAY_UI);

        mTimer = new Timer();
        mTimer.schedule(timerTask, 2000, 600000);

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("service started");
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "bar record service started",
                    Toast.LENGTH_LONG).show();
            }
        });
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {

        System.out.println("service onDestroy");

        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "bar record service destroyed",
                    Toast.LENGTH_LONG).show();
            }
        });

        super.onDestroy();

        try {
            mTimer.cancel();
            timerTask.cancel();
        } catch (Exception e) {
            e.printStackTrace();
        }

        sensorManager.registerListener(sensorEventListener, pressureSensor, SensorManager.SENSOR_DELAY_UI);

        database.close();
        if (handler != null)
            handler = null;

        //Intent intent = new Intent("de.excess.app.wtf");
        //sendBroadcast(intent);
    }

    private Timer mTimer;

    TimerTask timerTask = new TimerTask() {

        @Override
        public void run() {
            if (database.isOpen()) {
                String date = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date());
                database.execSQL("INSERT INTO barTable (BAR,DATE) VALUES ( " + actualValue + ",'" + date + "')");
            }
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
```


----------



## mihe7 (29. Jul 2018)

https://stackoverflow.com/questions/8713361/keep-a-service-running-even-when-phone-is-asleep


----------



## Excess (30. Jul 2018)

danke


----------

