Re: Executing my own Python scripts in Android using Eclipse

246 views
Skip to first unread message

Archu

unread,
Sep 25, 2012, 8:49:23 AM9/25/12
to python-fo...@googlegroups.com
Yes i am now able to execute my own scripts. Just I am putting in a file and passing the File object to launchScript() method.

On Tuesday, September 25, 2012 4:56:59 AM UTC+3, Prat wrote:
Hi Archu,

I am working on a similar issue. Did you have any luck with python scripts in eclipse for android app?

Regards,

Prat

On Wednesday, September 12, 2012 6:01:55 AM UTC-5, Archu wrote:
Hi, I m new to Android. I used the link  http://jokar-johnk.blogspot.fi/2011/02/how-to-make-android-app-with-sl4a.html to execute Python script in Android. However, I need to run my own script(after parsing from a JSON file). In my case, I cannot place the script in /res/raw folder. Please let me know the modifications in ScriptService.java. Any help will be appreciated. Thanks in advance !

Anthony Prieur

unread,
Sep 25, 2012, 8:59:40 AM9/25/12
to python-fo...@googlegroups.com
Alternatively you can just put your scripts in a .zip, put the .zip in
res/raw folder, fix variable name:
https://code.google.com/p/android-python27/source/browse/apk/src/com/android/python27/config/GlobalConstants.java

"Run as -> Android Application", and you're done.

Archu

unread,
Sep 26, 2012, 6:36:08 AM9/26/12
to python-fo...@googlegroups.com
Hi Anthony,

I am parsing a JSON(for my Python scripts) and able to see the output in emulator. I want to modify the JSON fields and post it back to the server. I used startActivityForResult() while calling the ScriptActivity(so that I can define HTTP Post method in onActivityResult()). But I want to know when my service(Script Service) gets over.?  I can define my HTTP Post only after actual execution of my script Is there a means to do the same?

Regards,
Archana

Archana

unread,
Sep 26, 2012, 7:12:12 AM9/26/12
to python-fo...@googlegroups.com
Please find attached my logs.. I want to know from which class the final lines are getting printed?

Thanks in advance!!
log_PythonInAndroid.docx

Anthony Prieur

unread,
Sep 26, 2012, 7:46:22 AM9/26/12
to python-fo...@googlegroups.com
When your script ends:

