Search in sources :

Example 36 with WebViewClient

use of android.webkit.WebViewClient in project ETSMobile-Android2 by ApplETS.

the class BiblioFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup v = (ViewGroup) inflater.inflate(R.layout.fragment_web_view, container, false);
    super.onCreateView(inflater, v, savedInstanceState);
    webView = (WebView) v.findViewById(R.id.webView);
    final WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    loadingView.showLoadingView();
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
            view.loadUrl(url);
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // super.onPageFinished(view, url);
            LoadingView.hideLoadingView(loadingView);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            view.loadUrl("file:///android_asset/webview_error_page.html");
        }
    });
    webView.loadUrl(getActivity().getString(R.string.url_biblio));
    AnalyticsHelper.getInstance(getActivity()).sendScreenEvent(getClass().getSimpleName());
    return v;
}
Also used : ViewGroup(android.view.ViewGroup) WebSettings(android.webkit.WebSettings) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 37 with WebViewClient

use of android.webkit.WebViewClient in project DroidPlugin by DroidPluginTeam.

the class WebViewTestActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view_test);
    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;
        }
    });
    mWebView.loadUrl("http://www.baidu.com");
}
Also used : WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 38 with WebViewClient

use of android.webkit.WebViewClient in project Bolts-Android by BoltsFramework.

the class WebViewAppLinkResolver method getAppLinkFromUrlInBackground.

@Override
public Task<AppLink> getAppLinkFromUrlInBackground(final Uri url) {
    final Capture<String> content = new Capture<String>();
    final Capture<String> contentType = new Capture<String>();
    return Task.callInBackground(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            URL currentURL = new URL(url.toString());
            URLConnection connection = null;
            while (currentURL != null) {
                // Fetch the content at the given URL.
                connection = currentURL.openConnection();
                if (connection instanceof HttpURLConnection) {
                    // Unfortunately, this doesn't actually follow redirects if they go from http->https,
                    // so we have to do that manually.
                    ((HttpURLConnection) connection).setInstanceFollowRedirects(true);
                }
                connection.setRequestProperty(PREFER_HEADER, META_TAG_PREFIX);
                connection.connect();
                if (connection instanceof HttpURLConnection) {
                    HttpURLConnection httpConnection = (HttpURLConnection) connection;
                    if (httpConnection.getResponseCode() >= 300 && httpConnection.getResponseCode() < 400) {
                        currentURL = new URL(httpConnection.getHeaderField("Location"));
                        httpConnection.disconnect();
                    } else {
                        currentURL = null;
                    }
                } else {
                    currentURL = null;
                }
            }
            try {
                content.set(readFromConnection(connection));
                contentType.set(connection.getContentType());
            } finally {
                if (connection instanceof HttpURLConnection) {
                    ((HttpURLConnection) connection).disconnect();
                }
            }
            return null;
        }
    }).onSuccessTask(new Continuation<Void, Task<JSONArray>>() {

        @Override
        public Task<JSONArray> then(Task<Void> task) throws Exception {
            // Load the content in a WebView and use JavaScript to extract the meta tags.
            final TaskCompletionSource<JSONArray> tcs = new TaskCompletionSource<>();
            final WebView webView = new WebView(context);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setNetworkAvailable(false);
            webView.setWebViewClient(new WebViewClient() {

                private boolean loaded = false;

                private void runJavaScript(WebView view) {
                    if (!loaded) {
                        // After the first resource has been loaded (which will be the pre-populated data)
                        // run the JavaScript meta tag extraction script
                        loaded = true;
                        view.loadUrl(TAG_EXTRACTION_JAVASCRIPT);
                    }
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    runJavaScript(view);
                }

                @Override
                public void onLoadResource(WebView view, String url) {
                    super.onLoadResource(view, url);
                    runJavaScript(view);
                }
            });
            // Inject an object that will receive the JSON for the extracted JavaScript tags
            webView.addJavascriptInterface(new Object() {

                @JavascriptInterface
                public void setValue(String value) {
                    try {
                        tcs.trySetResult(new JSONArray(value));
                    } catch (JSONException e) {
                        tcs.trySetError(e);
                    }
                }
            }, "boltsWebViewAppLinkResolverResult");
            String inferredContentType = null;
            if (contentType.get() != null) {
                inferredContentType = contentType.get().split(";")[0];
            }
            webView.loadDataWithBaseURL(url.toString(), content.get(), inferredContentType, null, null);
            return tcs.getTask();
        }
    }, Task.UI_THREAD_EXECUTOR).onSuccess(new Continuation<JSONArray, AppLink>() {

        @Override
        public AppLink then(Task<JSONArray> task) throws Exception {
            Map<String, Object> alData = parseAlData(task.getResult());
            AppLink appLink = makeAppLinkFromAlData(alData, url);
            return appLink;
        }
    });
}
Also used : JavascriptInterface(android.webkit.JavascriptInterface) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONException(org.json.JSONException) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 39 with WebViewClient

