Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run script with android app

i've got this shell script:

#!system/bin/sh
while :
do
sync
echo 3> /proc/sys/vm/drop_caches
echo "Script is been launched"
sleep 30m
done
exit 0;

i wish run this script with an android app. I have already created a button with only a toast for now. How can i take the script (free.sh) and launch it with the button on the app? Or is there a solution to rewrite the code in java? Thanks

like image 878
Atlas91 Avatar asked Apr 08 '13 12:04

Atlas91


People also ask

How do I run a script on Android?

1. On the SureMDM Web Console, navigate to Jobs > New Job > Android > Run Script. 2. In the Run Script prompt, enter a Job Name, Script and click Save.

Can you run .sh on Android?

Even though your device is rooted, android environment won't allow applications to execute 'sh' commands. As android security architecture says, applications run within a secured execution space inside Application Sandbox, sh execution can bypass this security.


1 Answers

First, you will use Eclipse to create a simple Android helloworld app. And add a button in your layout. This is very priliminary practise of Android development which you can dig a lot more from http://d.android.com

please try this code in your button's onclick call back function:

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("PathToYourScript")
                    .start();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if(p!=null) p.destroy();
                }
            }
        });
like image 101
Robin Avatar answered Oct 06 '22 05:10

Robin