进度条只适用于装载页面? [英] Progress bar only work on loading page?

查看:93
本文介绍了进度条只适用于装载页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C中的进度条工作在第一页加载以下$ C $。当我点击其他链接之后,progrss栏不显示?

如果有人能告诉我如何改变这个code有进度条上的工作负载的所有?

谢谢

安德烈。

 公共类Android_Activity延伸活动{
的WebView的WebView;

@覆盖
保护无效的onSaveInstanceState(包outState){
    的WebView的WebView =(web视图)findViewById(R.id.webview);
    webview.saveState(outState);
}

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    //设置的主要内容视图
    的setContentView(R.layout.main);

    //检查是否一个实例存储,因此恢复
    如果(savedInstanceState!= NULL){
        ((web视图)findViewById(R.id.webview))restoreState(savedInstanceState)。
    }

    最后ProgressDialog进度= ProgressDialog.show(这一点,的getString(R.string.progress_title)
            的getString(R.string.progress_msg));

    的WebView =(web视图)findViewById(R.id.webview);
    webview.setHttpAuthUsernamePassword(的getString(R.string.kdg_host)
            的getString(R.string.kdg_realm)
            的getString(R.string.kdg_user_name)
            的getString(R.string.kdg_password)
            );
    //webview.setWebViewClient(new myWebViewClient());
    webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webview.getSettings()setRenderPriority(RenderPriority.HIGH)。
    。webview.getSettings()setCacheMode(WebSettings.LOAD_NO_CACHE);
    。webview.getSettings()setAllowFileAccess(真正的);
    webview.getSettings()setJavaScriptEnabled(真)。

    webview.setWebViewClient(新WebViewClient(){
        公共无效onPageStarted(web视图查看,字符串URL){
            如果(!progressBar.isShowing()){
                progressBar.show();
            }
        }
        公共无效onPageFinished(web视图查看,字符串URL){
            //super.onPageFinished(view,URL);
            如果(progressBar.isShowing()){
                progressBar.dismiss();
            }
        }
    });

    webview.loadUrl(的getString(R.string.base_url));
}

@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){
    如果((钥匙code == KeyEvent.KEY code_BACK)及和放大器; webview.canGoBack()){
        webview.goBack();
        返回true;
    }
    返回super.onKeyDown(键code,事件);
}

私有类myWebViewClient扩展WebViewClient {
    @覆盖
    公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
        布尔错误=虚假的;

        //检查它是否是一个MAILTO URL
        如果(url.substring(0,6).equalsIgnoreCase(电子邮件地址)){
            意图emailIntent =新的意图(android.content.Intent.ACTION_SEND);
            emailIntent .setType(纯/文);
            emailIntent .putExtra(android.content.Intent.EXTRA_EMAIL,新的String [] {url.substring(7)});
            startActivity(Intent.createChooser(emailIntent,发送邮件...));
            返回true;
        }
        //检查它是否是一个EXT://(调用Web浏览器)
        否则如果(url.substring(0,6).equalsIgnoreCase(分机://)){
            乌里uriUrl = Uri.parse(url.substring(6));
            意图launchBrowser =新的意图(Intent.ACTION_VIEW,uriUrl);
            startActivity(launchBrowser);
            返回true;
        }
        //检查它是否是一个RTSP URL
        否则如果(url.substring(0,4).equalsIgnoreCase(RTSP)){
            开放的我们的uri = Uri.parse(URL);
            意向意图=新的意图(Intent.ACTION_VIEW,URI);
            startActivity(意向);
            返回true;
        }
        //否则,如果它是一个MP4文件链接
        否则如果(url.endsWith(。MP4)){
            开放的我们的uri = Uri.parse(URL);
            意向意图=新的意图(Intent.ACTION_VIEW);
            intent.setDataAndType(URI,视频/ MP4);
            startActivity(意向);
            返回true;
        }
        //否则,如果它是一个3GP文件链接
        否则如果(url.endsWith(。3GP)){
            开放的我们的uri = Uri.parse(URL);
            意向意图=新的意图(Intent.ACTION_VIEW);
            intent.setDataAndType(URI,视频/ 3GP);
            startActivity(意向);
            返回true;
        }
        //否则,如果它是一个MP3文件链接
        否则,如果(url.endsWith(MP3)){
            开放的我们的uri = Uri.parse(URL);
            意向意图=新的意图(Intent.ACTION_VIEW);
            intent.setDataAndType(URI,音频/ MP3);
            startActivity(意向);
            返回true;
        }
        //否则,如果它是一个网页的网址
        其他{
            URL网址;
            尝试 {
                URL =新的URL(网址);
                HttpURLConnection的HttpURLConnection的=(HttpURLConnection类)网址
                        .openConnection();
                INT响应= httpURLConnection.getResponse code();
                如果((应答GT; = 200)及及(应答所述; 400))
                    //view.loadUrl(url);
                    错误=虚假的;
                其他
                    错误= TRUE;
            }赶上(例外五){
                错误= TRUE;
            }
        }

        //如果出现错误
        如果(错误){
            showErrorDialog();
        }

        view.loadUrl(URL);
        返回true;
    }
}

