Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to get return values from javascript to android as String? without wasting too much if your time.
The question is published on by Tutorial Guruji team.
The question is published on by Tutorial Guruji team.
In the following Javascript function which returns a newpid
value. From android button click I will call javascript function with passing parameters.Now my question is I want to get newpid value from javascript function to android and store it as String.
web.getSettings().setLoadsImagesAutomatically(true); web.getSettings().setJavaScriptEnabled(true); web.getSettings().setDomStorageEnabled(true); web.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { int position = (Integer) view.getTag(); Log.d(TAG, "called" + str1.get(position)+position); web.loadUrl("javascript:process_save("" + str1.get(position) + "")"); web.addJavascriptInterface(JSInterface,"androidWebViewClient"); } });
My Javascript function:
function process_save(action) { var newpid = 0; if (check_mandatory()) { var pid = geturlparameter("pid"); var wfid = geturlparameter("wfid"); var transid = geturlparameter("transid"); var partialapprove = geturlparameter("partialapprove"); var partialactivityid = geturlparameter("activityid"); var loginid = geturlparameter("loginid"); loginid = (loginid == null) ? 0 : loginid; if (pid == null && parseInt(wfid) > 0) { CallVbMethod("initiateprocess", "{'workflowid':'" + geturlparameter("wfid") + "','activityid':'" + activityid + "','activityname':'" + activityname + "','review':'" + action + "','skip':'','loginid':'" + loginid + "'}", function (result) { showerror(result[result.length - 1]); if (result[0] != "") { newpid = result[0]; saverecords(newpid); parent.disablePopup(); } }, false, false); } else if (parseInt(pid) > 0 && parseInt(transid) > 0) { showerror("processid : " + pid); saverecords(pid); CallVbMethod("changewflowstatus", "{'transid':'" + transid + "','status':'1','sqlquery':'','review':'" + action + "','skip':'','activityid':'" + partialactivityid + "','processid':'" + pid + "','partialapprove':'" + partialapprove + "','loginid':'" + loginid + "'}", function (result) { showerror(result[result.length - 1]); if (result[0] != "") { newpid = pid; showsuccess(result[0]); parent.disablePopup(); } }, false, false); } } else { alert("Mandatory field not entered"); } window.androidWebViewClient.tellAndroidPid(newpid); return newpid; }
Thanks in advance!
Answer
API Level 19+ (KITKAT and above)
webView.evaluateJavascript //JS value received on => ValueCallback<String>#onReceiveValue
Old versions
loadUrl("javascript:...") //JS value received on => JavaScriptInterface#callback
==>
webView.getSettings().setJavaScriptEnabled(true); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) webView.addJavascriptInterface(new JavaScriptInterface(), "javascriptinterface"); private void saveProcess(int position) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { webView.evaluateJavascript("javascript:process_save('" + position + "');",new ValueCallback<String>() { @Override public void onReceiveValue(String value) { // value is the result returned by the Javascript as JSON // Receive newpid here Log.d("JS",value); } }); } else { webView.loadUrl("javascript:javascriptinterface.callback(process_save('" + position + "');"); } } private class JavaScriptInterface { @JavascriptInterface public void callback(String value) { // Receive newpid here } }
Checkout this working test project, Hope that guide you to achieve your target.
We are here to answer your question about How to get return values from javascript to android as String? - If you find the proper solution, please don't forgot to share this with your team members.