My past two questions were short and not detailed so I'll try my best this time. I have a large soundboard with around 430 sounds. It is so big, I have to create 2 soundmanagers on some devices. Anyway, on the loading screen, I am trying to implement an AsyncTask. I generally understand its types and its 4 steps, but I do not understand the parameters. Here is a simple AsyncTask for reference.
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Long result) {
}
}
What I need to do in the background is add sounds to my manager like this: SoundManager2.addSound(415, R.raw.rubber);
Please, this is my 3rd question here so if you need ANY other info, don't hesitate to ask, I'll be watching this thread for the next 20 minutes and I'll edit it with the information quickly!
In the example you give...
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {...}
The three types URL, Integer and Long (known as Params, Progress, Result) can be anything you need them to be.
The first (URL
) is the type of parameter you must pass to the .execute(<params>)
method of your AsyncTask
instance. More accurately when you look at the doInBackground()
method you will see URL...
which basically means it will accept an array of URL
. Even if you only need to pass one URL
you must still pass it as a single-item array
URL[] myURLs = new URL[] {<comma-separated URLs here>};
new DownloadFilesTask().execute(myURLs);
In the doInBackground(URL... urls)
method you access the URL
s as urls[0]
, urls[1]
etc or something like for (URL u:urls)
.
The second generic type in this example (Integer
) is the type expected by onProgressUpdate(Integer progress)
. Again this must be passed as an array. For instance if you are downloading 10 files then call it after each file has been downloaded. For example myProgress[0] = 1
to indicate one file has been successfully downloaded. This allows you to update a progress dialog of some sort.
Finally the third generic type (Long
) is again used internally and is the type onDoInBackground(...)
must return and is passed to onPostExecute(Long result)
. Note this is a single result and not an array. Depending on what your result is will depend on how onPostExecute()
should behave.
As I said you can use any types including generic Void
(note capital letter)...
private class MyAysncTask extends AsyncTask<Void, Void, Void>
In this case you don't pass anything to .execute()
and although you can still call publishProgress()
(to call onProgressUpdate()
) you can't pass any data to it. Similarly, onPostExecute
will not receive any actual result data.