如何在内部WebView中打开外部URL作为默认值?

时间:2016-04-17 15:05:43

标签: android webview android-webview default

所以我在Android Studio中有一个WebView应用程序,我希望当我打开一个Url(例如Google App)时,它会默认在我的应用程序中打开它。 这是我的代码的一部分:

String url = "http://google.com";

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

myBrowser = (WebView) findViewById(R.id.mybrowser);
    myBrowser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    myBrowser.getSettings().setJavaScriptEnabled(true);
    myBrowser.getSettings().setBuiltInZoomControls(true);
    myBrowser.setWebViewClient(new WebViewClient());
    myBrowser.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {

            prefs.edit().putString("LastUrl", myBrowser.getUrl().toString()).commit();
            editText.setText(myBrowser.getUrl().toString());
            if (progress < 100 && Pbar.getVisibility() == ProgressBar.GONE) {
                Pbar.setVisibility(ProgressBar.VISIBLE);
            }
            Pbar.setProgress(progress);
            if (progress == 100) {
                Pbar.setVisibility(ProgressBar.GONE);
            }
        }
            public boolean shouldOverrideUrlLoading(WebView view, String URL) {
            if (URL != null && URL.startsWith("http://")) {
                view.getContext().startActivity(
                        new Intent(Intent.ACTION_VIEW, Uri.parse(URL)));
                return true;
            } else {
                return false;
            }
        }
    });
    myBrowser.loadUrl(url);
}

1 个答案:

答案 0 :(得分:0)

将您的shouldOverrideUrlLoading()替换为:

        public boolean shouldOverrideUrlLoading(WebView view, String URL) {
          view.loadUrl(URL);
          return true;
        }