开发者

How to get Async to run thread in background?

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.

  1. What would I put in the params
  2. Does this go in another class or in my main oncreate method.
  3. 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?": AsyncTask is a class which you are supposed to subclass to do what you need, it is not a piece of code which can inlined in your onCreate. You could make an anonymous AsyncTask class 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 here

onPostExecute(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 the AsyncTask, passing the objects you put as the params into the doInBackground(Params...) method of the task. So the answer as to what to put as params is whatever you need to have access to in doInBackground(Params...).

That should be a decent overview for your needs but you should really check out the docs.


To start the AsyncTask, you simply go

(new UpdateFeedTask()).execute(activityInstance);

It can go where ever you want it, though where you put it might limit access to the variables and objects you want to interact with. E.g. private internal class will have access to them while an entirely new class might not have as easy of an access.

doInBackground(MyActivity... params)

is where the parameter you passed into the execute() function will go, and you can access it via params[0].

You should not call any methods in the AsyncTask class, besides execute().


1. What would I put in the params It depends. The First parameter is what the task will take in. The last generic is what it will return.

2.Does this go in another class or in my main oncreate method. You call the execute method when the you want the task to run. The implementation can be in the Activity class or in a different .java file.

3.When would I call the methods? You only call the execute method. That will make the task run in the background. Then the task will call onPostExecute when it is done.

Here is an example

private class UpdateFeedTask extends AsyncTask<String, Void, DataReturnType> {
    @Override
    protected DataReturnType doInBackground(String... params) {
        String url = params[0];
        //Get data from URL

    }

    @Override
    protected void onPostExecute(ReturnDataType result) {
        //Do something with result
    }
}

Then call the task using the execute("http://foo.com")

Also add android:configChanges=true to the Activity in the manifest. This will make sure that the activity is not killed when the task is still running in the background. Otherwise the task will finish and report back to a null Activity unless you tell the task to callback the new activity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