android - 使用 Android 应用程序运行脚本

标签 android bash shell button

我有这个 shell 脚本:

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

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

最佳答案

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

请在按钮的 onclick 回调函数中尝试此代码:

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

关于android - 使用 Android 应用程序运行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15879452/

相关文章:

linux - 如何删除名称最大的目录?

bash - 从文件中删除所有以 # 开头的行

bash - 从 xargs 获取原始返回值或模拟 xargs

Linux Shell 脚本 - 检查参数

linux - bash 中的 fork 和 exec

android - 如何像 chrome 一样拆分菜单

android - RecyclerView Item Click Listener 与 DataBinding

android - 启动 Accept Thread 时 Bindlisten 失败

android - webView.canGoBack() 不再工作

python-2.7 - 如何在unix中的多个文件中添加一行