0

I have two devices, Samsung Tablet GT-P3113 and Mini-PC rk31 sdk (both android 4.2.2), running my app. My app use a javascript interface to access a java method. In one of the two devices works fine(samsung), but the other don't.

BrowsableView - my class extends webview


 public class BrowsableView extends WebView {

     @SuppressLint("SetJavaScriptEnabled")
     public BrowsableView(Context context, AttributeSet attrs) {

        super(context, attrs);

        /// configurações para permitir execução de javascript
        WebSettings webSettings =  getSettings();              
        webSettings.setPluginState(PluginState.ON);
        webSettings.setJavaScriptEnabled(true);            

        setClickable(true);

        setKeepScreenOn(true);

        setBackgroundColor(Color.TRANSPARENT);    

        setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);        

        addJavascriptInterface(new WebAppInterface(Config.getContext()), "Android");

        setWebViewClient(new BrowsableViewClient());

        setWebChromeClient(new BrowsableViewChromeClient());
    }

    public void prepareNextHtml(String url){
        loadUrl(url);        
    }

    public class BrowsableViewClient extends WebViewClient{

        @Override
        public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {

            Log.d(TAG, "EVENTO ONERROR DENTRO DO BROWSABLE");                        
            _dispatchBrowsableViewErrorEvent();
        }
    }

    public class BrowsableViewChromeClient extends WebChromeClient{

        @Override
        public boolean onConsoleMessage(ConsoleMessage cm){
            Log.d("WebView", String.format("%s @ %d: %s", cm.message(), cm.lineNumber(), cm.sourceId()));
            Toast.makeText(getContext(), "WebViewConsole: " +  String.format("%s @ %d: %s", cm.message(), cm.lineNumber(), cm.sourceId()), Toast.LENGTH_LONG).show();
            return true;
        }
    }

    public class WebAppInterface {

        Context mContext;

        public WebAppInterface(Context c){
            mContext = c;   
        }            

        @JavascriptInterface
        public void finish(){              
            handler.removeCallbacksAndMessages(null);

            Toast.makeText(getContext(), "Evento onCompletion no BrowsableView  - Android.finish().", Toast.LENGTH_LONG).show();                      
            _dispatchBrowsableViewCompletionEvent();
        }
    }
}

Html Sample

<html>
<head>
<script type="text/javascript">
    setTimeout("Android.finish()",8000);    
</script>
</head>
<body width="100%" height="100%">
    <img width="100%" height="100%" src="654138997615629/teste.jpg" />    
</body>
</html>

In Mini-Pc device does not recognize the method the javascript interface and returns error like that

Uncaught TypeError: Object [object Object] has no method "finish"

  • 1
    possible duplicate of [Android 4.2.1, WebView and javascript interface breaks](http://stackoverflow.com/questions/14031635/android-4-2-1-webview-and-javascript-interface-breaks) – CRABOLO Aug 17 '14 at 10:11

1 Answers1

-1

I had similar problem. On some 4.4.2 device the callback was made, on some it wasn't. I found that downgrading the SDK in 'AndroidManifest.xml' helps:

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="16"
/>

instead of 19 and 19. Now the callback from JavaScript to @JavascriptInterface method is happening even on device that was ignoring it.

Jaroslav Tulach
  • 519
  • 4
  • 7