[英]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. 与其单击,我不希望应用程序在后台运行while循环,并自行进行搜索和保存,而无需任何用户交互。
Is the onStart(); 是onStart(); a good method to put the while loop in so it runs in the background while the app is running?
一个将while循环放入以便在应用程序运行时在后台运行的好方法吗? Also should i use te Runnable interface to dynamically show the user wich prime is found and saved to the file?
我是否还应该使用Runnable界面动态显示已找到素用户并将其保存到文件中?
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.). 如果您的while循环会做很多工作,或者考虑使用AsyncTask(最多几秒钟),则可以使用Service。
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. 通过AsyncTask,可以正确,轻松地使用UI线程。 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. 如@EpicPandaForce所述,如果您要占用大量CPU(例如,mp3),请考虑使用
IntentService
因为它是在另一个线程上执行的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.