@覆盖
公共无效onResume(){
    super.onResume();
    webview.reload();
}

公共无效onReceivedError(web视图来看,INT错误code,
        字符串描述,字符串failingUrl){
    showErrorDialog();
}

公共无效showErrorDialog(){
    AlertDialog alertDialog =新AlertDialog.Builder(本).create();
    alertDialog.setTitle(的getString(R.string.error_title));
    alertDialog.setMessage(的getString(R.string.error_msg));
    alertDialog.setButton(的getString(R.string.ok_btn)
            新DialogInterface.OnClickListener(){
                公共无效的onClick(DialogInterface对话框,
                        其中INT){
                }
            });
    alertDialog.show();
}
}
 

解决方案

使用以下工作code ::

 公共类Android_Activity延伸活动{
私人Android_Activity _activity;
    @覆盖
        公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            。getWindow()requestFeature(Window.FEATURE_PROGRESS);
            _activity =这一点;
            的setContentView(R.layout.main);

            mwebview =(web视图)view.findViewById(R.id.webview);
            mwebview.getSettings()setJavaScriptEnabled(真)。
            。mwebview.getSettings()setJavaScriptCanOpenWindowsAutomatically(真正的);

            如果(checkInternetConnection(_activity)==真){
                如果(savedInstanceState == NULL)
                    mwebview.loadUrl(URL);
                其他
                    mwebview.restoreState(savedInstanceState);
            }
            其他{
                AlertDialog.Builder建设者=新AlertDialog.Builder(_activity);
                builder.setMessage(请检查您的网络连接。)
                       .setCancelable(假)
                       .setPositiveButton(OK,新DialogInterface.OnClickListener(){
                           公共无效的onClick(DialogInterface对话框,INT ID){

                           }
                       });

                AlertDialog警报= builder.create();
                alert.show();
            }
            mwebview.setWebChromeClient(新WebChromeClient(){

                @覆盖
                公共无效onProgressChanged(web视图来看,INT进度){
                    如果(mwebview.getVisibility()== View.VISIBLE)
                    {
                        _activity.setProgress(进度* 100);
                    }
                }
            });
            mwebview.setWebViewClient(新HelloWebViewClient());
        }


        // HelloWebViewClient类的WebView
        私有类HelloWebViewClient扩展WebViewClient {

            @覆盖
            公共无效onPageStarted(web视图查看,字符串URL,位图图标){
                // TODO自动生成方法存根
                super.onPageStarted(查看,网址,网站图标);
            }
            @覆盖
            公共无效onReceivedError(web视图来看,INT错误code,
                    字符串描述,字符串failingUrl){
                // TODO自动生成方法存根
                super.onReceivedError(查看,错误code,说明,failingUrl);

            }
            @覆盖
            公共无效onPageFinished(web视图查看,字符串URL){
                // TODO自动生成方法存根
                super.onPageFinished(查看,网址);
            }
            @覆盖
            公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
                //否则,该链接不上我的网站页面,因此启动该处理URL另一个活动
                view.loadUrl(URL);
                返回true;
            }

        } // HelloWebViewClient级
        @覆盖
        公共布尔的onkeydown(INT键code,KeyEvent的事件){
            //检查关键事件是后退按钮,如果有历史记录
            如果((钥匙code == KeyEvent.KEY code_BACK)及和放大器; mwebview.canGoBack()){
                mwebview.goBack();
                返回true;
            }
            //如果不是返回键或没有网页的历史,泡到默认
            //系统行为(可能退出活动)
            返回super.onKeyDown(键code,事件);
        }
        //要检查是否可用的网络连接设备上或不
            公共静态布尔checkInternetConnection(活动_activity){
                ConnectivityManager conMgr =(ConnectivityManager)_activity.getSystemService(Context.CONNECTIVITY_SERVICE);
                如果(conMgr.getActiveNetworkInfo()!=空
                        &功放;&安培; conMgr.getActiveNetworkInfo()。isAvailable()
                        &功放;&安培; conMgr.getActiveNetworkInfo()。isConnected())
                    返回true;
                其他
                    返回false;
            } // checkInternetConnection()
}
 

I've in the following code the progressbar working on the first page load. after when I click on other links, the progrss bar is not shown ?

If somebody can tell me how to change this code to have the progress bar working on all loading ?

Thanks,

André.

public class Android_Activity extends Activity {
WebView webview;

@Override
protected void onSaveInstanceState(Bundle outState) {
    WebView webview = (WebView) findViewById(R.id.webview);
    webview.saveState(outState);
}   

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // set the main content view
    setContentView(R.layout.main);

    // check if an instance is stored and so restore it
    if (savedInstanceState != null){
        ((WebView)findViewById(R.id.webview)).restoreState(savedInstanceState);
    }

    final ProgressDialog progressBar = ProgressDialog.show(this, getString(R.string.progress_title),
            getString(R.string.progress_msg));

