Search in sources :

Example 11 with WebViewClient

use of android.webkit.WebViewClient in project remusic by aa112901.

the class TopFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_top, container, false);
    webView = (WebView) view.findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36");
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // 根据传入的参数再去加载新的网页
            view.loadUrl(url);
            // 表示当前WebView可以处理打开新网页的请求,不用借助系统浏览器
            return true;
        }
    });
    webView.loadUrl("http://music.163.com/#/discover/toplist?id=19723756");
    return view;
}
Also used : WebView(android.webkit.WebView) View(android.view.View) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 12 with WebViewClient

use of android.webkit.WebViewClient in project android-test-demo by abdyer.

the class WebViewActivity method loadUrl.

private void loadUrl() {
    String url = getIntent().getStringExtra(EXTRA_URL);
    webview.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });
    webview.loadUrl(url);
}
Also used : WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 13 with WebViewClient

use of android.webkit.WebViewClient in project materialistic by hidroh.

the class CacheableWebView method init.

private void init() {
    enableCache();
    setLoadSettings();
    setWebViewClient(new WebViewClient());
    setWebChromeClient(mArchiveClient);
}
Also used : WebViewClient(android.webkit.WebViewClient)

Example 14 with WebViewClient

use of android.webkit.WebViewClient in project Klyph by jonathangerbaud.

the class DonationsFragment method buildFlattrView.

/**
     * Build view for Flattr. see Flattr API for more information:
     * http://developers.flattr.net/button/
     */
@SuppressLint("SetJavaScriptEnabled")
@TargetApi(11)
private void buildFlattrView() {
    final FrameLayout mLoadingFrame;
    final WebView mFlattrWebview;
    mFlattrWebview = (WebView) getActivity().findViewById(R.id.donations__flattr_webview);
    mLoadingFrame = (FrameLayout) getActivity().findViewById(R.id.donations__loading_frame);
    // disable hardware acceleration for this webview to get transparent background working
    if (Build.VERSION.SDK_INT >= 11) {
        mFlattrWebview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    // define own webview client to override loading behaviour
    mFlattrWebview.setWebViewClient(new WebViewClient() {

        /**
             * Open all links in browser, not in webview
             */
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
            try {
                view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlNewString)));
            } catch (ActivityNotFoundException e) {
                openDialog(android.R.drawable.ic_dialog_alert, R.string.donations__alert_dialog_title, getString(R.string.donations__alert_dialog_no_browser));
            }
            return false;
        }

        /**
             * Links in the flattr iframe should load in the browser not in the iframe itself,
             * http:/
             * /stackoverflow.com/questions/5641626/how-to-get-webview-iframe-link-to-launch-the
             * -browser
             */
        @Override
        public void onLoadResource(WebView view, String url) {
            if (url.contains("flattr")) {
                HitTestResult result = view.getHitTestResult();
                if (result != null && result.getType() > 0) {
                    try {
                        view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    } catch (ActivityNotFoundException e) {
                        openDialog(android.R.drawable.ic_dialog_alert, R.string.donations__alert_dialog_title, getString(R.string.donations__alert_dialog_no_browser));
                    }
                    view.stopLoading();
                }
            }
        }

        /**
             * After loading is done, remove frame with progress circle
             */
        @Override
        public void onPageFinished(WebView view, String url) {
            // remove loading frame, show webview
            if (mLoadingFrame.getVisibility() == View.VISIBLE) {
                mLoadingFrame.setVisibility(View.GONE);
                mFlattrWebview.setVisibility(View.VISIBLE);
            }
        }
    });
    // get flattr values from xml config
    String projectUrl = mFlattrProjectUrl;
    String flattrUrl = this.mFlattrUrl;
    // make text white and background transparent
    String htmlStart = "<html> <head><style type='text/css'>*{color: #FFFFFF; background-color: transparent;}</style>";
    // https is not working in android 2.1 and 2.2
    String flattrScheme;
    if (Build.VERSION.SDK_INT >= 9) {
        flattrScheme = "https://";
    } else {
        flattrScheme = "http://";
    }
    // set url of flattr link
    mFlattrUrlTextView = (TextView) getActivity().findViewById(R.id.donations__flattr_url);
    mFlattrUrlTextView.setText(flattrScheme + flattrUrl);
    String flattrJavascript = "<script type='text/javascript'>" + "/* <![CDATA[ */" + "(function() {" + "var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];" + "s.type = 'text/javascript';" + "s.async = true;" + "s.src = '" + flattrScheme + "api.flattr.com/js/0.6/load.js?mode=auto';" + "t.parentNode.insertBefore(s, t);" + "})();" + "/* ]]> */" + "</script>";
    String htmlMiddle = "</head> <body> <div align='center'>";
    String flattrHtml = "<a class='FlattrButton' style='display:none;' href='" + projectUrl + "' target='_blank'></a> <noscript><a href='" + flattrScheme + flattrUrl + "' target='_blank'> <img src='" + flattrScheme + "api.flattr.com/button/flattr-badge-large.png' alt='Flattr this' title='Flattr this' border='0' /></a></noscript>";
    String htmlEnd = "</div> </body> </html>";
    String flattrCode = htmlStart + flattrJavascript + htmlMiddle + flattrHtml + htmlEnd;
    mFlattrWebview.getSettings().setJavaScriptEnabled(true);
    mFlattrWebview.loadData(flattrCode, "text/html", "utf-8");
    // disable scroll on touch
    mFlattrWebview.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            // already handled (returns true) when moving
            return (motionEvent.getAction() == MotionEvent.ACTION_MOVE);
        }
    });
    // make background of webview transparent
    // has to be called AFTER loadData
    // http://stackoverflow.com/questions/5003156/android-webview-style-background-colortransparent-ignored-on-android-2-2
    mFlattrWebview.setBackgroundColor(0x00000000);
}
Also used : ActivityNotFoundException(android.content.ActivityNotFoundException) FrameLayout(android.widget.FrameLayout) Intent(android.content.Intent) WebView(android.webkit.WebView) View(android.view.View) WebView(android.webkit.WebView) TextView(android.widget.TextView) WebViewClient(android.webkit.WebViewClient) HitTestResult(android.webkit.WebView.HitTestResult) MotionEvent(android.view.MotionEvent) SuppressLint(android.annotation.SuppressLint) TargetApi(android.annotation.TargetApi)

