Tôi muốn phân tích một trang web và hình ảnh theo kiểu progressdialog theo chiều ngang và tăng nó byte thành byte, nó có khả năng?Android: AsyncTask, làm thế nào có thể cập nhật ProgressDialog increment
6
A
Trả lời
20
Hãy thử một cái gì đó như thế này,
Tạo một ProgressDialog.
ProgressDialog mProgressDialog = new ProgressDialog(Your_Activity.this);
mProgressDialog.setMessage("Here you can set a message");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.show();
MyAsyncTask obj = new MyAsyncTask();
obj.execute("url");
Lớp AsyncTask của bạn.
private class MyAsyncTask extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... url) {
int count;
try {
URL url = new URL(url[0]);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int length = connection.getContentLength();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/file_name.txt");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int)(total*100/length));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
@Override
public void onProgressUpdate(String... args){
mProgressDialog.setProgress(args[0]);
}
}
}
Bạn phải từ bỏ những Permission trong file AndroidManifest.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+0
Trong một số URL, bạn sẽ luôn nhận được -1, sau đó sử dụng mã này HttpURLConnection connection = (HttpURLConnection) url.openConnection(); \t \t connection.setRequestMethod ("HEAD"); \t \t connection.connect(); \t \t int length = connection.getContentLength(); –
Các vấn đề liên quan
- 1. ProgressDialog không muốn cập nhật thông báo
- 2. Làm thế nào để sử dụng AsyncTask để hiển thị ProgressDialog trong khi thực hiện công việc nền trong Android?
- 3. Làm thế nào bạn có thể cập nhật động màu của một SeekBar?
- 4. Làm thế nào để sử dụng ExecutorService với Android AsyncTask?
- 5. Android ProgressDialog wont spin
- 6. Android AsyncTask bên trong AsyncTask
- 7. Android AsyncTask Bắt đầu Một AsyncTask
- 8. AsyncTask Không cập nhật trên chuỗi giao diện người dùng
- 9. ProgressDialog sa thải trong android
- 10. Mẫu WeakReference/AsyncTask trong android
- 11. Android: ProgressDialog không hiển thị
- 12. Android: SeekBar Increment Value - Cách
- 13. ProgressDialog hoặc ProgressBar làm chậm ứng dụng chạy Android 4.1
- 14. Ứng dụng Android không có trong Market: làm thế nào để đẩy cập nhật?
- 15. Làm cách nào để tôi có thể gỡ lỗi mã DoBackBackground của AsyncTask
- 16. Android: Làm thế nào để có được bối cảnh ứng dụng hiện tại trong AsyncTask?
- 17. ProgressDialog quay vòng tròn
- 18. Android: Làm cách nào tôi có thể chuyển các tham số cho OnPreExecute() của AsyncTask?
- 19. Android 4.0 asynctask cùng lúc không thể
- 20. Android: Làm thế nào để thay đổi màu văn bản của ProgressDialog?
- 21. Cập nhật tiến trình AnimateBar trong Android
- 22. Android: ProgressDialog không hiển thị trong Bộ điều hợp Class
- 23. Làm thế nào tôi có thể cập nhật lên jQuery 1.7+ bằng Rails 3?
- 24. Làm thế nào tôi có thể cập nhật monodevelop3.0.6 và giữ lại mono3.0.3 trong OSX?
- 25. Làm thế nào tôi có thể cập nhật một mục vectơ trong Clojure?
- 26. Làm thế nào tôi có thể cập nhật top 100 hàng trong DB2
- 27. Làm thế nào tôi có thể cập nhật một hàng trong một DataTable trong VB.NET?
- 28. làm thế nào tôi có thể cập nhật nhiều tài liệu trong mongoose
- 29. Tắt mờ nền trên ProgressDialog/AlertDialog trong Android
- 30. Android AsyncTask làm đối số phương thức
Có, có thể http://goo.gl/eeiu5 – Matthieu
Có một ví dụ điển hình? – MimmoG
Tham khảo liên kết mẫu này: https://sites.google.com/site/androidhowto/how-to-1/create-a-custom-progress-bar-using-asynctask – Uttam