marchemike
marchemike

Reputation: 3277

JavascriptInterface for Webview on Android Jelly Bean and above

I used to have a javascript interface on my mobile app that allows me to capture data from my webview back to the app, which used to work ,but recently when I upgraded the app to handle lollipop it started having issues regarding the javascriptInterface.

This is my current code for the javascriptInterface:

webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new MyBrowser());
webView.getSettings().setBuiltInZoomControls(false);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
webView.getSettings().setJavaScriptEnabled(true);
webView.setHorizontalScrollBarEnabled(false);

@JavascriptInterface
public boolean doSomething(String employeeID){

    RunSearch(employeeID);
    Log.d("runsearch", "running search");
    return true;
}
public class WebAppInterface {

    Context mContext;

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

My webpage is stored locally

The error says that :

None of the methods in the added interface have been annotated with @android.webkit.JavascriptInterface; they will not be visible in API 17

I've looked into the documentation but What should I do to make it work with Jelly Bean and higher?

Upvotes: 0

Views: 1198

Answers (1)

EJK
EJK

Reputation: 12534

The problem is that you have not exposed any callable methods in your WebAppInterface. I assume from the annotation that you intent for the doSomething method to be exposed? If so, simply move it into the interface.

public class WebAppInterface {

    Context mContext;

    WebAppInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public boolean doSomething(String employeeID){

        RunSearch(employeeID);
        Log.d("runsearch", "running search");
        return true;
    }
}

Upvotes: 3

Related Questions