use of android.webkit.WebViewClient in project bilibili-android-client by HotBitmapGG.

the class BrowserActivity method setupWebView.

@SuppressLint("SetJavaScriptEnabled")
private void setupWebView() {
    progressBar.spin();
    final WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    webSettings.setDomStorageEnabled(true);
    webSettings.setGeolocationEnabled(true);
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);
    mWebView.getSettings().setBlockNetworkImage(true);
    mWebView.setWebViewClient(webViewClient);
    mWebView.requestFocus(View.FOCUS_DOWN);
    mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
    mWebView.setWebChromeClient(new WebChromeClient() {

        @Override
        public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
            AlertDialog.Builder b2 = new AlertDialog.Builder(BrowserActivity.this).setTitle(R.string.app_name).setMessage(message).setPositiveButton("确定", (dialog, which) -> result.confirm());
            b2.setCancelable(false);
            b2.create();
            b2.show();
            return true;
        }
    });
    mWebView.loadUrl(url);
}
Also used : JsResult(android.webkit.JsResult) ConstantUtil(com.hotbitmapgg.bilibili.utils.ConstantUtil) Bundle(android.os.Bundle) JsResult(android.webkit.JsResult) Uri(android.net.Uri) Intent(android.content.Intent) RxBaseActivity(com.hotbitmapgg.bilibili.base.RxBaseActivity) MenuItem(android.view.MenuItem) BindView(butterknife.BindView) SuppressLint(android.annotation.SuppressLint) R(com.hotbitmapgg.ohmybilibili.R) WebSettings(android.webkit.WebSettings) WebResourceRequest(android.webkit.WebResourceRequest) Handler(android.os.Handler) Menu(android.view.Menu) WebViewClient(android.webkit.WebViewClient) View(android.view.View) WebView(android.webkit.WebView) ActionBar(android.support.v7.app.ActionBar) WebChromeClient(android.webkit.WebChromeClient) TextUtils(android.text.TextUtils) AlertDialog(android.app.AlertDialog) WebResourceError(android.webkit.WebResourceError) CircleProgressView(com.hotbitmapgg.bilibili.widget.CircleProgressView) Toolbar(android.support.v7.widget.Toolbar) Bitmap(android.graphics.Bitmap) ToastUtil(com.hotbitmapgg.bilibili.utils.ToastUtil) ClipboardUtil(com.hotbitmapgg.bilibili.utils.ClipboardUtil) Activity(android.app.Activity) WebSettings(android.webkit.WebSettings) WebChromeClient(android.webkit.WebChromeClient) WebView(android.webkit.WebView) SuppressLint(android.annotation.SuppressLint)

Example 40 with WebViewClient

use of android.webkit.WebViewClient in project UltimateAndroid by cymcsg.

the class WebViewActivity method initWebView.

private void initWebView() {
    mWebView.setWebViewClient(new WebViewClient());
    mWebView.setWebChromeClient(new WebChromeClient());
    WebSettings settings = mWebView.getSettings();
    settings.setSavePassword(true);
    settings.setSaveFormData(true);
    settings.setJavaScriptEnabled(true);
    settings.setSupportZoom(false);
    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    settings.setDomStorageEnabled(true);
    settings.setSupportMultipleWindows(false);
    mWebView.loadUrl("http://developer.android.com");
}
Also used : WebSettings(android.webkit.WebSettings) WebChromeClient(android.webkit.WebChromeClient) WebViewClient(android.webkit.WebViewClient)

Aggregations

WebViewClient (android.webkit.WebViewClient)119 WebView (android.webkit.WebView)109 WebChromeClient (android.webkit.WebChromeClient)32 View (android.view.View)30 WebSettings (android.webkit.WebSettings)28 Intent (android.content.Intent)26 SuppressLint (android.annotation.SuppressLint)23 Bitmap (android.graphics.Bitmap)19 WebResourceRequest (android.webkit.WebResourceRequest)11 TextView (android.widget.TextView)11 Bundle (android.os.Bundle)9 LinearLayout (android.widget.LinearLayout)9 Uri (android.net.Uri)8 WebResourceError (android.webkit.WebResourceError)8 KeyEvent (android.view.KeyEvent)7 JsResult (android.webkit.JsResult)6 WebResourceResponse (android.webkit.WebResourceResponse)6 DialogInterface (android.content.DialogInterface)4 RelativeLayout (android.widget.RelativeLayout)4 Activity (android.app.Activity)3