Learn how to start the service only while the app is closed.

Asked 2 years ago, Updated 2 years ago, 41 views

I would like to start the service only when I return from the currently open screen (MainActivity) or press the Home button to do something else, and give notifications every certain time.
When I go back to the app, I feel like I'm going to stop the service stop.
If anyone knows how to do it, please take care of me.

android java

2022-09-30 16:38

1 Answers

Simply
Run startService() in Activity.onPause()
Run stopService() in Activity.onResume()
Can't we?

話It's a little off topic, but
 The startService() may automatically terminate from the Android system.
 You can use startForeground() to suppress automatic termination in a significant number of situations, but
 Notification indicates that the service is starting.


2022-09-30 16:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.