I believe that it's possible to call Java methods from (PhoneGap) Javascript.
Anyone knows how to do that?? (I know how to do it by changing the source code of PhoneGap, but I'd avoid that)
I believe that it's possible to call Java methods from (PhoneGap) Javascript.
Anyone knows how to do that?? (I know how to do it by changing the source code of PhoneGap, but I'd avoid that)
You have to change the source code to PhoneGap. Otherwise, you are limited to the APIs PhoneGap publishes.
I finally made it work.
public class MyClass { private WebView mAppView; private DroidGap mGap; public MyClass(DroidGap gap, WebView view) { mAppView = view; mGap = gap; } public String getTelephoneNumber(){ TelephonyManager tm = (TelephonyManager) mGap.getSystemService(Context.TELEPHONY_SERVICE); String number = tm.getLine1Number(); return number; } }
public class Main extends DroidGap { private MyClass mc; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mc = new MyClass(this, appView); appView.addJavascriptInterface(mc, "MyCls"); super.loadUrl(getString(R.string.url)); } }
$(function(){ $("#phone").text("My telephone number is: " + window.MyCls.getTelephoneNumber()); });
Excelent!, very usefull.
But, is there a way you can call JavaScript functions from Java?