亲爱的我只是传递并从javascript和android返回一些价值。我能够将值javascript传递给android。我的问题是我无法再次返回该值。这是我的片段。任何人都可以帮助我
HTML和脚本
<html>
<head>
<script src="phonegap-1.3.0.js" type="text/javascript"></script>
<script type="text/javascript">
function invoke(param1,param2)
{
alert('Hai');
//invoking the JavascriptBridge registered under the 'jb' namespace
var result = jb.callMe(param1,param2);
//doing something with the return value, it should be concatenation
//of the two input parameters
alert(result);
}
</script>
</head>
<body>
<form id = "returning">
<h2>Demonstrating Android Javascript-To-Java Bridge</h2>
<input type="button" value="Invoke Bridge" onclick="invoke('Hello','World');"/>
</form>
</body>
机器人:
public class ReturnAndroidValActivity extends Activity
{
private WebView webView;
public ReturnAndroidValActivity()
{
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
protected void onStart()
{
super.onStart();
}
@Override
protected void onResume()
{
try
{
super.onResume();
//render the main screen
// this.setContentView(ViewHelper.findLayoutId(this, "main"));
//Find the WebView control
//this.webView = (WebView)ViewHelper.findViewById(this, "webview");
setContentView(R.layout.main);
this.webView = (WebView)findViewById(R.id.mybrowser);
//Enable Javascript...This is needed so that Javascript is allowed to execute
//inside the WebView
WebSettings webSettings = this.webView.getSettings();
webSettings.setJavaScriptEnabled(true);
//Register the 'Javascript Bridge' class under the 'jb' namespace
//this class can be invoked from the HTML/Javascript side
this.webView.addJavascriptInterface(new JavascriptBridge(), "jb");
//Register the WebChromeClient to assist with alerts/debugging
this.webView.setWebChromeClient(new MyWebChromeClient());
//Load assets/html/index.html resource into the WebView control
this.webView.loadUrl("file:///android_asset/www/index.html");
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
final class JavascriptBridge
{
public String callMe(String param1, String param2)
{
//Generate the returnValue from the bridge
String toastValue = param1 + "," + param2;
//Setup the Toast
Toast toast = Toast.makeText(ReturnAndroidValActivity.this, toastValue, Toast.LENGTH_LONG);
//Show the Toast
toast.show();
return toastValue;
}
}
/**
* Provides a hook for calling "alert" from javascript. Useful for
* debugging your javascript.
*/
final class MyWebChromeClient extends WebChromeClient
{
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result)
{
Log.d("JavascriptBridge", message);
resu lt.confirm();
return true;
}
}
}
答案 0 :(得分:1)
在javascript中再定义一个函数:
<script>
function my_callback_function(param){
alert("Called with value: " + param);
}
</script>
然后通过本机代码中的WebView调用此函数,如下所示:
webView.loadUrl("javascript:my_callback_function('TheValue')");