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,确认成功后才可以执行
本文介绍了一个简单的Android应用程序示例,该程序通过点击按钮的方式执行存储在/system目录下的Shell脚本。需要注意的是,这种方法要求设备已被root,并且脚本文件已获得执行权限。
1383

被折叠的 条评论
为什么被折叠?



