进度和刷新网页流量的组合机器人 [英] progressBar and refresh webview combination android

查看:144
本文介绍了进度和刷新网页流量的组合机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:
我使用工作正常网页视图一个进度条。现在我想刷新我的网页视图,这也适用。当我试图把它们结合在一起,微调不会结束的。这是我的code:

I am using a ProgressBar for a Webview that works fine. Now I want to refresh my Webview, which also works. When I am trying to combine them, the Spinner doesn't end at all. This is my code:

private final Runnable m_Runnable = new Runnable()
    {
        public void run()

        {
            //Toast.makeText(WebcamsScreen.this,"in runnable",Toast.LENGTH_SHORT).show();
            doRefreshingStuff(id);
            WebcamsScreen.this.mHandler.postDelayed(m_Runnable, 20000);            
        }

    };



    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            //initialitions 

        doRefreshingStuff(id);
        this.mHandler = new Handler();

        this.mHandler.postDelayed(m_Runnable,20000);

        doRefreshingStuff(id);
    }
public void doRefreshingStuff(String id){

                setContentView(R.layout.web4);

                webView7 = (WebView) findViewById(R.id.web_4_1);

                webView7.getSettings().setJavaScriptEnabled(true);
                webView7.getSettings().setJavaScriptEnabled(true);
                progressBar = ProgressDialog.show(WebcamsScreen.this, "example", "...");
                url="my url";
webView7.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url1) {              
                    view.loadUrl(url1);
                    return true;
                }


              public void onPageFinished(WebView view, String url1) {
                    if (progressBar.isShowing()) {
                        progressBar.dismiss();
                    }
                }
            });
              webView7.loadUrl(url);

什么是必须更改?

what must I change?

推荐答案

它看起来像您刷新了每20秒的网页视图,并弹出一个新的进展对话框当你这样做。您code假定旧的进度对话框已经被开除。如果还没有,那么进度对话框刚刚成为孤儿,而且将继续展出,直到永远。您可以通过插入该code可能解决这个问题:

It looks like you are refreshing your WebView every 20 seconds and popping up a new progress dialog when you do. Your code assumes that the old progress dialog has already been dismissed. If it hasn't, then the progress dialog just became orphaned and will remain on display forever. You can probably fix this by inserting this code:

if (progressBar != null && progressBar.isShowing()) {
    progressBar.dismiss();
}

只是前行进度= ...

但我不会解决您的code的方式。你正在做的,你不需要做工作,数额巨大。 (举例来说,你不需要重新加载相同的内容视图)。这里是我的修订版:

But I wouldn't fix your code that way. You are doing a huge amount of work that you don't need to do. (For instance, you don't need to reload the same content view.) Here's my revised version:

private final Runnable m_Runnable = new Runnable() {
    public void run() {
        doRefreshingStuff(id);
        mHandler.postDelayed(m_Runnable, 20000);
    }
};

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        //initialitions 

    setContentView(R.layout.web4);
    webView7 = (WebView) findViewById(R.id.web_4_1);
    webView7.getSettings().setJavaScriptEnabled(true);
    webView7.getSettings().setJavaScriptEnabled(true);
    webView7.setWebViewClient(new WebViewClient() {
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if (!mProgressBar.isShowing()) {
                mProgressBar.show();
            }
        }
        public void onPageFinished(WebView view, String url1) {
            if (mProgressBar.isShowing()) {
                mProgressBar.dismiss();
            }
        }
    });

    mProgressBar = new ProgressDialog(this);
    mProgressBar.setTitle("example");
    mProgressBar.setMessage("...");

    mHandler = new Handler();
}

protected void onResume() {
    super.onResume();
    mHandler.post(m_Runnable);
}

protected void onPause() {
    super.onPause();
    mHandler.removeCallbacks(m_Runnable);
}

public void doRefreshingStuff(String id) {
    url="my url";
    webView7.loadUrl(url);
}

请注意,我的页面加载环路的起点移到 onResume()和杀死的onPause的循环()。您可能需要稍微不同的逻辑,但你一定要以某种方式停止循环,当你的活动暂停或结束。

Note that I moved the starting of the page loading loop to onResume() and kill the looping in onPause(). You may want slightly different logic, but you should definitely stop looping somehow when your activity pauses or finishes.

这篇关于进度和刷新网页流量的组合机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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