如何在WebView中使用外部浏览器打开外部URL?

时间:2017-05-22 10:02:31

标签: android webview

我知道这是一个常见的问题,我发现很多这样的问题,但我没能在我的应用程序上执行。 请问我该怎么办? 以下是主要活动

公共类MainActivity扩展了AppCompatActivity {

WebView wv;
String mypage = "file:///android_asset/mypage/index.html";
String mypage_error = "file:///android_asset/mypage_error/index.html";
ProgressBar progressBar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    wv = (WebView) findViewById(R.id.wv);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setFocusable(true);
    wv.setFocusableInTouchMode(true);
    wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    wv.getSettings().setDomStorageEnabled(true);
    wv.getSettings().setDatabaseEnabled(true);
    wv.getSettings().setAppCacheEnabled(true);
    wv.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
    wv.loadUrl(mypage);
    wv.getSettings().setSupportZoom(true);
    wv.getSettings().setBuiltInZoomControls(true);
    progressBar=(ProgressBar) findViewById(R.id.progressBar);
    wv.setWebChromeClient(new WebChromeClient(){
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            progressBar.setProgress(newProgress);

            if (newProgress == 100){
                progressBar.setVisibility(View.GONE);

            }else {
                progressBar.setVisibility(View.VISIBLE);
            }
        }
    });
    wv.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long contentLength) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
        }
    });


    wv.setWebViewClient(new WebViewClient(){
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            wv.loadUrl(mypage_error);
        }
    });


}

@Override
public void onBackPressed()
{
    if(wv.canGoBack()){
        wv.goBack();
    }else{
        new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Exit!")
                .setMessage("Are you sure you want to close?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }

                })
                .setNegativeButton("No", null)
                .show();
    }
}

}

0 个答案:

没有答案