Run the script using an Android application

I have 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 want to run this script using an android application. At the moment, I have already created a button with a toast. How can I take script (free.sh) and run it using a button in an application? Or is there a solution to rewrite code in java? Thanks

+5
source share
4 answers

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

try this code in your onclick call back function button:

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();
                }
            }
        });
+2
source

I have errors. this is the code:

package com.mkyong.android;

import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.example.toast.R;

public class MainActivity extends Activity {


private Button button;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab1);

    button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {


              Process process = new ProcessBuilder()
               .command("PATH")
               .redirectErrorStream(true)
               .start();
           try {
             InputStream in = process.getInputStream();
             OutputStream out = process.getOutputStream();

             readStream(in);// Here: Syntax error, insert "}" to complete `Block`

            finally {
             process.destroy();
           }
         }

          }
    }); //Here: Syntax error on token "}", delete this token
}

protected void readStream(InputStream in) {
    // TODO Auto-generated method stub

}
}
0

Well, now there are no errors (THANKS!), But do nothing ... To try, I wrote a simple script that writes file.txt. See code:

package com.mkyong.android;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;  
import java.io.IOException;

import com.example.toast.R;

public class MainActivity extends Activity {


private Button button;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab1);

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

There are no errors, but when I click the button, I think the shell does not go, so do not create the .txt file

0
source

Here's how you can run a shell script from your Android app.

try {
            Process process = Runtime.getRuntime().exec("sh /sdcard/test.sh");
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String listOfFiles = "";
            String line;
            while ((line = in.readLine()) != null) {
                listOfFiles += line;
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
0
source

All Articles