2011-05-04 40 views

回答

26

是否有可能使用Android的下載管理器類,我發現這裏

是的,雖然這僅僅是由於Android API 9級(2.3版本)提供。 Here is a sample project演示使用DownloadManager

+1

@ CommonsWare..I通過你的所有代碼。所有都是偉大的去了。 我想利用DownloadManager與wifi手段..我必須使用SFTP和從路由器下載文件..這是可能的嗎? 將請你指導我.. – NovusMobile 2012-06-07 12:55:37

+2

@Satyam:'DownloadManager'不支持SFTP。 Android中沒有任何內容支持SFTP AFAIK。您需要爲此找到第三方JAR。 – CommonsWare 2012-06-07 12:58:16

+0

我發現我在java名稱「JSCH」中使用的API。 @ CommonsWare在我的Java項目中,它的運行完美,但在這裏,Android不是.. 是否有可能? 或者我可能面臨其他問題? 請回復.. – NovusMobile 2012-06-07 13:05:45

6

使用下載管理器類(薑餅,只有更新版本)

薑餅帶來了新的功能,下載管理器,它可以讓你輕鬆下載文件,並委託處理線程的辛勤工作,溪流等系統。

首先,讓我們來看看一個實用方法:

/** 
* @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; 
} 

方法的名稱解釋這一切。一旦確定下載管理器是可用的,你可以做這樣的事情:

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); 

下載進度將在通知欄展示。

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);