Skip to content Skip to sidebar Skip to footer

Insert A Java String Into Phonegap Javascript

I am trying to insert a java string into phonegap javascript. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String myName = 'Xxxxx'

Solution 1:

Try to pass sting via querystring, like

publicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String myName = "Xxxxx";

    super.loadUrl("file:///android_asset/www/login.html?name=" + myName );
}

take a look here to use querystring on javascript

Solution 2:

In your MainActivity :

publicvoidonCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  JavaScriptInterfaceClassmyInterface=newJavaScriptInterfaceClass(this, appView);
  appView.addJavascriptInterface(myInterface, "Android");

  super.loadUrl("file:///android_asset/www/login.html");
}

Create a new class in your project for instantiating myInterface(works only for phonegap):

publicclassJavascriptInterfaceClass {
    privateWebView mAppView;
    privateDroidGap mGap;

publicJavascriptInterfaceClass(DroidGap gap, WebView view) {
    mAppView = view;
    mGap = gap;
}

publicStringgetMyString() {
  String myName = 'xxxxx';
  return myName;

}

In your WebView:

<scripttype="text/javascript">functiongetString() {
      returnAndroid.getString();
  }
</script>

Post a Comment for "Insert A Java String Into Phonegap Javascript"