tonny-zhang / tonny-zhang.github.com

4 stars 1 forks source link

android webview 中alert不执行! #6

Closed tonny-zhang closed 10 years ago

tonny-zhang commented 10 years ago

1.今天做一个Demo郁闷了一下午,我在Android里要调用webview页面里的一个方法,但一直没有执行(用简单的alert调试)

2.后改成其它的调试方式,改写dom元素的内容达到调试目的,发现这种方案可行,就上网上查原因,最后找到如下解释:

One should use a WebChromeClient to get these alerts and the functionality can be overwritten using the onJsAlert() method. Hope this helps!

WebView wv=new WebView(this);   
wv.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                //Required functionality here
                return super.onJsAlert(view, url, message, result);
       }

https://code.google.com/p/android/issues/detail?id=752#c12 http://q.cnblogs.com/q/47060/

坑爹,现在还没有想明白为什么webview默认会把alert的特性禁用掉呢?

tonny-zhang commented 10 years ago

http://developer.android.com/reference/android/webkit/WebView.html

A WebView has several customization points where you can add your own behavior. These are:

Here's a more complicated example, showing error handling, settings, and progress notification:

// Let's display the progress in the activity title bar, like the
 // browser app does.
 getWindow().requestFeature(Window.FEATURE_PROGRESS);

 webview.getSettings().setJavaScriptEnabled(true);

 final Activity activity = this;
 webview.setWebChromeClient(new WebChromeClient() {
   public void onProgressChanged(WebView view, int progress) {
     // Activities and WebViews measure progress with different scales.
     // The progress meter will automatically disappear when we reach 100%
     activity.setProgress(progress * 1000);
   }
 });
 webview.setWebViewClient(new WebViewClient() {
   public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
     Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
   }
 });

 webview.loadUrl("http://developer.android.com/");