2011-05-04 40 views

Trả lời

26

Có thể sử dụng lớp download manager android mà tôi tìm thấy ở đây

Vâng, mặc dù đó là chỉ có sẵn từ API Android Cấp 9 (phiên bản 2.3). Here is a sample project thể hiện việc sử dụng DownloadManager.

+1

@ CommonsWare..Tôi đã trải qua tất cả các mã của bạn. Tất cả đều tuyệt vời. Tôi muốn sử dụng DownloadManager với các phương tiện wifi .. Tôi phải sử dụng SFTP và tải xuống tệp từ bộ định tuyến .. Có thể không? sẽ hướng dẫn tôi về việc này .. – NovusMobile

+2

@Satyam: 'DownloadManager' không hỗ trợ SFTP. Không có gì trong Android hỗ trợ SFTP AFAIK. Bạn sẽ cần phải tìm một JAR của bên thứ ba cho điều đó. – CommonsWare

+0

Tôi đã tìm thấy API mà tôi đã sử dụng trong tên java "JSCH". @ CommonsWare Trong dự án java của tôi chạy hoàn hảo nhưng ở đây Android của nó không .. là nó có thể? hoặc tôi có thể gặp phải vấn đề khác không? xin trả lời .. – NovusMobile

6

Sử dụng DownloadManager lớp (Gingerbread và mới hơn chỉ)

Gingerbread mang một tính năng mới, Download Manager, cho phép bạn tải về các file một cách dễ dàng và ủy công việc khó khăn của đề xử lý, suối, vv để hệ thống.

Đầu tiên, chúng ta hãy xem một phương pháp hữu ích: Tên

/** 
* @param context used to check the device version and DownloadManager information 
* @return true if the download manager is available 
*/ 
public static boolean isDownloadManagerAvailable(Context context) { 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { 
     return true; 
    } 
    return false; 
} 

Phương pháp của giải thích tất cả. Khi bạn chắc chắn có sẵn Trình quản lý tải xuống, bạn có thể thực hiện một việc như sau:

String url = "url you want to download"; 
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); 
request.setDescription("Some descrition"); 
request.setTitle("Some title"); 
// in order for this if to run, you must use the android 3.2 to compile your app 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
    request.allowScanningByMediaScanner(); 
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
} 
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext"); 

// get download service and enqueue file 
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
manager.enqueue(request); 

Tiến độ tải xuống sẽ hiển thị trên thanh thông báo.

3
DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
Uri uri = Uri.parse("http://www.example.com/myfile.mp3"); 
DownloadManager.Request request = new DownloadManager.Request(uri); 
request.setTitle("My File"); 
request.setDescription("Downloading"); 
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
request.setDestinationUri(Uri.parse("file://" + folderName + "/myfile.mp3")); 
downloadmanager.enqueue(request); 
Các vấn đề liên quan