I am pulling around 1500 data plots and adding them to overlays for an map view. I want to run this in another thread while the rest of my program finishes starting up. I would like a progress spinner to spin only on the map portion while its loading the data plot points.
I have searched and found what I need, but Im not sure how to implement it and where in my code to put it.
- What would I put in the params
- Does this go in another class or in my main oncreate method.
-
When would I call the methods?
private class UpdateFeedTask extends AsyncTask<MyActivity, Void, Void> { private ProgressDialog mDialog; protected void onPreExecute() { Log.d(TAG, " pre execute async"); mDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Retrieving data ...", true); } protected void onProgressUpdate(Void... progress) { Log.d(TAG, " progress async"); } @Override protected Void doInBackground(MyActivity... params) { // TODO Auto-generated method stub return null; } protected void onPostExecute(Void result) { Log.d(TAG, " post execute async"); mDialog.dismiss(); } }
From your question I actually am not a hundred percent sure what you currently understand about
AsyncTasks so this may be a little some stuff you already know but bear with me.“Does this go in another task or in my onCreate method?”:
AsyncTaskis a class which you are supposed to subclass to do what you need, it is not a piece of code which can inlined in youronCreate. You could make an anonymousAsyncTaskclass in your onCreate, but usually you want it as either a private internal class or its own class entirely.As for when you call the methods; you don’t they are lifecycle events.
onPreExecute()is called on the UI thread just before starting the background work and is a place to do things such as modify components to show progress, or throw up a dialog.doInBackground(Params...)is the main method which runs in the background on another thread, do your work here. Do not try to modify UI hereonPostExecute(Result) is when your task has finished and is run on the UI thread again. This is where you should handle your UI update.
You only call
execute(Params..), which will start theAsyncTask, passing the objects you put as the params into thedoInBackground(Params...)method of the task. So the answer as to what to put as params is whatever you need to have access to indoInBackground(Params...).That should be a decent overview for your needs but you should really check out the docs.