简体   繁体   中英

Android activity LifeCycle, whitch method to run while loop when app is runnig?

I have created an app that is searhcing for bigger and bigger primes and saves them in a textfile. Right know im letting the user to click a button to make the app search for bigger primes and save the to file.

Istead of clicking i want the app to run a while loop in the background and do the searching and saving by it self without any user interaction.

Is the onStart(); a good method to put the while loop in so it runs in the background while the app is running? Also should i use te Runnable interface to dynamically show the user wich prime is found and saved to the file?

Thank you in advance!

You can use Service if your while loop will do a lot of work or consider using AsyncTask (a few seconds at the most.).

A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use ( here )

AsyncTask enables proper and easy use of the UI thread. here

As mentioned @EpicPandaForce, if you're doing a CPU intensive(mp3 for eg.) consider using an IntentService because it is executed on another thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM