简体   繁体   English

Android活动LifeCycle,当应用程序运行时要在while循环中运行的抽动方法?

[英]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.

相关问题 活动崩溃生命周期方法 - android - Activity crash lifecycle method - android Android活动生命周期-取决于安装方法吗? - Android activity lifecycle - different depending on method of install? 关闭飞行模式时,为什么 Android 应用程序 go 通过活动和片段生命周期方法 - Why does an Android app go through the activity and fragment lifecycle methods when airplane mode is turned off 加载我的应用程序时出现 android 生命周期问题 - android lifecycle issue while loading my app Android线程活动生命周期 - Android thread activity lifecycle 在android生命周期中,当从最近的应用列表中滑动应用时,是否会调用任何独特的方法? - In android lifecycle, is there any unique method gets called when app is swiped from recent app list? 用户关闭监视器时调用什么活动生命周期方法 - What activity lifecycle method is called when the user switches off the monitor 当应用程序在Android中未激活时如何运行方法 - How to run a method when app is not active in Android Android Jellybean Activity Lifecycle Bug - Android Jellybean Activity Lifecycle Bug Android - setRequestedOrientation - 活动和视图生命周期 - Android - setRequestedOrientation - Activity and views lifecycle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM