How to call java method from angular and get answer back for Android?


#1

Hello,

I’m angular+android developer and started to play with Ionic.

I googled and goolged but didn’t find any example how to do so. I saw PhoneGap but sounds like Cordova a bit different. Sadly, but from Ionic site I succeeded to run 2 demos where native side is empty and I have no clue how to do that.

Any links, examples…

[EDIT]

I grabbed some example from this site that works for me:

AlertBack.java

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.widget.Toast;
public class AlertBack extends CordovaPlugin  {


@Override
public boolean execute(String action, JSONArray args,
		CallbackContext callbackContext) throws JSONException {
	if (action.equals("alertBack")) {
		

		 Toast.makeText(cordova.getActivity(), "Using Toast You Entered "+
		 args.getString(0), Toast.LENGTH_LONG).show();
		callbackContext.success("Returning from native You Entered "
				+ args.getString(0));
		return true;
	}
	return false; // Returning false results in a "MethodNotFound" error.
  }
}

res/xml/config.xml

...
<feature name="AlertBack">
    <param name="android-package" value="com.oodles.plugin.AlertBack" />
</feature>
...

plugin.js

window.alertBack = function(str, callback) {
  cordova.exec(callback, function(err) {
     callback('Nothing to echo.');
   }, "AlertBack", "alertBack", [str]);
};

implementation:

window.alertBack(userName, function(echoValue) {
    alert(echoValue); 
});

You can see that its JavaScript implementation. I’m looking for Angular way to rewrite plugin.js and it’s call

Thank you


#2

Hello !
There is no way to do that unless you use Cordova / Phonegap (same difference in the API).
Javascript is absolutely incapable of calling Java, wheter you code with Angular, jQuery or any other javascript framework.
There are other framework that fill the gap between native and web APIs, like Titanium.

With Cordova, you can developp your own native Java code and call it from any javascript.
If you stick with Cordova plugins, you have a lot of features available already.

Ionic choose to work together with Cordova and Angular, it’s just a choice.

Does that answer your question ?


#3

Thanks for quick answer.

Generally My goal to convert native Android App. to Ionic+Angular+Cordova App. I saw that Cordova has some APIs. FOr 1st time Im interesting only to pass Strings both directions. Please, see my edit I posted. Maybe it will be a bit clearer. Thanks again.


#4

Hello,

Well this is a plugin for Cordova, why would you want to rewrite this in angular ?
All the data you can manage through your plugin will be passed to your controllers / services in Angular.
If you want to rewrite plugin.js, it means that you want to get “into” Cordova core somehow, from what I understand, since the mandatory stuff in it is “cordova.exec” …

You should take a look at how a “native” cordova plugin does integrate in an Angular-Ionic app, and then you should be able to do the same. But I think this is very “interessting” thing to do.
Init of the plugin call would be somewhere near Platform.ready in Ionic I guess.