I am new to python on android and I am trying to buid the small example of a web application found here ( http://pythoncentral.io/python-for-android-using-webviews-sl4a/ ) where a python script opens a web page and communicates with it (you enter a text in the web page and the python script process it and send it back to the web page for display). In my attempt to do this the python script can open the page but there is no dialog between the web page and the script. It seems that the line " var android = new Android() " in the web page doesn't work as in the example.
Here is the code for the webpage :
<!DOCTYPE HTML>
<html>
<head>
<script>
var droid = new Android();
function postInput(input) {
if (event.keyCode == 13)
droid.eventPost('line', input)
droid.registerCallback('stdout', function(e) {
document.getElementById('output').innerHTML = e.data;
});
}
</script>
</head>
<body>
<div id="banner">
<h1>SL4A Webviews</h1>
<h2>Example: Python Evaluator</h2>
</div>
<input id="userin" type="text" spellcheck="false"
autofocus="autofocus" onkeyup="postInput(this.value)"
/>
<div id="output"></div>
<button id="killer" type="button"
onclick="droid.eventPost('kill', '')"
>QUIT</button>
</body>
</html>
and the python code launching the web page above :
import sys, androidhelper
droid = androidhelper.Android()
def line_handler(line):
''' Evaluate user input and print the result into a webview.
This function takes a line of user input and calls eval on
it, posting the result to the webview as a stdout event.
'''
output = str(eval(line))
droid.eventPost('stdout', output)
droid.webViewShow('file:///storage/emulated/0/com.hipipal.qpyplus/scripts/webview1.html')
while True:
event = droid.eventWait().result
if event['name'] == 'kill':
sys.exit()
elif event['name'] == 'line':
line_handler(event['data'])
I really don't understand how the Android() instance in the web page was supposed to work. Thank you for any help ! (I am running qpython with SL4A library on android lollipop)