进度条不工作webview android

时间:2015-01-13 08:52:21

标签: android webview

我正在尝试在我的网页浏览器中实现进度条,但它没有显示任何进展我收到此警告“类型MainActivity.MyWebViewClient永远不会在本地使用”行

私有类MyWebViewClient扩展了WebChromeClient {

CODE

package com.abc.browser;

import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class MainActivity extends Activity {

    private WebView mWebView;
    private EditText urlEditText;
    private ProgressBar progress;
    // FULL-SCREEN VIDEO-1
    private LinearLayout mContentView;
    private FrameLayout mCustomViewContainer;
    private View mCustomView;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
    private WebChromeClient mWebChromeClient;

    // FULL-SCREEN VIDEO-1

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

        findViewById(R.id.menu2).setVisibility(View.GONE);
        findViewById(R.id.set_1_b).setVisibility(View.GONE);
        findViewById(R.id.set_2_b).setVisibility(View.GONE);
        findViewById(R.id.reload_btn).setVisibility(View.GONE);
        findViewById(R.id.menu3).setVisibility(View.GONE);
        findViewById(R.id.menu1).setVisibility(View.GONE);

        // FULL-SCREEN VIDEO-2
        mContentView = (LinearLayout) findViewById(R.id.linearlayout);
        mWebView = (WebView) findViewById(R.id.webView);
        mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);

        mWebChromeClient = new WebChromeClient() {

            @Override
            public void onShowCustomView(View view,
                    WebChromeClient.CustomViewCallback callback) {
                // if a view already exists then immediately terminate the new
                // one
                if (mCustomView != null) {
                    callback.onCustomViewHidden();
                    return;
                }

                // Add the custom view to its container.
                mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
                mCustomView = view;
                mCustomViewCallback = callback;

                // hide main browser view
                mContentView.setVisibility(View.GONE);

                // Finally show the custom view container.
                mCustomViewContainer.setVisibility(View.VISIBLE);
                mCustomViewContainer.bringToFront();
            }

            @Override
            public void onHideCustomView() {
                if (mCustomView == null)
                    return;

                // Hide the custom view.
                mCustomView.setVisibility(View.GONE);
                // Remove the custom view from its container.
                mCustomViewContainer.removeView(mCustomView);
                mCustomView = null;
                mCustomViewContainer.setVisibility(View.GONE);
                mCustomViewCallback.onCustomViewHidden();

                // Show the content view.
                mContentView.setVisibility(View.VISIBLE);
            }
        };
        // FULL-SCREEN VIDEO-2
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);

        mWebView.loadUrl("http://www.google.com");
        mWebView.setWebViewClient(new HelloWebViewClient());

        // Download manager
        mWebView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                    String contentDisposition, String mimetype,
                    long contentLength) {
                Request request = new Request(Uri.parse(url));
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(
                        Environment.DIRECTORY_DOWNLOADS, "download");
                DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                dm.enqueue(request);

            }
        });
        // **//

        // URL BAR AND PROGRESS BAR

        progress = (ProgressBar) findViewById(R.id.progressBar);
        progress.setMax(100);

        urlEditText = (EditText) findViewById(R.id.urlField);
        // Use Done/search button in keyboard as go button
        urlEditText.setOnEditorActionListener(new OnEditorActionListener() {
            public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                // Toast.makeText(getApplicationContext(), "some key pressed",
                // Toast.LENGTH_LONG).show();
                String url = urlEditText.getText().toString();
                if (url.endsWith(".ac") || url.endsWith(".ac.uk")
                        || url.endsWith(".ad") || url.endsWith(".ae")
                        || url.endsWith(".aero") || url.endsWith(".af")
                        || url.endsWith(".ag") || url.endsWith(".ai")
                        || url.endsWith(".al") || url.endsWith(".am")
                        || url.endsWith(".an") || url.endsWith(".ao")
                        || url.endsWith(".aq") || url.endsWith(".ar")
                        || url.endsWith(".arpa") || url.endsWith(".as")
                        || url.endsWith(".asia") || url.endsWith(".at")
                        || url.endsWith(".au") || url.endsWith(".aw")
                        || url.endsWith(".ax") || url.endsWith(".az")
                        || url.endsWith(".ba") || url.endsWith(".bb")
                        || url.endsWith(".bd") || url.endsWith(".be")
                        || url.endsWith(".bf") || url.endsWith(".bg")
                        || url.endsWith(".bh") || url.endsWith(".bi")
                        || url.endsWith(".biz") || url.endsWith(".bj")
                        || url.endsWith(".bm") || url.endsWith(".bn")
                        || url.endsWith(".bo") || url.endsWith(".br")
                        || url.endsWith(".bs") || url.endsWith(".bt")
                        || url.endsWith(".bv") || url.endsWith(".bw")
                        || url.endsWith(".by") || url.endsWith(".bz")
                        || url.endsWith(".ca") || url.endsWith(".cat")
                        || url.endsWith(".cc") || url.endsWith(".cd")
                        || url.endsWith(".cf") || url.endsWith(".cg")
                        || url.endsWith(".ch") || url.endsWith(".ci")
                        || url.endsWith(".ck") || url.endsWith(".cl")
                        || url.endsWith(".cm") || url.endsWith(".cn")
                        || url.endsWith(".co") || url.endsWith(".co.uk")
                        || url.endsWith(".com") || url.endsWith(".coop")
                        || url.endsWith(".cr") || url.endsWith(".cs")
                        || url.endsWith(".cu") || url.endsWith(".cv")
                        || url.endsWith(".cx") || url.endsWith(".cy")
                        || url.endsWith(".cz") || url.endsWith(".dd")
                        || url.endsWith(".de") || url.endsWith(".dj")
                        || url.endsWith(".dk") || url.endsWith(".dm")
                        || url.endsWith(".do") || url.endsWith(".dz")
                        || url.endsWith(".ec") || url.endsWith(".edu")
                        || url.endsWith(".ee") || url.endsWith(".eg")
                        || url.endsWith(".eh") || url.endsWith(".er")
                        || url.endsWith(".es") || url.endsWith(".et")
                        || url.endsWith(".eu") || url.endsWith(".fi")
                        || url.endsWith(".firm") || url.endsWith(".fj")
                        || url.endsWith(".fk") || url.endsWith(".fm")
                        || url.endsWith(".fo") || url.endsWith(".fr")
                        || url.endsWith(".fx.ga") || url.endsWith(".gb")
                        || url.endsWith(".gd") || url.endsWith(".ge")
                        || url.endsWith(".gf") || url.endsWith(".gh")
                        || url.endsWith(".gi") || url.endsWith(".gl")
                        || url.endsWith(".gm") || url.endsWith(".gn")
                        || url.endsWith(".gov") || url.endsWith(".gov.uk")
                        || url.endsWith(".gp") || url.endsWith(".gq")
                        || url.endsWith(".gr") || url.endsWith(".gs")
                        || url.endsWith(".gt") || url.endsWith(".gu")
                        || url.endsWith(".gw") || url.endsWith(".gy")
                        || url.endsWith(".hk") || url.endsWith(".hm")
                        || url.endsWith(".hn") || url.endsWith(".hr")
                        || url.endsWith(".ht") || url.endsWith(".hu")
                        || url.endsWith(".id") || url.endsWith(".ie")
                        || url.endsWith(".il") || url.endsWith(".im")
                        || url.endsWith(".in") || url.endsWith(".info")
                        || url.endsWith(".int") || url.endsWith(".io")
                        || url.endsWith(".iq") || url.endsWith(".ir.is")
                        || url.endsWith(".it") || url.endsWith(".je")
                        || url.endsWith(".jm") || url.endsWith(".jo")
                        || url.endsWith(".jobs") || url.endsWith(".jp")
                        || url.endsWith(".ke") || url.endsWith(".kg")
                        || url.endsWith(".kh") || url.endsWith(".ki")
                        || url.endsWith(".km") || url.endsWith(".kn")
                        || url.endsWith(".kp") || url.endsWith(".kr")
                        || url.endsWith(".kw") || url.endsWith(".ky")
                        || url.endsWith(".kz") || url.endsWith(".la")
                        || url.endsWith(".lb") || url.endsWith(".lc")
                        || url.endsWith(".li") || url.endsWith(".lk")
                        || url.endsWith(".lr") || url.endsWith(".ls")
                        || url.endsWith(".lt") || url.endsWith(".ltd.uk")
                        || url.endsWith(".lu") || url.endsWith(".lv")
                        || url.endsWith(".ly") || url.endsWith(".ma")
                        || url.endsWith(".mc") || url.endsWith(".md")
                        || url.endsWith(".me") || url.endsWith(".me.uk")
                        || url.endsWith(".mg") || url.endsWith(".mh")
                        || url.endsWith(".mil") || url.endsWith(".mk")
                        || url.endsWith(".ml") || url.endsWith(".mm")
                        || url.endsWith(".mn") || url.endsWith(".mo")
                        || url.endsWith(".mobi") || url.endsWith(".mod.uk")
                        || url.endsWith(".mp") || url.endsWith(".mq")
                        || url.endsWith(".mr") || url.endsWith(".ms")
                        || url.endsWith(".mt") || url.endsWith(".mu")
                        || url.endsWith(".museum") || url.endsWith(".mv")
                        || url.endsWith(".mw") || url.endsWith(".mx")
                        || url.endsWith(".my") || url.endsWith(".mz.na")
                        || url.endsWith(".name") || url.endsWith(".nato")
                        || url.endsWith(".nc") || url.endsWith(".ne")
                        || url.endsWith(".net") || url.endsWith(".net.uk")
                        || url.endsWith(".nf") || url.endsWith(".ng")
                        || url.endsWith(".nhs.uk") || url.endsWith(".ni")
                        || url.endsWith(".nl") || url.endsWith(".no")
                        || url.endsWith(".nom") || url.endsWith(".np")
                        || url.endsWith(".nr") || url.endsWith(".nt")
                        || url.endsWith(".nu") || url.endsWith(".nz")
                        || url.endsWith(".om") || url.endsWith(".org")
                        || url.endsWith(".org.uk") || url.endsWith(".pa")
                        || url.endsWith(".pe") || url.endsWith(".pf")
                        || url.endsWith(".pg") || url.endsWith(".ph")
                        || url.endsWith(".pk") || url.endsWith(".pl")
                        || url.endsWith(".plc.uk") || url.endsWith(".pm")
                        || url.endsWith(".pn") || url.endsWith(".post")
                        || url.endsWith(".pr") || url.endsWith(".pro")
                        || url.endsWith(".ps") || url.endsWith(".pt")
                        || url.endsWith(".pw") || url.endsWith(".py")
                        || url.endsWith(".qa") || url.endsWith(".re")
                        || url.endsWith(".ro") || url.endsWith(".ru")
                        || url.endsWith(".rw") || url.endsWith(".sa")
                        || url.endsWith(".sb") || url.endsWith(".sc")
                        || url.endsWith(".sch.uk") || url.endsWith(".sd")
                        || url.endsWith(".se") || url.endsWith(".sg")
                        || url.endsWith(".sh") || url.endsWith(".si")
                        || url.endsWith(".sj") || url.endsWith(".sk")
                        || url.endsWith(".sl") || url.endsWith(".sm")
                        || url.endsWith(".sn") || url.endsWith(".so")
                        || url.endsWith(".sr") || url.endsWith(".store")
                        || url.endsWith(".su") || url.endsWith(".sv")
                        || url.endsWith(".sy") || url.endsWith(".sz")
                        || url.endsWith(".tc") || url.endsWith(".td")
                        || url.endsWith(".tel") || url.endsWith(".tf")
                        || url.endsWith(".tg") || url.endsWith(".th")
                        || url.endsWith(".tj") || url.endsWith(".tk")
                        || url.endsWith(".tl") || url.endsWith(".tm")
                        || url.endsWith(".tn") || url.endsWith(".to")
                        || url.endsWith(".tp") || url.endsWith(".tr")
                        || url.endsWith(".travel") || url.endsWith(".tt")
                        || url.endsWith(".tv") || url.endsWith(".tw")
                        || url.endsWith(".tz") || url.endsWith(".ua")
                        || url.endsWith(".ug") || url.endsWith(".uk")
                        || url.endsWith(".um") || url.endsWith(".us")
                        || url.endsWith(".uy") || url.endsWith(".va")
                        || url.endsWith(".vc") || url.endsWith(".ve")
                        || url.endsWith(".vg") || url.endsWith(".vi")
                        || url.endsWith(".vn") || url.endsWith(".vu")
                        || url.endsWith(".web") || url.endsWith(".wf")
                        || url.endsWith(".ws") || url.endsWith(".xxx")
                        || url.endsWith(".ye") || url.endsWith(".yt")
                        || url.endsWith(".yu") || url.endsWith(".za")
                        || url.endsWith(".zm") || url.endsWith(".zr")
                        || url.endsWith(".zw")) {
                    if (!url.startsWith("http://")
                            && !url.startsWith("https://")) {
                        url = "http://" + url;

                    }
                } else
                    url = "https://www.google.com/search?q="
                            + url.replace(" ", "+");// Prefix (replace spaces
                                                    // with a '+' sign)
                mWebView.loadUrl(url);

                if (validateUrl(url)) {
                    mWebView.getSettings().setJavaScriptEnabled(true);
                    mWebView.loadUrl(url);

                    MainActivity.this.progress.setProgress(10);
                }
                return false;
            }

            private boolean validateUrl(String url) {
                return true;

            }
        });
        // **//
    }

    private class MyWebViewClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            MainActivity.this.setValue(newProgress);
            super.onProgressChanged(view, newProgress);
        }
    }

    public void setValue(int progress) {
        this.progress.setProgress(progress);
    }

    private class HelloWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView webview, String url) {
            webview.setWebChromeClient(mWebChromeClient);
            webview.loadUrl(url);

            return true;
        }
    }

    // FULL-SCREEN VIDEO-3
    @Override
    protected void onStop() {// plays video outside frame on back button and
                                // prevents from going back

        super.onStop();
        if (mCustomView != null) {
            if (mCustomViewCallback != null)
                mCustomViewCallback.onCustomViewHidden();
            mCustomView = null;
        }

    }

    @Override
    public void onBackPressed() {// hide custom view (in which the video plays)
                                    // on back button

        super.onBackPressed();
        if (mCustomView != null) {
            mWebChromeClient.onHideCustomView();
        } else {
            finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {// prevents the
                                                            // custom view from
                                                            // running in
                                                            // background when
                                                            // app is closed
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
        // FULL-SCREEN VIDEO-3
    }
}

0 个答案:

没有答案