Search in sources :

Example 1 with JsInject

use of cn.nicolite.palm300heroes.injection.JsInject in project Palm300Heroes by nicolite.

the class WebViewActivity method initWebView.

private void initWebView() {
    settings = webView.getSettings();
    settings.setLoadWithOverviewMode(true);
    // 使页面支持缩放
    settings.setBuiltInZoomControls(true);
    // 支持缩放
    settings.setSupportZoom(true);
    // 缩放按钮
    settings.setDisplayZoomControls(false);
    // 支持自动加载图片
    settings.setLoadsImagesAutomatically(true);
    // 开启DOM
    settings.setDomStorageEnabled(true);
    // 关闭webview中缓存
    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    settings.setAppCacheEnabled(true);
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    settings.setDefaultTextEncodingName("UTF-8");
    // 将图片调整到适合webview的大小
    settings.setUseWideViewPort(true);
    // //当webview调用requestFocus时为webview设置节点
    settings.setNeedInitialFocus(true);
    // 允许访问文件
    settings.setAllowFileAccess(true);
    // 支持缩放
    settings.setSupportZoom(true);
    settings.setPluginState(WebSettings.PluginState.ON);
    webView.setFocusable(true);
    webView.setFocusableInTouchMode(true);
    webView.addJavascriptInterface(new JsInject(this), "imageListener");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String s) {
            if (s.startsWith("http") || s.startsWith("https")) {
                webView.loadUrl(s);
            } else if (s.startsWith("intent")) {
                try {
                    Intent intent = Intent.parseUri(s, Intent.URI_INTENT_SCHEME);
                    intent.addCategory("android.intent.category.BROWSABLE");
                    intent.setComponent(null);
                    intent.setSelector(null);
                    List<ResolveInfo> resolves = context.getPackageManager().queryIntentActivities(intent, 0);
                    if (resolves.size() > 0) {
                        startActivityIfNeeded(intent, -1);
                    }
                    return true;
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(s));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            }
            return super.shouldOverrideUrlLoading(webView, s);
        }

        @Override
        public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
            super.onPageStarted(webView, s, bitmap);
            progressBar.setProgress(20);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (view != null && toolbarTitle != null) {
                String title = view.getTitle();
                if (!TextUtils.isEmpty(title) && !title.contains("about:blank")) {
                    toolbarTitle.setText(title);
                }
            }
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
        }
    });
    webView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView webView, int i) {
            super.onProgressChanged(webView, i);
            if (progressBar != null) {
                if (i == 100) {
                    progressBar.setVisibility(View.GONE);
                    if (type != TYPE_VIDEO && type != TYPE_TY_CARTOON && type != TYPE_THEME_MUSIC) {
                        addImgClickListener();
                    }
                    if (type == TYPE_THEME_MUSIC) {
                        changThemeMusicDom();
                    }
                } else {
                    if (View.GONE == progressBar.getVisibility()) {
                        progressBar.setVisibility(View.VISIBLE);
                    }
                    if (i <= 20) {
                        progressBar.setProgress(20);
                    } else {
                        progressBar.setProgress(i);
                    }
                }
            }
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            if (!TextUtils.isEmpty(title) && !title.contains("about:blank")) {
                toolbarTitle.setText(title);
            }
        }
    });
}
Also used : WebResourceRequest(android.webkit.WebResourceRequest) Intent(android.content.Intent) URISyntaxException(java.net.URISyntaxException) URISyntaxException(java.net.URISyntaxException) JsInject(cn.nicolite.palm300heroes.injection.JsInject) ResolveInfo(android.content.pm.ResolveInfo) Bitmap(android.graphics.Bitmap) WebChromeClient(android.webkit.WebChromeClient) WebResourceError(android.webkit.WebResourceError) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Aggregations

Intent (android.content.Intent)1 ResolveInfo (android.content.pm.ResolveInfo)1 Bitmap (android.graphics.Bitmap)1 WebChromeClient (android.webkit.WebChromeClient)1 WebResourceError (android.webkit.WebResourceError)1 WebResourceRequest (android.webkit.WebResourceRequest)1 WebView (android.webkit.WebView)1 WebViewClient (android.webkit.WebViewClient)1 JsInject (cn.nicolite.palm300heroes.injection.JsInject)1 URISyntaxException (java.net.URISyntaxException)1