    webview = (WebView)findViewById(R.id.webview);
    webview.setHttpAuthUsernamePassword(getString(R.string.kdg_host),
            getString(R.string.kdg_realm),
            getString(R.string.kdg_user_name),
            getString(R.string.kdg_password)
            );
    //webview.setWebViewClient(new myWebViewClient());
    webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webview.getSettings().setRenderPriority(RenderPriority.HIGH);
    webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    webview.getSettings().setAllowFileAccess(true);
    webview.getSettings().setJavaScriptEnabled(true);

    webview.setWebViewClient(new WebViewClient() {
        public void onPageStarted(WebView view, String url) {
            if (!progressBar.isShowing()) {
                progressBar.show();
            }
        }           
        public void onPageFinished(WebView view, String url) {
            //super.onPageFinished(view, url);
            if (progressBar.isShowing()) {
                progressBar.dismiss();
            }
        }           
    });

    webview.loadUrl(getString(R.string.base_url));
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
        webview.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

private class myWebViewClient extends WebViewClient {   
    @Override       
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        boolean error = false;          

        // check if it's an MAILTO URL
        if(url.substring(0, 6).equalsIgnoreCase("mailto")){
            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent .setType("plain/text");
            emailIntent .putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{url.substring(7)});
            startActivity(Intent.createChooser(emailIntent, "Send mail..."));   
            return true;
        }
        // check if it's an EXT:// (Call Web Browser)
        else if(url.substring(0, 6).equalsIgnoreCase("ext://")){
            Uri uriUrl = Uri.parse(url.substring(6));
            Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl); 
            startActivity(launchBrowser);   
            return true;
        }               
        // check if it's an RTSP URL
        else if(url.substring(0, 4).equalsIgnoreCase("rtsp")){
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
            return true;
        }
        // else if it's an MP4 file link
        else if(url.endsWith(".mp4")){
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "video/mp4");
            startActivity(intent);
            return true;
        }
        // else if it's a 3GP file link
        else if(url.endsWith(".3gp")){
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "video/3gp");
            startActivity(intent);
            return true;
        }
        // else if it's a MP3 file link
        else if(url.endsWith(".mp3")){
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "audio/mp3");
            startActivity(intent);
            return true;
        }               
        // else if it's a page URL
        else{
            URL Url;
            try {
                Url = new URL(url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) Url
                        .openConnection();
                int response = httpURLConnection.getResponseCode();
                if ((response >= 200)&&(response < 400))
                    //view.loadUrl(url);
                    error = false;
                else
                    error = true;
            } catch (Exception e) {
                error = true;
            }
        }

        // if an error occurred
        if (error) {
            showErrorDialog();
        }

        view.loadUrl(url);          
        return true;                    
    }
}   

@Override
public void onResume() {
    super.onResume();
    webview.reload();
}   

public void onReceivedError(WebView view, int errorCode,
        String description, String failingUrl) {
    showErrorDialog();
}

public void showErrorDialog(){
    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle(getString(R.string.error_title));
    alertDialog.setMessage(getString(R.string.error_msg));
    alertDialog.setButton(getString(R.string.ok_btn),
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int which) {
                }
            });
    alertDialog.show();
}
}

解决方案

Use below working code ::

public class Android_Activity extends Activity {
private Android_Activity _activity;
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);     
            getWindow().requestFeature(Window.FEATURE_PROGRESS);
            _activity = this;   
            setContentView(R.layout.main);

            mwebview=(WebView)view.findViewById(R.id.webview);
            mwebview.getSettings().setJavaScriptEnabled(true);
            mwebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

            if(checkInternetConnection(_activity)==true){
                if(savedInstanceState==null)
                    mwebview.loadUrl(URL);
                else
                    mwebview.restoreState(savedInstanceState);
            }
            else{
                AlertDialog.Builder builder = new AlertDialog.Builder(_activity);
                builder.setMessage("Please check your network connection.")
                       .setCancelable(false)
                       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {

                           }
                       });

                AlertDialog alert = builder.create();    
                alert.show();
            }
            mwebview.setWebChromeClient(new WebChromeClient() {

                @Override
                public void onProgressChanged(WebView view, int progress) { 
                    if(mwebview.getVisibility()==View.VISIBLE)
                    {
                        _activity.setProgress(progress * 100);
                    }
                }
            });
            mwebview.setWebViewClient(new HelloWebViewClient());
        }


        //HelloWebViewClient class for webview
        private class HelloWebViewClient extends WebViewClient {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
            }
            @Override
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                // TODO Auto-generated method stub
                super.onReceivedError(view, errorCode, description, failingUrl);

            }
            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
                view.loadUrl(url);
                return true;
            }

        }   //HelloWebViewClient-class
        @Override
        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) && mwebview.canGoBack() ){
                mwebview.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);
        }
        //To check whether network connection is available on device or not
            public static boolean checkInternetConnection(Activity _activity) {
                ConnectivityManager conMgr = (ConnectivityManager) _activity.getSystemService(Context.CONNECTIVITY_SERVICE);
                if (conMgr.getActiveNetworkInfo() != null
                        && conMgr.getActiveNetworkInfo().isAvailable()
                        && conMgr.getActiveNetworkInfo().isConnected()) 
                    return true;
                else
                    return false;
            }//checkInternetConnection()
}

这篇关于进度条只适用于装载页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