Example 15 with WebViewClient

use of android.webkit.WebViewClient in project superCleanMaster by joyoyao.

the class DeciveInfoFragment method initWebview.

private void initWebview() {
    // TODO Auto-generated method stub
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    // 设置可以访问文件
    webSettings.setAllowFileAccess(true);
    // 设置可以支持缩放
    webSettings.setSupportZoom(true);
    // 设置默认缩放方式尺寸是far
    webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
    // 设置出现缩放工具
    webSettings.setBuiltInZoomControls(false);
    webSettings.setDefaultFontSize(20);
    // 访问assets目录下的文件
    mWebView.loadUrl("http://girl-atlas.com");
    // 设置WebViewClient
    mWebView.setWebViewClient(new WebViewClient() {

        // url拦截
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // 使用自己的WebView组件来响应Url加载事件,而不是使用默认浏览器器加载页面
            view.loadUrl(url);
            // 相应完成返回true
            return true;
        // return super.shouldOverrideUrlLoading(view, url);
        }

        // 页面开始加载
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            mProgressBar.setVisibility(View.VISIBLE);
            super.onPageStarted(view, url, favicon);
        }

        // 页面加载完成
        @Override
        public void onPageFinished(WebView view, String url) {
            mProgressBar.setVisibility(View.GONE);
            super.onPageFinished(view, url);
        }

        // WebView加载的所有资源url
        @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
    });
    // 设置WebChromeClient
    mWebView.setWebChromeClient(new WebChromeClient() {

        @Override
        public // 处理javascript中的alert
        boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
            return super.onJsAlert(view, url, message, result);
        }

        ;

        @Override
        public // 处理javascript中的confirm
        boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
            return super.onJsConfirm(view, url, message, result);
        }

        ;

        @Override
        public // 处理javascript中的prompt
        boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
            return super.onJsPrompt(view, url, message, defaultValue, result);
        }

        ;

        // 设置网页加载的进度条
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            mProgressBar.setProgress(newProgress);
            super.onProgressChanged(view, newProgress);
        }

        // 设置程序的Title
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
        }
    });
    mWebView.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
                    // 表示按返回键
                    // 后退
                    mWebView.goBack();
                    // 已处理
                    return true;
                }
            }
            return false;
        }
    });
}
Also used : InjectView(butterknife.InjectView) View(android.view.View) WebView(android.webkit.WebView) JsResult(android.webkit.JsResult) KeyEvent(android.view.KeyEvent) Bitmap(android.graphics.Bitmap) WebSettings(android.webkit.WebSettings) WebChromeClient(android.webkit.WebChromeClient) WebView(android.webkit.WebView) JsPromptResult(android.webkit.JsPromptResult) WebViewClient(android.webkit.WebViewClient)

Aggregations

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