我一直在使用AsyncTask下載某個文件,并經歷了一些教程,只是未能使進度條隨著下載而移動。代碼是和 AsyncTask 調用一個方法來執行 HTTP 連接,然后返回以正確的方式對數據進行排序,以便為應用程序操作它這是我的AsynTask,在主要活動 private class getFood extends AsyncTask<Void, Integer, Cursor> { private ProgressDialog mProgressDialog; @Override protected Cursor doInBackground(Void... params) { // Create URL object String site = "https://afternoon-ridge-50060.herokuapp.com/allsnacks"; URL url = createUrl(site); // Perform HTTP request to the URL and receive a JSON response back String jsonResponse = null; try { String jsonResponseEmpty = ""; // If the URL is null, then return early. if (url == null) { jsonResponse = jsonResponseEmpty; } HttpURLConnection urlConnection = null; InputStream inputStream = null; try { assert url != null; urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setReadTimeout(20000 /* milliseconds */); urlConnection.setConnectTimeout(25000 /* milliseconds */); urlConnection.setRequestMethod("GET"); urlConnection.setRequestProperty("Authorization", "\"token\": " + token); urlConnection.connect();
2 回答

達令說
TA貢獻1821條經驗 獲得超6個贊
根據安卓文檔:
“onProgressUpdate(Progress...),在調用 publishProgress(Progress...) 后在 UI 線程上調用。執行時間未定義。此方法用于在后臺計算仍在執行時在用戶界面中顯示任何形式的進度。例如,它可用于對進度條進行動畫處理或在文本字段中顯示日志”
使用它

暮色呼如
TA貢獻1853條經驗 獲得超9個贊
您必須發布進度,然后只有 具有適當的值。Integer... values
像這樣:
@Override
protected String doInBackground(Context... params) {
//Part-1 of the task done
publishProgress(20);
//Part-2 of the task done
publishProgress(50);
//Part-3 of the task done
publishProgress(100);
return “success”;
}
添加回答
舉報
0/150
提交
取消