在android手机上运行shell脚本

本文介绍了一个简单的Android应用程序示例,该程序通过点击按钮的方式执行存储在/system目录下的Shell脚本。需要注意的是,这种方法要求设备已被root,并且脚本文件已获得执行权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class MainActivity extends AppCompatActivity {
    TextView editText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.editText);

        Button startButton = findViewById(R.id.startButton);
        startButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                do_exec("sh /system/a.sh");
            }
        });
    }

    String do_exec(String cmd) {
        String s = "\n";
        try {
            Process p = Runtime.getRuntime().exec(cmd);
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line = "";
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                s += line + "/n";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        editText.setText(s);
        return cmd;
    }


}

xml页面就是一个TextView 和一个按钮

点击按钮,执行shell脚本。


注意:

1.手机必须root

2.shell脚本需要放在/system目录下,并且chmod 777 a.sh,确认成功后才可以执行





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值