直接在android WebView中下载pdf文件

时间:2018-02-10 13:29:27

标签: android pdf webview

我正在android中开发一个webview应用程序。在Chrome或其他浏览器中,当我点击任何链接到pdf文件的链接将在新标签页中打开或下载文件。但是在android webview中,当我点击该链接时,没有任何反应。 这是我的代码......

package aneridevelopers.com.govtofgaurang;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

private String WebAddress = "http://www.govtofgaurang.com/";
String url = WebAddress;
String loadUrl = WebAddress;
private WebView webView;
private ProgressBar progressBar;
private FrameLayout frameLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    progressBar.setMax(100);
    webView = (WebView) findViewById(R.id.webView);

    webView.setWebViewClient(new HelpClient());

    webView.setWebChromeClient(new WebChromeClient(){
        public void onProgressChanged(WebView view, int progress){
            frameLayout.setVisibility(View.VISIBLE);
            progressBar.setProgress(progress);

            setTitle("Loading...");

            if(progress == 100){
                frameLayout.setVisibility(View.GONE);
                setTitle(view.getTitle());
            }
            super.onProgressChanged(view, progress);
        }
    });

    webView.getSettings().setJavaScriptEnabled(true);
    webView.setVerticalScrollBarEnabled(false);
    webView.loadUrl(WebAddress);
    progressBar.setProgress(0);
}
private class HelpClient extends WebViewClient{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        frameLayout.setVisibility(View.VISIBLE);
        return true;
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK){
        if(webView.canGoBack()){
            webView.goBack();
            return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}
}

请帮助我......我是Android的新手。

0 个答案:

没有答案