android 2.3中引入了Download manager,作为一个service来优化长时间下载操作的处理。Download Manager通过处理HTTP链接、监控链接的变化和系统重新启动来确保每一次下载都能成功。
下载文件;
public void funclick(View view){
String serviceString = Context.DOWNLOAD_SERVICE;
DownloadManager downloadManager;
downloadManager = (DownloadManager)getSystemService(serviceString);
Uri uri = Uri.parse("http://img00.hc360.com/it/201206/201206121034589404.jpg");
DownloadManager.Request request = new Request(uri);
long reference = downloadManager.enqueue(request);
}
<uses-permission android:name="android.permission.INTERNET"/>
如果要限制为WIFI下载呢,可以这样:
request.setAllowedNetworkTypes(Request.NETWORK_WIFI);
downloadManager.getRecommendedMaxBytesOverMobile(getApplicationContext());