Android - WebView OnPageStarted,OnPageFinished& shouldOverrideUrlLoading没有被调用

时间:2014-03-18 13:15:17

标签: android webview

我正在开发一个应用程序,当用户从一个链接导航到另一个链接时,我想要更改edittext的内容,并希望在edittext中显示当前网址。

但是这三种方法都不起作用,甚至不打印日志。这是代码。

    wv.setWebViewClient(new WebViewClient()
    {
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {               

            String url_new = view.getUrl();

            Log.v("","Webview URL: "+url);

            addressbar.setText(url_new);

            return false;                            
        }

        @Override
        public void onPageFinished(WebView view, String url)
        {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            String url_new = view.getUrl();                

            Log.v("","Webview Function URL: "+url_new);

            addressbar.setText(url_new);
        }

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

            String url_new = view.getUrl();             

            Log.v("","Webview Function URL: "+url_new);

            addressbar.setText(url_new);
        }
    }); 

wvWebViewaddressbar是上述代码中的EditText

我缺少什么?

请帮助.. !!

编辑:整个代码..

public class Clip_From_Web_Home extends Activity
 {
WebView wv;

TextView back, clip;
ProgressDialog dialog;

Button previous, reload, next, go;

LinearLayout ll;
public static final int REQUEST_CODE_CROP_IMAGE   = 0x1;
Bitmap drawingCache;

public File mFileTemp;
public static final String TEMP_PHOTO_FILE_NAME = "temp_photo_web_clip.png";
FileOutputStream out;

EditText addressbar;

String url;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.clip_from_web);

    wv = (WebView) findViewById(R.id.clip_from_web_webView1);

    back = (TextView) findViewById(R.id.clip_from_web_textview_back);
    back.setOnClickListener(new OnClickListener()
    {           
        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub
            finish();
        }
    });

    url = "http://www.google.com";

    addressbar = (EditText) findViewById(R.id.clip_web_address_bar);
    addressbar.setText(url);

    //wv.getSettings().setJavaScriptEnabled(true);
    wv.setWebViewClient(new WebViewClient()
    {
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {               

            String url_new = view.getUrl();

            Log.v("","Webview URL: "+url);

            addressbar.setText(url_new);

            return false;                            
        }

        @Override
        public void onPageFinished(WebView view, String url)
        {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            String url_new = view.getUrl();                

            Log.v("","Webview Function URL: "+url_new);

            addressbar.setText(url_new);
        }

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

            String url_new = view.getUrl();             

            Log.v("","Webview Function URL: "+url_new);

            addressbar.setText(url_new);
        }
    }); 

    go = (Button) findViewById(R.id.clip_web_go);
    go.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub

            url = addressbar.getText().toString();

            Log.v("","URL ENTERED: "+url);

            if(url.equals(""))
            {
                url = "http://www.google.com";

                dialog = ProgressDialog.show(Clip_From_Web_Home.this,"","Loading",true,false);

                new webview_load().execute();
            }
            else
            {
                dialog = ProgressDialog.show(Clip_From_Web_Home.this,"","Loading",true,false);

                new webview_load().execute();
            }

        }
    });                 

    dialog = ProgressDialog.show(Clip_From_Web_Home.this,"","Loading",true,false);

    new webview_load().execute();   
}   

class webview_load extends AsyncTask<Void, Void, Void>
{

    @Override
    protected Void doInBackground(Void... arg0)
    {
        // TODO Auto-generated method stub              
        return null;
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onPostExecute(Void result)
    {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        wv.setBackgroundColor(0);
        wv.setBackgroundResource(android.R.color.black);
        wv.setWebChromeClient(new WebChromeClient());
        wv.setWebViewClient(new WebViewClient());
        WebSettings settings = wv.getSettings();
        settings.setBuiltInZoomControls(true);
        //settings.setDefaultZoom(ZoomDensity.MEDIUM);
        settings.setDefaultZoom(ZoomDensity.FAR);

         wv.setInitialScale(125);
       // settings.setUseWideViewPort(true);
        settings.setRenderPriority(RenderPriority.HIGH);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);

        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setLoadWithOverviewMode(true);
        wv.getSettings().setUseWideViewPort(false);

        wv.loadUrl(url);

        int secondsDelayed = 5;
        new Handler().postDelayed(new Runnable()
        {
                public void run()
                {
                    Log.v("","In Handler");                  
                    dialog.dismiss();
                }
        }, secondsDelayed * 1000);          
    }
}   
}

1 个答案:

答案 0 :(得分:3)

您在AsyncTask对象中覆盖了WebViewClient:

wv.setWebViewClient(new WebViewClient());

这会覆盖您在Activity.onCreate()中设置的自定义WebViewClient。

你也可以摆脱整个AsyncTask实现。您可以在UI线程中的WebView中调用loadUrl而不会出现任何问题。这可能会大大简化你的工作。