由于调用系统默认浏览器下载更新,造成用户体验非常不好,所以决定在webview中直接下载系统更新。然后直接安装。
由于要下载,所以必须用webview,联网权限这里不说了,直接写在manifafest中。
我们经常使用的下载都是调用Android默认浏览器 这样写
1、设置WebView的DownloadListener:
webView.setDownloadListener(new MyWebViewDownLoadListener());
2、实现MyWebViewDownLoadListener这个类,详细能够例如以下这样:
-
private class MyWebViewDownLoadListener implements DownloadListener {
-
-
@Override
-
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,
-
long contentLength) {
-
Uri uri = Uri.parse(url);
-
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
-
startActivity(intent);
-
}
-
-
}
为了直接下载,
Sample:
java代码:
public class zwebxiazai extends Activity {
WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.webView=(WebView) this.findViewById(R.id.webview);
this.webView.getSettings().setSupportZoom(false);
this.webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
this.webView.loadUrl("http://a.zntx.cc/");
this.webView.setWebViewClient(new WebViewClientDemo());
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
//实现下载的代码
Uri uri =
Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
});
}
private class WebViewClientDemo extends WebViewClient {
@Override
// 在WebView中而不是默认浏览器中显示页面
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
main.xml代码:
<?
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<WebView android:id="@+id/webview" android:layout_width="fill_parent"
android:layout_height="0dip" android:layout_weight="1" />
</LinearLayout>
在AndroidManifest.xml中增加訪问internet的权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Sample2 这个比較具体
WebView控制调用对应的WEB页面进行展示。当碰到页面有下载链接的时候,点击上去是一点反应都没有的。
原来是由于WebView默认没有开启文件下载的功能。假设要实现文件下载的功能。须要设置WebView的DownloadListener,通过实现自己的DownloadListener来实现文件的下载。详细操作例如以下:
1、设置WebView的DownloadListener:
webView.setDownloadListener(new MyWebViewDownLoadListener());
2、实现MyWebViewDownLoadListener这个类,详细能够例如以下这样:
-
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);
-
}
-
}
-
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进行一系列设置。
-
WebView webview=(WebView)layout.findViewById(R.id.webview);
-
webview.getSettings().setJavaScriptEnabled(true);
-
webview.setWebChromeClient(new MyWebChromeClient());
-
webview.requestFocus();
-
-
webview.loadUrl(jcrs_sub.get(position).addr);
-
-
webview.setWebViewClient(new MyWebViewClient());
-
webview.setDownloadListener(new MyWebViewDownLoadListener());
-
-
-
public class MyWebViewClient extends 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();
-
}
-
}
-
-
-
-
public boolean onKeyDown(int keyCode, KeyEvent event) {
-
-
-
-
-
return false;
-
}
-
WebView webview=(WebView)layout.findViewById(R.id.webview);
-
webview.getSettings().setJavaScriptEnabled(true);
-
webview.setWebChromeClient(new MyWebChromeClient());
-
webview.requestFocus();
-
-
webview.loadUrl(jcrs_sub.get(position).addr);
-
-
webview.setWebViewClient(new MyWebViewClient());
-
webview.setDownloadListener(new MyWebViewDownLoadListener());
-
-
-
public class MyWebViewClient extends 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();
-
}
-
}
-
-
-
-
public boolean onKeyDown(int keyCode, KeyEvent event) {
-
-
-
-
-
return false;
-
}
第二步。起线程開始下载文件。
-
-
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) {
-
-
String url=params[0];
-
-
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();
-
-
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();
-
-
return fileName;
-
}else{
-
return null;
-
}
-
} catch (Exception e) {
-
e.printStackTrace();
-
return null;
-
}
-
}
-
-
@Override
-
protected void onCancelled() {
-
-
super.onCancelled();
-
}
-
-
@Override
-
protected void onPostExecute(String result) {
-
-
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() {
-
-
super.onPreExecute();
-
showProgressDialog();
-
}
-
-
@Override
-
protected void onProgressUpdate(Void... values) {
-
-
super.onProgressUpdate(values);
-
}
-
-
-
}
-
-
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) {
-
-
String url=params[0];
-
-
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();
-
-
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();
-
-
return fileName;
-
}else{
-
return null;
-
}
-
} catch (Exception e) {
-
e.printStackTrace();
-
return null;
-
}
-
}
-
-
@Override
-
protected void onCancelled() {
-
-
super.onCancelled();
-
}
-
-
@Override
-
protected void onPostExecute(String result) {
-
-
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() {
-
-
super.onPreExecute();
-
showProgressDialog();
-
}
-
-
@Override
-
protected void onProgressUpdate(Void... values) {
-
-
super.onProgressUpdate(values);
-
}
-
-
-
}
第三步,实现一些工具方法。