send events from python to javascript using sl4a
Asked Answered
K

2

6

I wanted to know the answer to a simple question but i have'nt found a good one (i've google it for hours :) )

I'm playing with the sl4a with python and i can send events from js to the python script, but the js is not catching the eventPost i put in the code below from python to js.

Anyone knows how is this been done or if there is another way without the registerCallback?

HTML CODE :

<html>
<head>
<script>
var droid = new Android();
function doit(){
    droid.makeToast("Text send :=>"+document.getElementById("msg").value);
    droid.eventPost("doit",document.getElementById("msg").value);
}

function alert_me(data){
    droid.makeToast("All done!");
    document.getElementById("msg").value = '';
}

droid.registerCallback("done",alert_me);
</script>
</head>
<body>
<input type="text" name="boton" id="msg" value="" />
<input type="button" name="boton" value="Go!" onclick="javascript:doit()" />
</body>
</html>

PYTHON CODE:

import android,time

if __name__ == '__main__' :

    droid = android.Android()
    droid.webViewShow("file:///sdcard/sl4a/scripts/sample.html")

    while True:        
        event = droid.eventWait().result
        if event["name"] == 'doit':
          droid.makeToast("Event catched! %s" % event['data'])

          droid.eventPost("done","Done message")
          time.sleep(2)


    droid.exit()
Kokanee answered 22/8, 2011 at 23:10 Comment(1)
Your JS alert_me function makes no use of the argument it receives. If it did, the arg would have a member called data, so data.data in your function, which would evaluate to "Done Message".Ovoid
O
1

This is simple to get working, but isn't obvious or well documented.

First you want to get a hook to the Android object inside the webview. Then you can use it to register one or more callbacks. For a simple example, we'll just do one that pops an alert with a message from Python.

    var droid = new Android();

    droid.registerCallback("echo", function(msg) {
        alert(msg.data)
    });

In this case, echo is the name of the event type you want this callback to handle. So this will handle 'echo events'. The event names are arbitrary strings, just call them whatever makes sense.

In the Python script that launched the webview, you can now post events to the registered handler whenever you like.

droid.eventPost("echo", "hello world")

The second argument here is the message you want to pass to the JavaScript callback.

Note that although you pass the message back as a string, it arrives in the JavaScript function as an object. That object, we're calling it msg above, has an attribute called data which contains the string you passed from the Python side.

Ovoid answered 29/10, 2012 at 11:22 Comment(0)
S
0

Unfortunately I have never personally been able to get this working, using both registerCallback() and eventWaitFor(). However, if you are still keen on getting this working, I strongly recommend you head on over and download sl4a_r5x – an unofficial but newer and updated release of SL4A. In it is support for using FullScreenUi's based off the same xml code that native Android apps use. With this you can do what you're after and examples can be found on the page.
Hopefully this has been helpful and you're still interested in SL4A!

Sawyers answered 23/12, 2011 at 17:28 Comment(2)
actually the script above works fine :) i'm using that version of the sl4a but i'm still waiting until the fullscreenUI becomes more efficient parsing some xml properties that did not work by now. Today the more efficient way to program android are still java or c# with the mono..cause the gui with python without a webstyle is a nightmare. Thanks for your answerKokanee
It's trivial to use something like jQuery Mobile UI to build an interface, if you don't need it to look native.Ovoid

© 2022 - 2024 — McMap. All rights reserved.