如何使用webview显示进度条

时间:2013-12-01 23:04:42

标签: android android-activity webview loading progress

这是我的最后一段代码。我想显示用户网络信息。当用户离线时警告用户 我想在线下或在线向用户展示。我用进度条。当我使用此代码时,我的应用程序崩溃并退出我的应用程序。我该怎么办

   ConnectivityManager conMgr;
   NetworkInfo netInfo;
    private WebView webView;
    ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_activity1);

           Toast.makeText(Activity5.this, "welcome",30000).show();

            progressDialog = new ProgressDialog(Activity5.this);
            Button btn1 = (Button) findViewById(R.id.buttn1);
            btn1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    finish();
                    System.exit(0);
                }
            });
            Button b = (Button) findViewById(R.id.button10);
            b.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    startActivity(new Intent(Activity5.this, Activity2.class));
                }
            });


            String url = "http://student.iaun.ac.ir";
            webView = (WebView) findViewById(R.id.webView2);

            webView.setWebViewClient(new myWebViewClient());
            webView.getSettings().setJavaScriptEnabled(false);
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.getSettings().setUseWideViewPort(true);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.loadUrl(url);
            webView.getSettings().setSavePassword(false);
            Button button = (Button) findViewById(R.id.new_button);
            button.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                        startActivity(new Intent(Activity5.this, Prefs.class));
                }
        });


            progressDialog.setMessage("please wait web page is loading");
            progressDialog.setCancelable(false);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.show();

            new Handler().postDelayed(new Runnable() {
                public void run() {

                    progressDialog.dismiss();

                }
            }, 25000);

            progressDialog = new ProgressDialog(Activity5.this);
                if (isOnline()) {
                       /*Your Code*/
                  }
                else{
                   try {
                AlertDialog alertDialog = new AlertDialog.Builder(
                        Activity5.this).create();
                alertDialog.setTitle("Info");
                alertDialog.setMessage("Please check your internet connection");
                alertDialog.setButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                finish();
                            }
                        });
                alertDialog.show();
            } catch (Exception e) {
            }
        }


            new Thread(new Runnable() {
                @Override
                public void run() {
                                  try {
                                        startDelay();
                                        webView.getSettings().setJavaScriptEnabled(true);     
                                  } catch (InterruptedException e) {
                                     e.printStackTrace();
                                    }

                        }
            }).start();    

    }



    void startDelay() throws InterruptedException {
        Thread.sleep(15000);
    }
       private boolean isOnline() {
            conMgr = (ConnectivityManager) getApplicationContext()
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            netInfo = conMgr.getActiveNetworkInfo();

            if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
                return false;
            }
            return true;
        }

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity1, menu);
        return true;


    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }

3 个答案:

答案 0 :(得分:3)

要点是:

  1. 您无法在按钮单击时使用System.exit(0),您可以使用finish()完成该活动或moveTaskToBack(true)以最小化应用程序。

  2. I would like to refer u this link :

  3. 首先要了解每件事情,然后继续前进,而不仅仅是复制&粘贴你的代码,理解它背后的概念和逻辑。

  4. 如果您需要有关此主题的更多帮助,那么我就在这里,但请花些时间尝试理解逻辑

答案 1 :(得分:1)

我已经更改了代码,现在进度对话框出现了20秒,同时webview也加载了它的内容......

public class Activity1 extends Activity {

    private webview webview;
    ProgressDialog progressDialog;

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

        Toast.makeText(getApplicationContext(), "welcome", Toast.LENGTH_LONG)
                .show();
        progressDialog = new ProgressDialog(Activity1.this);
        String url = "http://student.iaun.ac.ir";
        webview = (webview) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(false);
        webview.getSettings().setLoadWithOverviewMode(true);
        webview.getSettings().setUseWideViewPort(true);
        webview.getSettings().setBuiltInZoomControls(true);
        webview.loadUrl(url);

        progressDialog.setMessage("Loading ...");
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.show();

        new Handler().postDelayed(new Runnable() {
            public void run() {

                progressDialog.dismiss();

            }
        }, 20000);



    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up
        // to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }
}

答案 2 :(得分:0)

你必须使用WebViewClient,并覆盖onPageStarted(),onpageFinished()等方法......

public class WebActivity extends Activity {
        private WebView browser;
        ProgressDialog progressDialog;
        ConnectivityManager conMgr;
        NetworkInfo netInfo;
        String url;

        @SuppressWarnings("deprecation")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_web_activity);
            browser = (WebView) findViewById(R.id.shop);
            progressDialog = new ProgressDialog(WebActivity.this);
            if (isOnline()) {
                browser.getSettings().setDomStorageEnabled(true);
                browser.getSettings().setJavaScriptEnabled(true);

                browser.loadUrl(/*Your url paste here*/);
                browser.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        return false;
                    }

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

                    @Override
                    public void onPageStarted(WebView view, String url,
                            Bitmap favicon) {
                        // TODO Auto-generated method stub
                        super.onPageStarted(view, url, favicon);
                        progressDialog.setMessage("Loading ...");
                        progressDialog.setCancelable(false);
                        progressDialog.setCanceledOnTouchOutside(false);
                        progressDialog.show();
                    }
                });
            } else {
                try {
                    AlertDialog alertDialog = new AlertDialog.Builder(
                            WebActivity.this).create();
                    alertDialog.setTitle("Info");
                    alertDialog.setMessage("Please check your internet connection");
                    alertDialog.setButton("OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    finish();
                                }
                            });
                    alertDialog.show();
                } catch (Exception e) {
                }
            }
        }

        private boolean isOnline() {
            conMgr = (ConnectivityManager) getApplicationContext()
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            netInfo = conMgr.getActiveNetworkInfo();

            if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
                return false;
            }
            return true;
        }

        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && browser.canGoBack()) {
                browser.goBack();
                return true;
            } else {
                moveTaskToBack(true);
                return super.onKeyDown(keyCode, event);
            }
        }
    }

这是检查用户网络状态的代码......

ConnectivityManager conMgr;
NetworkInfo netInfo;
ProgressDialog progressDialog;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_activity);
        browser = (WebView) findViewById(R.id.shop);

        progressDialog = new ProgressDialog(WebActivity.this);
            if (isOnline()) {
                   /*Your Code*/
              }
            else{
               try {
            AlertDialog alertDialog = new AlertDialog.Builder(
                    WebActivity.this).create();
            alertDialog.setTitle("Info");
            alertDialog.setMessage("Please check your internet connection");
            alertDialog.setButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            finish();
                        }
                    });
            alertDialog.show();
        } catch (Exception e) {
        }
    }

这是检查状态的isOnline()方法......

private boolean isOnline() {
        conMgr = (ConnectivityManager) getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        netInfo = conMgr.getActiveNetworkInfo();

        if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
            return false;
        }
        return true;
    }