Run script with android app

2019-03-13 21:38发布

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

4条回答
Luminary・发光体
2楼-- · 2019-03-13 21:45

Here is how you can run 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();
        }
查看更多
不美不萌又怎样
3楼-- · 2019-03-13 21:49

Well, now No errors (THANK YOU!) but do nothing..to try i've written a easy script that write a file.txt. See the 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();
            }
        }
    });
}
}

The are no errors but when i press the button i think the shell doesn't go so don't create the file.txt

查看更多
Root(大扎)
4楼-- · 2019-03-13 21:59

I've got errors in my code:

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
    }
}
查看更多
Ridiculous、
5楼-- · 2019-03-13 22:07

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();
                }
            }
        });
查看更多
登录 后发表回答