如何处理webview中的下载链接

时间:2014-01-23 21:16:14

标签: android webview

好的,对不起noob问题,但我已经搜索过,我似乎无法让这个工作。我想从webview调用我的网站,但我需要下载链接来实际下载文件。以下是我到目前为止的情况:

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.actionbarsherlock.app.SherlockFragment;
import com.mapleleaf.mp3download.R;

public class AppWallFragment2 extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle        

savedInstanceState) {
    View inflate = inflater.inflate(R.layout.app_wall2, container, false);
    WebView webView = (WebView)inflate.findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    final ProgressDialog mProgress = ProgressDialog.show(getActivity(),  

"Loading", "Please wait for a moment...");
    webView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
        public void onPageFinished(WebView view, String url) {
            if(mProgress.isShowing()) {
                mProgress.dismiss();
            }
        }
    });

    webView.loadUrl("http://anime.appcreator.org");
    return inflate;
}
}

2 个答案:

答案 0 :(得分:0)

您需要在WebViewClient中定义onDownloadStart函数

public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
    //Do something with this URL (like queue a download)
}

答案 1 :(得分:0)

请参阅以下代码段:

webView.setWebViewClient(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent, 
            String contentDisposition, String mimetype, 
            long contentLength) {
        //TODO: Handle your download task here.
    }
});

有关详细信息,请参阅here