Communication between Android Java and Phonegap Javascript?

I finally made it work Create a class with methods you want to use: 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; } } In your main activity add a Javascript interface for this class: 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)); } } In Javascript call window. MyCls methods: $(function(){ $("#phone").

Text("My telephone number is: " + window.MyCls. GetTelephoneNumber()); }).

I finally made it work. Create a class with methods you want to use: 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; } } In your main activity add a Javascript interface for this class: 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)); } } In Javascript call window.

MyCls methods: $(function(){ $("#phone"). Text("My telephone number is: " + window.MyCls. GetTelephoneNumber()); }).

Hmm, this example is great, but I get a nullpointer Exception on appView, it seems to be null. Where do you intialise the object? – dan Oct 26 at 13:39 It doesn't work unless the methods are public!

Great job! – dan Oct 26 at 14:15.

Zorglab76. AddJavaScriptInterface(mc, "MyCls") without Gap init()ed may cause crush of the app, you'd better add super.init() before addJavascriptInterface() public class Main extends DroidGap { private MyClass mc; @Override public void onCreate(Bundle savedInstanceState) { super. OnCreate(savedInstanceState); super.init(); mc = new MyClass(this, appView); appView.

AddJavascriptInterface(mc, "MyCls"); super. LoadUrl(getString(R.string. Url)); } }.

PhoneGap has a decent Plugin API. You'd write the plugin in Java by implementing the IPlugin interface. Most of the magic is in the execute() function.

Public interface IPlugin { /** * Executes the request and returns PluginResult. * * @param action The action to execute. * @param args JSONArry of arguments for the plugin.

* @param callbackId The callback id used when calling back into JavaScript. * @return A PluginResult object with a status and message. */ PluginResult execute(String action, JSONArray args, String callbackId); // ... more ... } The best way to start writing a plugin is by writing the javascript API first.

You would typical start by writing a custom javascript class, and in each method on the javascript class, marshal the variables and call into the plugin you developed using the Phonegap.exec() method. Here is the method signature for your reference. /* src/com/phonegap/api/PluginManager.

Java */ /** * Receives a request for execution and fulfills it by finding the appropriate * Java class and calling it's execute method. * * PluginManager. Exec can be used either synchronously or async.In either case, a JSON encoded * string is returned that will indicate if any errors have occurred when trying to find * or execute the class denoted by the clazz argument.

* * @param service String containing the service to run * @param action String containt the action that the class is supposed to perform. This is * passed to the plugin execute method and it is up to the plugin developer * how to deal with it. * @param callbackId String containing the id of the callback that is execute in JavaScript if * this is an async plugin call.

* @param args An Array literal string containing any arguments needed in the * plugin execute method. * @param async Boolean indicating whether the calling JavaScript code is expecting an * immediate return value. If true, either PhoneGap.

CallbackSuccess(...) or * PhoneGap. CallbackError(...) is called once the plugin code has executed. * * @return JSON encoded string with a response message and status.

*/ @SuppressWarnings("unchecked") public String exec(final String service, final String action, final String callbackId, final String jsonArgs, final boolean async) You also need to register the Plugin. You do this by adding the registration code at the bottom of your custom javascript library.In the example below, the author defined a javascript BarcodeScanner class and registers it using the addConstructor method. Two steps are carried out in the addConstructor: Create a new instance of BarcodeScanner in javascript and registers it.

This is accessible in javascript as window.plugins. BarcodeScanner Registers the custom Plugin class with a service name. This service name is passed in as the first argument to PhoneGap.

Exec so that PhoneGap can instantiate the java plugin class and call the execute() method on it. Sample registration code: PhoneGap. AddConstructor(function() { /* The following registers an instance of BarcodeScanner in window.plugins.

BarcodeScanner */ PhoneGap. AddPlugin('barcodeScanner', new BarcodeScanner()); /* The following associates a service name BarcodeScanner with a class com.beetight.barcodescanner. BarcodeScanner */ /* The service name is the first argument passed into PhoneGap.

Exec */ PluginManager. AddService("BarcodeScanner","com.beetight.barcodescanner. BarcodeScanner"); }).

You have to change the source code to PhoneGap. Otherwise, you are limited to the APIs PhoneGap publishes.

I've found this for iPhone: groups.google. Com/group/phonegap/browse_thread/thread/… so I hoped something like that is doable in Java too... – zorglub76 Apr 28 '10 at 20:21 My read of the PhoneGap Android source says they took a different approach and do not have the equivalent of PhoneGapCommand. However, PhoneGap knows more about PhoneGap than I do, so would recommend you ask your question on that Google Group and see what they say.

– CommonsWare Apr 28 '10 at 20:34 already did, but no one answered :( – zorglub76 Apr 29 '10 at 7:13 This answer isn't accurate, see the accepted answer above – Russell Davis Jul 15 at 6:12 @Russell Davis: That answer isn't accurate either -- see the plugin answer. – CommonsWare Jul 15 at 11:02.

PhoneGap has a decent Plugin API. You'd write the plugin in Java by implementing the IPlugin interface. Most of the magic is in the execute() function.

The best way to start writing a plugin is by writing the javascript API first. You would typical start by writing a custom javascript class, and in each method on the javascript class, marshal the variables and call into the plugin you developed using the Phonegap.exec() method. Here is the method signature for your reference.

You also need to register the Plugin. You do this by adding the registration code at the bottom of your custom javascript library. In the example below, the author defined a javascript BarcodeScanner class and registers it using the addConstructor method.

Create a new instance of BarcodeScanner in javascript and registers it. This is accessible in javascript as window.plugins. Registers the custom Plugin class with a service name.

Is passed in as the first argument to PhoneGap. Can instantiate the java plugin class and call the execute() method on it.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions