Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用webview显示进度条_Android_Android Activity_Webview_Loading_Progress - Fatal编程技术网

Android 如何使用webview显示进度条

Android 如何使用webview显示进度条,android,android-activity,webview,loading,progress,Android,Android Activity,Webview,Loading,Progress,这是我最后的代码和密码。我想显示用户网络信息。当用户脱机时,向用户发出警报。 我想在离线或在线状态下向用户显示。我使用进度条。当我使用此代码时,我的应用程序正在崩溃并退出应用程序。我该怎么办 ConnectivityManager conMgr; NetworkInfo netInfo; private WebView webView; ProgressDialog progressDialog; @Override protected void on

这是我最后的代码和密码。我想显示用户网络信息。当用户脱机时,向用户发出警报。
我想在离线或在线状态下向用户显示。我使用进度条。当我使用此代码时,我的应用程序正在崩溃并退出应用程序。我该怎么办

   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);
    }

您必须使用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) {
        }
    }
private boolean isOnline() {
        conMgr = (ConnectivityManager) getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        netInfo = conMgr.getActiveNetworkInfo();

        if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
            return false;
        }
        return true;
    }
这是检查状态的isOnline()方法

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) {
        }
    }
private boolean isOnline() {
        conMgr = (ConnectivityManager) getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        netInfo = conMgr.getActiveNetworkInfo();

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

您必须使用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) {
        }
    }
private boolean isOnline() {
        conMgr = (ConnectivityManager) getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        netInfo = conMgr.getActiveNetworkInfo();

        if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
            return false;
        }
        return true;
    }
这是检查状态的isOnline()方法

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) {
        }
    }
private boolean isOnline() {
        conMgr = (ConnectivityManager) getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        netInfo = conMgr.getActiveNetworkInfo();

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

我已经改变了代码,现在一个进度对话框出现了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);
    }
}

我已经改变了代码,现在一个进度对话框出现了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);
    }
}
要点是:

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

  • 首先理解每一件事,然后继续前进,而不仅仅是复制和粘贴电子代码,理解其背后的概念和逻辑

  • 如果你在这个话题上需要更多的帮助,那么我就在这里,但是请慢慢来,试着理解其中的逻辑

  • 要点是:

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

  • 首先理解每一件事,然后继续前进,而不仅仅是复制和粘贴电子代码,理解其背后的概念和逻辑

  • 如果你在这个话题上需要更多的帮助,那么我就在这里,但是请慢慢来,试着理解其中的逻辑



  • 安卓:我使用这个代码,但它不起作用,程序也不正常crash@user3051593:我使用此代码,但它不起作用,并且程序很崩溃。您只需要一个警告对话框,直到您的网页完成加载,所以您只需使用WebViewClient,它就能解决您的问题。您能告诉我错误的具体原因吗(从您的日志中).eclipse dos没有错误。当我运行程序时,它崩溃了,不幸地说,web已停止并退出程序。你能键入写代码吗?我的web视图cod是你能在记事本中看到我的代码的txt吗?你只需删除倒计时,你的web视图实例在哪里,你提供的链接无效…安卓:我使用这段代码,但它不起作用,程序是无效的crash@user3051593:我使用此代码,但它不起作用,并且程序很崩溃。您只需要一个警告对话框,直到您的网页完成加载,所以您只需使用WebViewClient,它就可以解决您的问题。您能告诉我错误的确切原因吗(来自您的日志).eclipse dos没有错误。当我运行程序时,它崩溃了,不幸地说,web已停止并退出程序。你能键入写代码吗?我的web视图cod是你能在记事本中看到我代码的txt吗?你只需删除倒计时,你的web视图实例在哪里,你提供的链接无效…我写了这段代码,但我想使用它我的程序中的thread.sleep我应该做什么处理程序也是android中的一个线程,postDelayed()方法的第二个参数是它的睡眠时间,从这段代码中可以看到行progressDialog.discouse();20秒后执行…否我想在程序中使用thread.sleep设置JavaScriptEnabled(true),因为我设置JavaScriptEnabled(false)首先和15秒后setJavaScriptEnabled(true)。我应该做什么?您只需添加语句以在run()方法中启用java脚本,如果您需要一个单独的线程,那么您可以创建它并为该线程使用睡眠。我想添加代码,向用户显示internet状况。您使用alertDialog.setMessage(“请检查您的internet连接”);在您的第一个cod中。我希望当用户脱机时,它会显示警报或进度bari编写此代码,但我希望在我的程序中使用thread.sleep我应该做什么处理程序也是android中的一个线程和postDelayed()的第二个参数方法是它的睡眠时间,从这段代码中,行progressDialog.disease();20秒后执行…否我想在程序中使用thread.sleep设置JavaScriptEnabled(true),因为我首先设置JavaScriptEnabled(false),然后在15秒后设置JavaScriptEnabled(true)。我该怎么办?您只需为enable the java script in run()方法添加语句,如果您需要一个单独的线程,那么您可以创建它并为该线程使用睡眠。我想添加代码,向用户显示internet状况。您使用alertDialog.setMessage(“请检查您的internet连接”);在您的第一个cod中。我希望当用户脱机时,它显示警报或进度条