I am trying to use a webview to display a webpage and let the users to
browse from the same WebView. I want to show them a progress
indeterminate when loading the pages. This works to an extent with the
code below but there is a bug in this code which I am not able to fix.
It displays the progress indeterminate correctly the first time the
page loads. But when clicked on the links it sometimes works fine and
sometimes it stops immediately though the WebView is still loading.
Can someone tell me what I am doing wrong or any other better way to
do this.
Here is the entire code.

Thank you.

package browser.com.achie;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebView2 extends Activity {

        private final String tag = "############## WEBVIEW :::: ";
        String url = "http://developer.android.com/index.html";;
        WebView mWebView;
        MyHandler mHandler = null;
    ProgressThread progressThread;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.webview);


        mWebView = (WebView) findViewById(R.id.web_view);
        mWebView.getSettings().setJavaScriptEnabled(true);

        mWebView.setWebViewClient(new MyWebViewClient());
        mWebView.loadUrl(url);

        mHandler = new MyHandler();
        progressThread = new ProgressThread(mHandler);
        mHandler.startThread();
    }


    private class MyHandler extends Handler{
        MyHandler(){
        }

        public synchronized void startThread(){
                Log.v(tag, "starting thread from handler ##########");
                setProgressBarIndeterminateVisibility(true);
                progressThread.setState(ProgressThread.STATE_RUNNING);
            progressThread.start();
        }

        public synchronized void stopThread(){
                Log.v(tag, "stopping thread from handler ##########");
                if(progressThread != null){
                        progressThread.setState(ProgressThread.STATE_DONE);
                }
                setProgressBarIndeterminateVisibility(false);
        }

        public synchronized void handleMessage(Message msg){
                int total = msg.getData().getInt("total");
                if(total >= 100){
                        stopThread();
                }
        }
    }

    private class ProgressThread extends Thread{
        Handler mHandler;
        final static int STATE_DONE = 0;
        final static int STATE_RUNNING = 1;
        int mState;

        ProgressThread(Handler h){
                mHandler = h;
        }

        public synchronized void run(){
                mState = STATE_RUNNING;
                int messagesSent = 0;
                while (mState == STATE_RUNNING){
                        try{
                                Thread.sleep(200);
                        }catch(InterruptedException e){
                                Log.e("ERROR: ", "Thread Interrupted");
                        }
                        Log.v(">>>>>>>>>>>>Progress of the webView", " is : " +
mWebView.getProgress());
                        if(messagesSent ==0 && mWebView.getProgress()==100){
                                messagesSent++;
                                Message msg = mHandler.obtainMessage();
                                Bundle b = new Bundle();
                                b.putInt("total", 100);
                                msg.setData(b);
                                mHandler.sendMessage(msg);
                                break;
                        }
                }
        }

        /*
         * Sets the current state for the thread,
         * Used to stop the thread
         */
        public void setState(int state){
                        Log.v(">>>>>>>>>>>><<<<<<<<Setting state as: ", " : " + 
state);
                mState = state;
        }
    }

        private class MyWebViewClient extends WebViewClient{
//              ProgressThread zProgressThread;
                MyHandler myHandler;
                @Override
                public synchronized boolean shouldOverrideUrlLoading(WebView 
view,
String url){
                        view.loadUrl(url);

                        if(myHandler == null){
                                myHandler = new MyHandler();
                        }
//                      if(progressThread != null){
//                              zHandler.stopThread();
//                      }
                        progressThread = new ProgressThread(myHandler);
                        myHandler.startThread();
                        return true;
                }

        }

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

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe from this group, send email to 
android-developers+unsubscribegooglegroups.com or reply to this email with the 
words "REMOVE ME" as the subject.

Reply via email to