日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Java/Android中实现Shell命令

發布時間:2025/3/20 Android 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java/Android中实现Shell命令 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

有時候我們需要實現一個功能。不過這個功能用我們傳統的Java代碼實現起來會有一些困難,這時我們可以嘗試利用Shell命令來實現。你可以按照下面的代碼模塊來進行你想要實現的Shell命令(注:也不是所有的Shell命令都能用Java代碼來實現)。

public class MainActivity extends Activity {private final String TAG = "---MainActivity---";public final String SHELL_0 = "busybox ps";public final String SHELL_1 = "su";public final String SHELL_2 = "cd mnt/shell/emulated/0/.ZFSafeFS";public final String SHELL_3 = "busybox mount .a1.img ./.abc";public final String SHELL_4 = "busybox umount ./.abc";public final String EXECUTE_SHELL = SHELL_0;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button sendButton = (Button) findViewById(R.id.send_btn);Button suButton = (Button) findViewById(R.id.su_button);Button cdButton = (Button) findViewById(R.id.alert_path_button);Button mountButton = (Button) findViewById(R.id.mount_button);Button umountButton = (Button) findViewById(R.id.umount_button);suButton.setText(SHELL_1);cdButton.setText(SHELL_2);mountButton.setText(SHELL_3);umountButton.setText(SHELL_4);sendButton.setOnClickListener(viewOnClickListener);suButton.setOnClickListener(viewOnClickListener);cdButton.setOnClickListener(viewOnClickListener);mountButton.setOnClickListener(viewOnClickListener);umountButton.setOnClickListener(viewOnClickListener);handleShell(EXECUTE_SHELL);}private void handleShell(String shell) {TextView textView = (TextView) findViewById(R.id.textview);Process p = null;try {p = Runtime.getRuntime().exec(shell);if (p == null) {textView.setText("p == null");} else {BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));int read = 0;char[] buffer = new char[4096];StringBuffer output = new StringBuffer();Log.i(TAG, "read = " + read);Log.i(TAG, "output = " + output);while((read = bufferedReader.read(buffer)) > 0) {output.append(buffer, 0, read);}bufferedReader.close();String string = String.copyValueOf(buffer);Log.i(TAG, "string = " + string);textView.setText("result:\n" + string);}} catch (IOException e) {textView.setText("e=" + e);e.printStackTrace();}}private OnClickListener viewOnClickListener = new OnClickListener() {@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.su_button:handleShell(SHELL_1);break;case R.id.alert_path_button:handleShell(SHELL_2);break;case R.id.mount_button:handleShell(SHELL_3);break;case R.id.umount_button:handleShell(SHELL_4);break;default:break;}}}; }

總結

以上是生活随笔為你收集整理的Java/Android中实现Shell命令的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。