Can somebody explain how to use ASyncTask with Android?

后端 未结 4 1141
生来不讨喜
生来不讨喜 2021-01-16 07:13

I\'ve been doing a bunch of research and looking over the documentation for ASyncTask in Android, but I just can\'t seem to wrap my head around it. I simply want to run some

4条回答
  •  旧时难觅i
    2021-01-16 08:01

    The three types used by an asynchronous task are the following:

    1. Params, the type of the parameters sent to the task upon execution. i.e if you want to send some variable/array to your async task background task. You use that information using that variable.

    2. Progress, the type of the progress units published during the background computation. i.e to show the progress of your background progress. ( such as showing how much a video/image is downloaded)

      1. Result, the type of the result of the background computation. i.e the result that you calculated in background process used for passing result to onPostExecute method.

    String[] username;

    username[0]="user1"; username[1]="user2";

    new asynctask().execute(username);

    private class asynctask extends AsyncTask 
    {
    @Override
    protected void onPreExecute()
    {
       // anything you want to do prior starting the async task.
    }
    @Override
    protected String doInBackground(USER... users)
    {
         int count = users.length;
         for (int i = 0; i < count; i++) 
            retriveinformation(users[i]); 
    return "Hey";
    }
     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }
    
     protected void onPostExecute(String result) {
    
             // the value result is HEY that is returned by doInBackground.
     }
    

    }

    Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void.

    information taken from https://developer.android.com/reference/android/os/AsyncTask.html

提交回复
热议问题