@Override
public void run() {

Is executed, see line 195 of
https://code.google.com/p/android-python27/source/browse/apk/src/com/android/python27/ScriptService.java

Archana r

unread,
Sep 26, 2012, 7:51:57 AM9/26/12
to python-fo...@googlegroups.com
Hi,
Actually I want to know, after ScriptLauncher.launchScript(), where(from which class) does the following get printed?


09-26 09:41:11.881: D/dalvikvm(6427): mprotect(RO) failed (13), file will remain read-write

09-26 09:41:11.891: D/dalvikvm(6427): mprotect(RO) failed (13), file will remain read-write

09-26 09:41:11.891: D/dalvikvm(6427): mprotect(RO) failed (13), file will remain read-write

09-26 09:41:11.891: D/dalvikvm(6427): mprotect(RO) failed (13), file will remain read-write

09-26 09:41:11.901: D/dalvikvm(6427): mprotect(RO) failed (13), file will remain read-write

09-26 09:41:11.901: D/dalvikvm(6427): mprotect(RO) failed (13), file will remain read-write

09-26 09:41:11.911: D/dalvikvm(6427): mprotect(RO) failed (13), file will remain read-write

09-26 09:41:12.071: D/dalvikvm(6427): mprotect(RO) failed (13), file will remain read-write

09-26 09:41:12.461: D/dalvikvm(6427): No JNI_OnLoad found in /system/lib/libandroid_runtime.so 0x0, skipping init

09-26 09:41:14.111: V/sl4a.SimpleServer$ConnectionThread:89(6411): Server thread 15 started.

09-26 09:41:14.571: D/dalvikvm(6411): GC_CONCURRENT freed 509K, 55% free 2862K/6279K, external 410K/517K, paused 5ms+5ms

09-26 09:41:15.011: D/dalvikvm(6411): GC_CONCURRENT freed 478K, 54% free 2893K/6279K, external 410K/517K, paused 5ms+6ms

09-26 09:41:15.161: D/dalvikvm(6411): GC_FOR_MALLOC freed 189K, 54% free 2892K/6279K, external 410K/517K, paused 38ms

09-26 09:41:15.161: I/dalvikvm-heap(6411): Grow heap (frag case) to 5.777MB for 8208-byte allocation

09-26 09:41:15.251: D/dalvikvm(6411): GC_FOR_MALLOC freed 4K, 55% free 2895K/6343K, external 410K/517K, paused 75ms

09-26 09:41:15.251: V/sl4a.JsonRpcServer:74(6411): Received: {"id":1,"method":"_authenticate","params":["c7b9507b-6249-4ea9-9776-87fd801c9932"]}

09-26 09:41:15.281: V/sl4a.JsonRpcServer:117(6411): Sent: {"error":null,"id":1,"result":true}

09-26 09:41:15.301: V/sl4a.JsonRpcServer:74(6411): Received: {"id":2,"method":"makeToast","params":["hello javascript15"]}

09-26 09:41:15.351: V/sl4a.JsonRpcServer:117(6411): Sent: {"error":null,"id":2,"result":null}

09-26 09:41:15.401: V/sl4a.JsonRpcServer:74(6411): Received: {"id":3,"method":"notify","params":["javaScript","Success"]}

09-26 09:41:15.451: V/sl4a.JsonRpcServer:117(6411): Sent: {"error":null,"id":3,"result":null}

09-26 09:41:15.561: D/dalvikvm(6427): GC_EXPLICIT freed 707K, 55% free 648K/1411K, external 0K/0K, paused 106ms

09-26 09:41:15.611: D/dalvikvm(6427): GC_CONCURRENT freed 15K, 56% free 633K/1411K, external 0K/0K, paused 1ms+2ms

09-26 09:41:15.811: V/sl4a.SimpleServer$ConnectionThread:100(6411): Server thread 15 died.

09-26 09:41:15.822: V/sl4a.Process$1:135(6411): Process 6427 exited with result code 0.



Thank you!!

--
Regards,
Archana

Anthony Prieur

unread,
Sep 26, 2012, 7:53:06 AM9/26/12
to python-fo...@googlegroups.com
Add some printf and check the exec path.

Archana r

unread,
Sep 26, 2012, 7:58:59 AM9/26/12
to python-fo...@googlegroups.com
Can u pls explain? I m not getting what you are saying
--
Regards,
Archana

Anthony Prieur

unread,
Sep 26, 2012, 8:13:36 AM9/26/12
to python-fo...@googlegroups.com
ScriptLauncher.launchScript last arguments is a callback called when
the script ended:

ScriptLauncher.launchScript(script, mInterpreterConfiguration,
mProxy, new Runnable() {
@Override
public void run() {
// here your script ended
}
});

So do whatever you need in run(), add some print in the Java code to see better.

Archana r

unread,
Sep 26, 2012, 8:33:57 AM9/26/12
to python-fo...@googlegroups.com
Ya am able to see the print statements. Thanks for the clarification.  I want to post the updated JSON(after script execution) to a server. Now I m calling ScriptActivity from my ownActivity using startActivityForResult(). Can I define post method in onActivityResult() in my own activity?
--
Regards,
Archana

Archana

unread,
Sep 27, 2012, 5:29:55 AM9/27/12
to python-fo...@googlegroups.com
Hi Anthony,

Can I write the output of my script to a new file(in phone's internal memory)? Basically, I need to get the result from the script(like simple calculation). Can you also let me know of more sample Python scripts?

Thank you!

Archana

unread,
Sep 27, 2012, 7:12:28 AM9/27/12
to python-fo...@googlegroups.com
My script is like:
import android   
droid = android.Android() 
score1 = 10 
score2 = 20 
average = (score1 + score2)/2 
droid.makeToast('Result is: ' +str(average))

Can I get the value of average and store it in a file(in internal memory)? Please let me know if it is possible to do it?


Anthony Prieur

unread,
Sep 27, 2012, 7:29:33 AM9/27/12
to python-fo...@googlegroups.com
1) You could dump the result in a file after the toast:

f = open("myfile", "wb")
f.write(str(average))
f.close()

You can use also pickle module to dump more complex data structure:
http://docs.python.org/library/pickle.html

2) You can dump the result in a database:
http://docs.python.org/library/sqlite3.html

3) you can post the result to the SL4A server, using you own facade to
manage it: https://code.google.com/p/android-python27/wiki/TutorialHowToCreateCustomRpcFacade

I would use 3)

Archana r

unread,
Sep 27, 2012, 8:41:28 AM9/27/12
to python-fo...@googlegroups.com
Hi,

Can you explain / provide an example for the III option please? 
--
Regards,
Archana

Archana r

unread,
Sep 27, 2012, 9:57:09 AM9/27/12
to python-fo...@googlegroups.com
Hi Anthony, 

I edited my script as:

import android <br/>  droid = android.Android() <br/> score = var1 <br/> score1 = ccc <br/> 
score2 = rreference2<br/>score3 = 10<br/>average = (score + score1 + score2 + score3) <br/>
droid.makeToast('Result is: ' +str(average))<br/>droid.Sample(str(average))

In ScriptService.java, added the code:

public void Sample(@RpcParameter(name="message") String message) throws InterruptedException
{
mLatch.await();
Log.d("Received"+message);
}

And I am able to see 09-27 13:51:14.034: V/sl4a.JsonRpcServer:74(8647): Received: {"params": ["5808"], "id": 2, "method": "Sample"} in the LogCat. Does it mean I have written  the result to the SL4Aserver? How do I get the value (of average) to be updated in JSON. Please let me know if I am doing correctly?
--
Regards,
Archana

Archana r

unread,
Sep 27, 2012, 10:03:21 AM9/27/12
to python-fo...@googlegroups.com
The logs actually show:

