使用安卓应用程序运行脚本

问题描述 投票:0回答:4

我有这个 shell 脚本:

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

我希望用安卓应用运行这个脚本。我现在已经创建了一个只有祝酒词的按钮。我如何获取脚本 (free.sh) 并使用应用程序上的按钮启动它?或者有没有用java重写代码的解决方案?谢谢

android bash shell button
4个回答
2
投票

首先,您将使用 Eclipse 创建一个简单的 Android Hello World 应用程序。并在您的布局中添加一个按钮。这是 Android 开发的一个非常初步的实践,您可以从 http://d.android.com

挖掘更多内容

请在您按钮的

OnClick
回调函数中尝试此代码:

Button b = 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();
             }
        }
 });

1
投票

这里是如何从你的安卓应用程序运行 shell 脚本

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
投票

我的代码有错误:

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
投票

好吧,现在没有错误(谢谢!)但是什么都不做..尝试我写了一个简单的脚本来写一个 file.txt。见代码:

public class MainActivity extends Activity {


    private Button button;

    public void onCreate(Bundle savedInstanceState) {

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

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

没有错误,但是当我按下按钮时,我认为 shell 没有运行所以不要创建 file.txt

© www.soinside.com 2019 - 2024. All rights reserved.