I am currently designing a native android application. I am planning to use jQuery Mobile web view as my interface and do all the calculations with java back-end. (still deciding using phonegap or not)
I have some difficulties implementing a page that allows a user to fill in a form and pass the variable to the android java part.
Researching all morning, I have learned how to interact between javascript/html and java with addJavascriptInterface(). But the only thing I can find that answer my question is with JSON. That seems a little complicated. Is there a way I can pass the variable as a parameter of a java function?
(I learned that if I do not use a web view, I can simply use getText() or getSelectedItem() with default UI to do what I want to)
I apologize there is no code avaliable, since this is still in designing stage, and I am a little new to android sdk.
Thanks
OK, here's an example of interacting with the javascript interface...
Setting the javascript interface in your Activity...
JavascriptInterface javasriptInterface = new JavascriptInterface(this);
webview.addJavascriptInterface(javasriptInterface, "Android");
The inner JavascriptInterface class in your Android Activity...
public class JavascriptInterface {
Context mContext;
JavascriptInterface(Context c) {
mContext = c;
}
public boolean doSomething(String name, String address) {
...
return true;
}
}
EDIT: Your form will have various input fields. Example...
<form name="myForm" ...>
<input type=text name=personName>
<input type=text name=personAddress>
<input type="button" value="Do it" onClick="callDoSomething()" />
</form>
<script type="text/javascript">
function callDoSomething() {
var theName = document.myForm.personName.value;
var theAddress = document.myForm.personAddress.value;
var result = Android.doSomething(theName, theAddress);
}
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With