09-27 13:51:14.034: V/sl4a.JsonRpcServer:74(8647): Received: {"params": ["5808"], "id": 2, "method": "Sample"}
09-27 13:51:14.064: V/sl4a.JsonRpcServer:117(8647): Sent: {"error":"com.googlecode.android_scripting.rpc.RpcError: Unknown RPC.","id":2,"result":null}
--
Regards,
Archana

Archana r

unread,
Sep 28, 2012, 5:45:31 AM9/28/12
to python-fo...@googlegroups.com
Hi,

In case1, where the file will be stored? How will I be able to access the result? I actually want to write the result to internal memory. Can you please guide me? 

Thank you!

--
Regards,
Archana

Anthony Prieur

unread,
Sep 28, 2012, 6:01:41 AM9/28/12
to python-fo...@googlegroups.com
> In case1, where the file will be stored?

That's up to you to choose where you want to store your file.

> How will I be able to access the result?

Let's say you store your data in the file this way:

MYKEY = MYVALUE

From Python you read such a file with e.g.
http://docs.python.org/library/configparser.html

From Java smth like:

File f = new File(pathToMyFile);
Properties pro = new Properties();
pro.load(new FileInputStream(f));
Log.info("MYKEY =" + pro.getProperty("MYVALUE"));


On Fri, Sep 28, 2012 at 11:45 AM, Archana r

Anthony Prieur

unread,
Sep 28, 2012, 6:03:17 AM9/28/12
to python-fo...@googlegroups.com
>Log.info("MYKEY =" + pro.getProperty("MYVALUE"));

Read:

Log.info("MYKEY =" + pro.getProperty("MYKEY"));


On Fri, Sep 28, 2012 at 12:01 PM, Anthony Prieur

Archana r

unread,
Sep 28, 2012, 6:27:21 AM9/28/12
to python-fo...@googlegroups.com
Hi,

Basically I have to write the result of my script to a file in the phone's internal memory(Let me know, if we can create a file in phone's memory and write data to it). I need to access the file later in my code. My script is now: 

import android <br/>  
droid = android.Android() <br/>
score = 10 <br/> score1 = 20<br/>
score2 = 30<br/>
average = (score + score1 + score2 )<br/>
droid.makeToast('Result is: ' +str(average))<br/>
f=open('sample','wb')<br/>
f.write(str(average))<br/>
f.close()
--
Regards,
Archana

Robbie Matthews

unread,
Sep 29, 2012, 7:53:50 PM9/29/12
to python-fo...@googlegroups.com
Go look at the Python web page, there's lots and lots about writing and reading files.
Generally, it's a good idea to provide a fully qualfied path, Under android, "/sdcard" is usually pretty safe.
ie:
"/sdcard/myfile.txt"

You can find out the current folder in python as follows:

import os; os.getcwd()

Also, read this page,
http://developer.android.com/guide/topics/data/data-storage.html

Particularly the section of 'External Storage'.

Robbie
--

Beginning Android Tablet Programming

  • Publication Date: November 2, 2011
  • ISBN13: 978-1-4302-3783-9

Robbie Matthews gives a full, no-holds barred introduction to Android  programming for tablets. No previous experience required! Learn More …

Beginning Android Tablet Programming


Archana

unread,
Oct 1, 2012, 3:56:59 AM10/1/12
to python-fo...@googlegroups.com
I m executing both PythonScript and JavaScript(simple program to calculate average) using SL4A. I need the results(average values) in my MainActivity (from where I call ScriptActivity). Right now I am using,

import android <br/>droid = android.Android() <br/> score = 10 <br/> score1 = 20 <br/>
score2 = 30<br/>score3 = 10<br/>average = (score + score1 + score2 + score3)/4 <br/>
droid.notify('Result is: ','' +str(average))<br/>
f=open('/data/data/com.example.downloadinbackground/files/result.txt','wb')<br/>f.write(str(average))<br/>f.close()

How do I do the same for JavaScript?
Is there a generic way by which I can get the results (for both of my scripts) or should I write the file to internal memory individually? 


Thank you!

Archana r

unread,
Oct 1, 2012, 9:19:22 AM10/1/12
to python-fo...@googlegroups.com

Hi,

I just wanted to know if there is a way to get the output of PythonScript in class MainActivity.java where the ScriptService is being called(without writing the output to a file)? Please let me know if this can be done.

Thank you!

--
Regards,
Archana

Archana r

unread,
Oct 2, 2012, 4:23:57 AM10/2/12
to python-fo...@googlegroups.com
Hi Anthony,

I extracted the script.jar and added my class in FacadeConfiguration.java. How do I compile this and my new Facade java file? and where(in which path) should I put my new Facade class?

Please also let me know if this can be done for Javascript also(using RhinoForAndroid).. Please guide me in this regard.

Thank you,

On Mon, Oct 1, 2012 at 10:56 AM, Archana <ramalinga...@gmail.com> wrote:



--
Regards,
Archana
Reply all
Reply to author
Forward
0 new messages