Using Services for this task
I have tried to search many discussion threads but I am not clear on this thing. Being new I am unable to understand half of the things hence I am going to ask the direct question.
For my application when the final button (start analysis button) is clicked, I want a service to start, the analysis to be done inside the service is long almost 2-3 minutes, once the analysis has been done I want the service to automatically start a result activity page present in my main activity.
How can this task be achieved ? and I would also like to know that if the application will run in background and will not be closed by android automatically when on pause ?
The definition of Android service is that it can run in the background, devoid of UI. So yes, it is possible. However, a Service by itself does not automatically run in the background (it runs on the main thread). Instead, what you want to do is probably something like this:
- Have a service which listens for intents that say to start the operation.
- When your user clicks the button, send the intent to that Service.
- The service will start a new background thread, do some work for a while.
- After finishing its work, the Service will start a new Activity. (Like this..)
Please take note, that it is considered extremely bad practice to start a new Activity if the user is not using your app. It's horrible if you're using a chat application and a random app starts taking over... Instead, a better idea is to stick a flag somewhere and then in the onResume() handler of your other Activity, check if the task is finished, and if so send the user to the other Activity.