Start a new topic

How Android interacts with Js

How Android interacts with Js

1 person has this problem


Hi, I'm a new developer, so that also means I'm new to Android.

I am familiar with Java and with the explenation of the samples I I understood the Js documents. What I don't quite get is how the Java source files of the example interact with the Js samples. With this I mean, I can't find where they are "called", if they ever finis by themselves or only when I hit return on the phone and how to send parameters and call functions of the Js.

Could anbody help me figure this out or tell me where I can read about it?

Thanks for your help

There are 2 types of communication


Java -> JS

JS -> Java


First one is easy, just use the ArchitectView.callJavascript method. It allows to call any javascript code in the context of the loaded architect world. e.g. any global functions defined in the js file is call able via this method.

From JS to java you have to do following steps:


implement an ArchitectUrlListener and register it with ArchitectView.registerUrlListener(ArchitectUrlListener listener) 

in JS set the "document.location" to any url starting with "archtiectsdk://"

once set, the listener's urlWasInvoked method is called

parse the url (parameters are possible as well) and react accordingly


for example an url can look like this:

document.location = "architectsdk://callactioninJava?id=123";

 

Thanks, it was really helpfull.

Another question, the camera view and render of the drawable objects on it is handled by de js files o by the Java activity? I would appreciate if you could tell me exactly were on the sample app this is made.

Our SDK has a very thin platform layer and allows to create the full AR experience using web technologies (HTML, JavaScript, CSS). The creation of POIs (AR.GeoObject's with attached visual representations) is done in JavaScript. Please have a look at the SDK Samples and the JavaScript API reference for details on how to do accomplish certain things.

document.location = "architectsdk://callactioninJava?id=123";

is the "callactioninJava" is a method name in a java class or a java class name?

What I tried is the following: I have button ("Take me there") in Poi detail panel near the "More" button and when user clicked to the "Take me there" button, I want to call a java method to do some calculation.

in my js file on button click method I have:

var currentMarker = World.currentMarker;
var architectSdkUrlm = "architectsdk://Navigation?id=" + encodeURIComponent(currentMarker.poiData.id) + "&title=" + encodeURIComponent(currentMarker.poiData.title);
document.location = architectSdkUrlm;

where "Navigation" in the architectSdkUrlm is name of the java class that I created for the calculations. Navigation class is:

public class Navigation extends Activity implements ArchitectView.ArchitectUrlListener {
protected ArchitectView architectView;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
architectView.registerUrlListener(this);
}
@Override
public boolean urlWasInvoked(String s) {
Log.d("Hi",s);
return false;
}
}

I want to see if I could call the java file from js by a Log message but I don't get anything.

Do you have any idea about what might be the problem?

Thanks.

Hi there!

Please have a look at the sample app for recommended approaches.
Although I just see the snippets please check that you set the architectView properly (using id from your layout.xml)

Best regards,
Andreas

Could you please tell me which javascript file from wikitude sample has "document.location = "architectsdk://" ? I couldn't find it...

thank you

Note that this document location changes is handled in a special way - the UrlListener will intercept them and report them to your native Android/iOS listener. That way you can pass information from JS to native in an easy way.

Have a look at the native POI detail page sample where the concept is described in more detail.

Best regards,
Andreas
Login or Signup to post a comment