在默认浏览器中打开链接 - WebView Android Studio

时间:2017-06-11 01:07:34

标签: android webview

我需要你的帮助!

我在Android Studio中完成了webview,它工作得很好,但是,当我点击某个链接时,我需要它,它会在默认浏览器中打开。

我的MainActivity.java

package com.example.levelmax.powertuberprojetoapp;

    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.KeyEvent;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

public class MainActivity extends ActionBarActivity {
private WebView view; //membuat variabel view agar bisa akses method onKeyDown

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

    view = (WebView) this.findViewById(R.id.webView);
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new AppWebViewClient());
    view.loadUrl("http://google.com"); //try js alert
    view.setWebChromeClient(new WebChromeClient()); // adding js alert support
}


private class AppWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }


}



public boolean onKeyDown(int keyCode, KeyEvent event) {
    //ketika disentuh tombol back
    if ((keyCode == KeyEvent.KEYCODE_BACK) && view.canGoBack()) {
        view.goBack(); //method goback() dieksekusi untuk kembali pada halaman sebelumnya
        return true;
    }
    // Jika tidak ada history (Halaman yang sebelumnya dibuka)
    // maka akan keluar dari activity
    return super.onKeyDown(keyCode, event);
}
}

1 个答案:

答案 0 :(得分:1)

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
startActivity(browserIntent);