1、设置WebView的DownloadListener:
webView.setDownloadListener(new MyWebViewDownLoadListener())
2、实现MyWebViewDownLoadListener这个类,具体可以如下这样:
Java代码
private class MyWebViewDownLoadListener implements DownloadListener{
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,
long contentLength) {
Log.i("tag", "url="+url)
Log.i("tag", "userAgent="+userAgent)
Log.i("tag", "contentDisposition="+contentDisposition)
Log.i("tag", "mimetype="+mimetype)
Log.i("tag", "contentLength="+contentLength)
Uri uri = Uri.parse(url)
Intent intent = new Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
}
}
[java] view plain copy
private class MyWebViewDownLoadListener implements DownloadListener{
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,
long contentLength) {
Log.i("tag", "url="+url)
Log.i("tag", "userAgent="+userAgent)
Log.i("tag", "contentDisposition="+contentDisposition)
Log.i("tag", "mimetype="+mimetype)
Log.i("tag", "contentLength="+contentLength)
Uri uri = Uri.parse(url)
Intent intent = new Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
}
}
这只是调用系统中已经内置的浏览器进行下载,还没有WebView本身进行的文件下载,不过,这也基本上满足我们的应用场景了。
我在项目中的运用
项目要求这样:
1,需要使用WebView加载一个网页;
2,网页中有文件下载的链接,点击后需要下载文件到SDcard;
3,然后自动打开文件;
下面是具体解决办法
第一步,对WebView进行一系列设置。
Java代码
WebView webview=(WebView)layout.findViewById(R.id.webview)
webview.getSettings().setJavaScriptEnabled(true)
webview.setWebChromeClient(new MyWebChromeClient())
webview.requestFocus()
// webview.loadUrl("file:///android_asset/risktest.html")
webview.loadUrl(jcrs_sub.get(position).addr)
// 设置web视图客户端
webview.setWebViewClient(new MyWebViewClient())
webview.setDownloadListener(new MyWebViewDownLoadListener())
//内部类
public class MyWebViewClient extends WebViewClient {
// 如果页面中链接,如果希望点击链接继续在当前browser中响应,
// 而不是新开Android的系统browser中响应该链接,必须覆盖 webview的WebViewClient对象。
public boolean shouldOverviewUrlLoading(WebView view, String url) {
L.i("shouldOverviewUrlLoading")
view.loadUrl(url)
return true
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
L.i("onPageStarted")
showProgress()
}
public void onPageFinished(WebView view, String url) {
L.i("onPageFinished")
closeProgress()
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
L.i("onReceivedError")
closeProgress()
}
}
// 如果不做任何处理,浏览网页,点击系统“Back”键,整个Browser会调用finish()而结束自身,
// 如果希望浏览的网 页回退而不是推出浏览器,需要在当前Activity中处理并消费掉该Back事件。
public boolean onKeyDown(int keyCode, KeyEvent event) {
// if((keyCode==KeyEvent.KEYCODE_BACK)&&webview.canGoBack()){
// webview.goBack()
// return true
// }
return false
}
[java] view plain copy
WebView webview=(WebView)layout.findViewById(R.id.webview)
webview.getSettings().setJavaScriptEnabled(true)
webview.setWebChromeClient(new MyWebChromeClient())
webview.requestFocus()
// webview.loadUrl("file:///android_asset/risktest.html")
webview.loadUrl(jcrs_sub.get(position).addr)
// 设置web视图客户端
webview.setWebViewClient(new MyWebViewClient())
webview.setDownloadListener(new MyWebViewDownLoadListener())
//内部类
public class MyWebViewClient extends WebViewClient {
// 如果页面中链接,如果希望点击链接继续在当前browser中响应,
// 而不是新开Android的系统browser中响应该链接,必须覆盖 webview的WebViewClient对象。
public boolean shouldOverviewUrlLoading(WebView view, String url) {
L.i("shouldOverviewUrlLoading")
view.loadUrl(url)
return true
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
L.i("onPageStarted")
showProgress()
}
public void onPageFinished(WebView view, String url) {
L.i("onPageFinished")
closeProgress()
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
L.i("onReceivedError")
closeProgress()
}
}
// 如果不做任何处理,浏览网页,点击系统“Back”键,整个Browser会调用finish()而结束自身,
// 如果希望浏览的网 页回退而不是推出浏览器,需要在当前Activity中处理并消费掉该Back事件。
public boolean onKeyDown(int keyCode, KeyEvent event) {
// if((keyCode==KeyEvent.KEYCODE_BACK)&&webview.canGoBack()){
// webview.goBack()
// return true
// }
return false
}
第二步,起线程开始下载文件。
Java代码
//内部类
private class MyWebViewDownLoadListener implements DownloadListener {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,
long contentLength) {
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast t=Toast.makeText(mContext, "需要SD卡。", Toast.LENGTH_LONG)
t.setGravity(Gravity.CENTER, 0, 0)
t.show()
return
}
DownloaderTask task=new DownloaderTask()
task.execute(url)
}
}
//内部类
private class DownloaderTask extends AsyncTask<String, Void, String>{
public DownloaderTask() {
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String url=params[0]
// Log.i("tag", "url="+url)
String fileName=url.substring(url.lastIndexOf("/")+1)
fileName=URLDecoder.decode(fileName)
Log.i("tag", "fileName="+fileName)
File directory=Environment.getExternalStorageDirectory()
File file=new File(directory,fileName)
if(file.exists()){
Log.i("tag", "The file has already exists.")
return fileName
}
try {
HttpClient client = new DefaultHttpClient()
//client.getParams().setIntParameter("http.socket.timeout",3000)//设置超时
HttpGet get = new HttpGet(url)
HttpResponse response = client.execute(get)
if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){
HttpEntity entity = response.getEntity()
InputStream input = entity.getContent()
writeToSDCard(fileName,input)
input.close()
// entity.consumeContent()
return fileName
}else{
return null
}
} catch (Exception e) {
e.printStackTrace()
return null
}
}
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled()
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result)
closeProgressDialog()
if(result==null){
Toast t=Toast.makeText(mContext, "连接错误!请稍后再试!", Toast.LENGTH_LONG)
t.setGravity(Gravity.CENTER, 0, 0)
t.show()
return
}
Toast t=Toast.makeText(mContext, "已保存到SD卡。", Toast.LENGTH_LONG)
t.setGravity(Gravity.CENTER, 0, 0)
t.show()
File directory=Environment.getExternalStorageDirectory()
File file=new File(directory,result)
Log.i("tag", "Path="+file.getAbsolutePath())
Intent intent = getFileIntent(file)
startActivity(intent)
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute()
showProgressDialog()
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values)
}
}
恢复出厂设置。备份好重要文件后,还原所有设置和清除所有数据即可。
或者在关机状态下同时按住,锁屏键和音量加键,当出现vivo界面时松开锁屏键继续按住音量加键,选择进入recovery模式(上下音量键为选择键,电源键为确定键),选择清除数据,等界面跳转之后,再选择还原或清除所有设置后返回上层重启手机即可。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)