Can an Android AsyncTask doInBackground be synchronized to serialize the task execution?

前端 未结 4 596
滥情空心
滥情空心 2020-12-03 12:06

Is it possible to make AsyncTask.doInBackground synchronized - or achieve the same result in another way?



        
4条回答
  •  星月不相逢
    2020-12-03 12:31

    AsyncTask is used to run a background thread so that you current process is not interupted .

    private class DownloadFilesTask extends AsyncTask {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
         }
         return totalSize;
     }
    
     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }
    
     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
    

    }

    where first of all your doInBackground function iscalled and the returned object will move to on post execute. which line of code you want to run after some process you can put that in PostExecute function. this will surely help you

提交回复
热议问题