Android - Easiest way to pass values from HTML or Javascript to Java?
I need to pass some simple values from HTML or Javascript to Java. I have reviewed all the documentation but can't find a straight forward way.
I have a HTML form with a couple of values that are needed in Java. I'm already calling Java from Javascript just not sure how to get the data the other way around.
For instance Java is emailing a local file for me. I need to tell Java the name of the file which is named in Javascript by document.getElementById('CompanyName').value + .html
.
How can I do this?
Here is the current Java code I'm using to email the file: (I need all the "test" phrases to be replaced by values from the form.)
public class JavaScriptInterface {
private WebView mAppView;
private DroidGap mGap;
public JavaScriptInterface (DroidGap gap, WebView view)
{
mAppView = view;
mGap = gap;
}
public void doEmail(){
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("te开发者_开发技巧xt/html");
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT,"test text");
sendIntent.putExtra(Intent.EXTRA_SUBJECT,"test subject");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file:///sdcard/test co.html"));
mGap.startActivity(sendIntent);
}
}
Just add a parameter. In your case I'm assuming that your JavaScript already calls doEmail()
. Change the signature to something like doEmail(String filename)
, use the parameter in the Java method, and when you call it, pass the parameter like:
window.whateverYouCalledThis.doEmail(document.getElementById('CompanyName').value + '.html');
精彩评论