在下面的Javascript函数中,该函数返回一个newpid
值。从android按钮单击,我将调用带有传递参数的javascript函数。现在,我的问题是我想从javascript函数获取android的newpid值并将其存储为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");
}
});
我的Javascript函数:
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;
}
谢谢!
答案 0 :(得分:0)
在活动中添加方法
@JavascriptInterface
public void tellAndroidPid(String pid){
//you can save your pid in here,
Log.i("check pid"," get pid:"+pid);
}
为您的webview添加js界面
webview.addJavascriptInterface(this,"androidWebViewClient");
以js方法调用本机接口
function process_save(action) {
...
window.androidWebViewClient.tellAndroidPid(YOUR_PID);
...
}
别忘了启用js功能
setting.setJavaScriptEnabled(true);//here setting is WebSettings,you can get it by webview.getSettings()
重新编辑,调用前应添加js接口
web.getSettings().setLoadsImagesAutomatically(true);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setDomStorageEnabled(true);
web.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
web.addJavascriptInterface(JSInterface,"androidWebViewClient");
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) + "\")");
}
});
答案 1 :(得分:0)
API级别 19岁以上(KITKAT及以上)
webView.evaluateJavascript //JS value received on => ValueCallback<String>#onReceiveValue
旧版本
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
}
}
查看此working test project,希望能指导您实现目标的
。