I am new to Android. I am developing an application which logs sensor data. I have an activity with two buttons. When the user presses the start button, I want a service to be started and log sensor data until the user presses the stop button. I am not sure which type of service I want to use. I read about local vs remote services but I didn't quite actually understand the difference.
What I tried till now:
I created an activity with two buttons which start and stop a local service:
//Start Service
startService(new Intent(this, MyService.class));
//Stop Service
stopService(new Intent(this, MyService.class));
I also created a sticky service which onStartCommand()
begins to log sensor data:
public int onStartCommand(Intent intent, int flags, int startId) {
mSensorManager.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, magnetometer,
SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, lightSensor,
SensorManager.SENSOR_DELAY_UI);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return Service.START_STICKY;
}
AndroidManifest.xml:
<service android:name=".MyService" android:enabled="true" android:process=":HelloSensors_background"/>
It works well, however the problem is that when I kill the application which started the service, this service restarts and loses all the previously logged data. What shall I use in order to have a service which runs smoothly and continue to run even when the application is killed so as not to lose any logged data please?