JavaScript does not work in API 17 and up

1

I made the webview of a web app and it works perfectly with versions of API 16 below. However, from the Jelly Bean MRI (API 17) onwards, there are various errors (depending on the API version) in the JavaScript. I read that you have to add a new public class of JavascriptInterface () or something so that the webview detects all the javascript methods and works in the webviwew. But I do not know how to implement it in my main_activity.java code. You already have setJavaScriptEnabled ("true"), but that only works with APIS lower than 17 ... I do not know what to do. Can someone please show me how to solve this in my code?

main_activity.java:

public class MainActivity extends Activity {

/* URL saved to be loaded after fb login */
private static final String target_url="http://www.chapatelo.com.ar/";
private static final String target_url_prefix="www.chapatelo.com.ar";
private Context mContext;
private WebView mWebview;
private WebView mWebviewPop;
private RelativeLayout mContainer;
private long mLastBackPressTime = 0;
private Toast mToast;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // final View controlsView =
    // findViewById(R.id.fullscreen_content_controls);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    mWebview = (WebView) findViewById(R.id.activity_main_webview);
    //mWebviewPop = (WebView) findViewById(R.id.webviewPop);
    mContainer = (RelativeLayout) findViewById(R.id.webview_relative);
    WebSettings webSettings = mWebview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setSupportMultipleWindows(true);
    mWebview.setWebViewClient(new UriWebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            findViewById(R.id.splashLoading1).setVisibility(View.GONE);
            findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
        }
    });
    mWebview.setWebChromeClient(new UriChromeClient());
    mWebview.loadUrl(target_url);

    mContext=this.getApplicationContext();

}


private class UriWebViewClient extends WebViewClient{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        String host = Uri.parse(url).getHost();
        //Log.d("shouldOverrideUrlLoading", url);
        if (host.equals(target_url_prefix))
        {
            // This is my web site, so do not override; let my WebView load
            // the page
            if(mWebviewPop!=null){
                mWebviewPop.setVisibility(View.GONE);
                mContainer.removeView(mWebviewPop);
                mWebviewPop=null;
            }
            return false;
        }

        if(host.equals("m.facebook.com") || host.equals("www.facebook.com")){
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch
        // another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler,
                                   SslError error) {
        Log.d("onReceivedSslError", "onReceivedSslError");
        //super.onReceivedSslError(view, handler, error);
    }
}

class UriChromeClient extends WebChromeClient {

    @Override
    public boolean onCreateWindow(WebView view, boolean isDialog,
                                  boolean isUserGesture, Message resultMsg) {
        mWebviewPop = new WebView(mContext);
        mWebviewPop.setVerticalScrollBarEnabled(false);
        mWebviewPop.setHorizontalScrollBarEnabled(false);
        mWebviewPop.setWebViewClient(new UriWebViewClient());
        mWebviewPop.getSettings().setJavaScriptEnabled(true);
        mWebviewPop.getSettings().setSavePassword(false);
        mWebviewPop.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        mContainer.addView(mWebviewPop);
        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(mWebviewPop);
        resultMsg.sendToTarget();

        return true;
    }

    @Override
    public void onCloseWindow(WebView window) {
        Log.d("onCloseWindow", "called");
    }
}

@Override
public void onBackPressed(){
    if(mWebview.canGoBack()){
        mWebview.goBack();
    }else{
        super.onBackPressed();
    }
}
}
    
asked by Criss 31.05.2016 в 19:05
source

2 answers

1

It must be made clear that to enable Javascript in a WebView it is done through the setJavaScriptEnabled () , this has always been from the Android API 1.

   WebSettings webSettings = myWebview.getSettings();
   webSettings.setJavaScriptEnabled(true);

and I see you do it correctly.

In terms of having problems working with API 17+ check the documentation :

  

Caution: If you have set your targetSdkVersion to 17 or more, it should be   add the @JavascriptInterface annotation to any method you want   are available for JavaScript (the method must also be   public). If you do not provide the annotation, the method is not   accessible on your website when running on Android 4.2 or   superior.

Add the @JavascriptInterface annotation to your methods where you use javascript:

@JavascriptInterface
public void miMetodoJS() {
    ...
    ...
    ...
}
    
answered by 31.05.2016 в 19:33
0

The problem was in the app itself. Problems in the CSS and the display of certain elements for certain types of screens. It was not a problem on the android. This works perfectly fine.

    
answered by 02.06.2016 в 02:02