Possible for android app to get value from script

invisible.hippo

Arch-Supremacy Member
Joined
Nov 8, 2008
Messages
17,902
Reaction score
315
Hi, is it possible for an android application to execute a script and then retrieve the value in the script like this:

Code:
Button b = (Button)findViewById(R.id.buttonPower);
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Process p=null;
                try {
                    p = new ProcessBuilder()
                    .command("/sdcard/Script/test.sh")
                    .start();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if(p!=null) p.destroy();
                }
            }
        });


this is the script:
Code:
#!/bin/sh
HW="Hello World!"

Is it possible to pass "HW" back to the android app?

I would like to ask one more question, has anyone tried before Kivy. I understand that it works in the following way. Please correct me if i am wrong.
It will import and existing android app and from there i can add python scripting into it and final package it back as a new android app?
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
Hi, is it possible for an android application to execute a script and then retrieve the value in the script like this:

Code:
Button b = (Button)findViewById(R.id.buttonPower);
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Process p=null;
                try {
                    p = new ProcessBuilder()
                    .command("/sdcard/Script/test.sh")
                    .start();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if(p!=null) p.destroy();
                }
            }
        });


this is the script:
Code:
#!/bin/sh
HW="Hello World!"

Is it possible to pass "HW" back to the android app?

I would like to ask one more question, has anyone tried before Kivy. I understand that it works in the following way. Please correct me if i am wrong.
It will import and existing android app and from there i can add python scripting into it and final package it back as a new android app?

Code:
Process p = ....;
p.getOutputstream();

Code:
#!/bin/sh
HW="Hello World!"
echo $HW

You wouldn't get the variable name in the way above

But you can also do this for your external script
Code:
#!/bin/sh
HW="Hello World!"
set

Code:
$ set
PIPESTATUS=([0]="0")
PPID=2120
PROMPT_COMMAND='echo -e "\033]0;${USER}@${HOSTNAME}\007\c"'
PS1='\h:\W \u\$ '
PS2='> '
PS4='+ '
PWD=/Users/xxxxxx
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=1
SSH_AUTH_SOCK=/tmp/launch-uMLk64/Listeners
TERM=xterm-color
TMPDIR=/var/folders/ds/ctmnt0cn7z16b42clkk7zjs00000gn/T/
UID=123
USER=xxxxxx

$ (A_VAR_NAME=SOME_VALUE; set | grep VAR)
A_VAR_NAME=SOME_VALUE

The "set" shell command with no arguments will list out all environment variables in the current process. You can then parse them easily. STDIN, STDOUT, STDERR are the basic file descriptors you can play with interprocess communication.

As for kivy, i will let others share more on it. :)
 
Last edited:

ykgoh

Master Member
Joined
Jan 1, 2000
Messages
2,782
Reaction score
0
API wise, this seems possible. However, the program or command to run should be an executable. In your case your command should look like:

Code:
bash /sdcard/Script/test.sh

See: http://android-er.blogspot.sg/2014/02/run-linux-command-with-processbuilder.html

Once you call the start method, you have to enter a loop to read the output from the command like a string. See the link above for example.

In any case, you need to use the sample skeleton to try it out. There are several unknown issues:

  1. whether bash/sh is installed
  2. whether you can invoke bash/sh as a command due to security reason
  3. whether you need to chmod +x your shell script
 

ykgoh

Master Member
Joined
Jan 1, 2000
Messages
2,782
Reaction score
0
Anyway think over carefully your requirements and why you would want to introduce Python into an existing native Android project.

If you're building something new from scratch, and have no Python libraries or codes that you need to reuse, it's not a good reason. I am sure that whatever Kivy can do, a native app should be able to do too.

By developing on native platform, you can take advantage of the latest Android innovations. Kivy may or may not wrap and expose these new features. Just my 2 cents.
 
Last edited:

invisible.hippo

Arch-Supremacy Member
Joined
Nov 8, 2008
Messages
17,902
Reaction score
315
Hi, im trying to run my script from android terminal however i encounter error "no found" and "directory does not exist" when i run this script. However when i run the individuals command from android terminal, there is no error. May i know what is the reason. Thank you.

I run from this command:
sh myscript.sh


Code:
    #!/system/bin/sh
    su
    LD_LIBRARY_PATH=/data/data/com.bcmon.bcmon/files/libs
    LD_PRELOAD=/data/data/com.bcmon.bcmon/files/libs/libfake_driver.so sh
    cd /data/data/com.bcmon.bcmon/files/tools
 

ykgoh

Master Member
Joined
Jan 1, 2000
Messages
2,782
Reaction score
0
Hi, im trying to run my script from android terminal however i encounter error "no found" and "directory does not exist" when i run this script. However when i run the individuals command from android terminal, there is no error. May i know what is the reason. Thank you.

I run from this command:
sh myscript.sh


Code:
    #!/system/bin/sh
    su
    LD_LIBRARY_PATH=/data/data/com.bcmon.bcmon/files/libs
    LD_PRELOAD=/data/data/com.bcmon.bcmon/files/libs/libfake_driver.so sh
    cd /data/data/com.bcmon.bcmon/files/tools

Why not you try running ls or pwd from the app to see where you are and what files can be seen? And do you need the phone to be rooted in order to browse the file system?

Quite honestly, when you try to execute su, it is a red flag. I strongly believe that Android would not permit su or sudo from an app with its security model.
 

invisible.hippo

Arch-Supremacy Member
Joined
Nov 8, 2008
Messages
17,902
Reaction score
315
Why not you try running ls or pwd from the app to see where you are and what files can be seen? And do you need the phone to be rooted in order to browse the file system?

Quite honestly, when you try to execute su, it is a red flag. I strongly believe that Android would not permit su or sudo from an app with its security model.

pardon me if i am not good at explaining. It is quite confusing. Actually my app is calling up another app(app2). App2 will then call up terminal emulator and making certain configuration so that i can use the installed functions in terminal emulator (basically app2 is just a app that install tools to android and configure the needed environment(something like vm).)

I am thinking since i have the tools installed and i just need to load the script of app2 in terminal. My app can call terminal emulator directly configure just like app2 and at the same time i can pass in my script directly from my app bypassing app2

i saw that people have managed to do it.
http://forum.xda-developers.com/showthread.php?t=2405208

however when i run all the lines in the code block from command line directly, there is no error, but if i place them in a script and run the script, i encountered error like no found.

My guess is that most likely the commands are only recognised in command line.
 
Last edited:

invisible.hippo

Arch-Supremacy Member
Joined
Nov 8, 2008
Messages
17,902
Reaction score
315
Hi, now that i managed to run my script in android terminal emulator. I would like to embed the terminal in my apk like this (2.35mintue)

. How do i do it?
Thank you
 

invisible.hippo

Arch-Supremacy Member
Joined
Nov 8, 2008
Messages
17,902
Reaction score
315
usually people embed by api? But in this case the object i want to embed is apk, how can i do this?
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top