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;

	@Override
	public 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() {
		
		@Override
		public 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;
			}
		}
	};
}


转载于:https://www.cnblogs.com/fengju/p/6336132.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值