1

I have developed an android watch app. It has a play button which when you click starts the sensor manager to analyse the sensor data. Using the data, I recognize the gestures performed by the user. So when I press the play button, sensor data analysis starts and the user can see a stop button to stop the sensor whenever he wants. So once the sensor is started, if I swipe and go back to the home screen and the again open the app, I can see the play button instead of stop button. I want to see the stop button when the user returns. Ideally I want to run a foreground service to detect the sensor readings.

So I want to start a foreground service when the user hits the play button and when the user open the notification, he must see the stop button to stop the sensor data as well as the service. How can I do this? Please advise.

TheRedOne
  • 105
  • 8

1 Answers1

0

UI:

Save your required UI data to the Bundle of the current states (stop button appearing) using onSaveInstanceState for that instance to your onPause() so that it can be retrieved by the onResume() using onRestoreInstanceState and viewed.

Service:

The Service should be bound to the activity so the two communicate directly. The service can be stopped from the activity using

stopService(new Intent(this, MyService.class));

You can also call stopSelf() from within the Service.

childofthehorn
  • 697
  • 1
  • 4
  • 12