0

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!

Spoofy
  • 621
  • 4
  • 9
  • 27
  • An operation like the one you describe is best done in a background thread. If you want the option of displaying the data from the function call, look into using an AsyncTask. – VERT9x May 12 '15 at 20:42

1 Answers1

1

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.

issathink
  • 1,220
  • 1
  • 15
  • 25
  • 2
    I personally recommend an `IntentService` because it is executed on another thread. – EpicPandaForce May 12 '15 at 20:48
  • I know :D I just figured I'd mention that extending `Service` itself would be a bad choice because it still executes on the UI Thread – EpicPandaForce May 12 '15 at 21:07
  • is it possible to pause AsyncTask with onPause() and then start the AsyncTask in the onResume() ? i manage to stop the Task with: `protected void onPause() { super.onPause(); Log.d("Method:","OnPause();"); new MyClass().cancel(true); }` But this dosent work: ` protected void onResume(){ super.onResume(); Log.d("Method:","onResume();"); new MyClass().execute(); }` – Spoofy May 13 '15 at 01:31
  • Have a look at this : http://stackoverflow.com/questions/2474584/pause-and-resume-asynctasks-android – issathink May 13 '15 at 10:13