Android执行shell脚本命令(备忘记录与实际应用)

        今天MotoG 刷了个美版Android 5.1 系统,刷上后感觉不错,毕竟moto神优化,motog一直在承受着这个价位不该有的流畅和顺滑,哈哈哈。

        话说回来,5.0以后的系统,总是会伴随着一个感叹号问题:wifi或者移动信号一直有个感叹号标志,一直链接不上谷歌服务器(这个你懂的,404 page not found)。下载一个之前用过的去感叹号软件,结果不好使,网上搜索一下,说可以在虚拟终端输入以下命令完成,重启见效:

    su
    settings put global captive_portal_detection_enabled 0

 试了下,效果不错嘿。

于是想想,在剁手不刷机之前,大概还会遇到几次这个问题,又不想每次都这么输命令,呵呵,咱是做开发的呀,自己来。于是就写了个单纯得不能再单纯的软件,专门是去除这个感叹号的。来来来,直接上界面:


界面布局就不上了,直接上Activity代码:

package com.example.warningaway;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity implements OnClickListener,
		OnCheckedChangeListener {

	Button btnRun;
	Button btnReboot;
	CheckBox cbConfirm;
	private final String KEY_CHECK = "check";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView() {
		btnRun = (Button) findViewById(R.id.btn_run);
		btnReboot = (Button) findViewById(R.id.btn_reboot);

		cbConfirm = (CheckBox) findViewById(R.id.cb_confirm);
		cbConfirm.setChecked(getCheckInfo());
		cbConfirm.setOnCheckedChangeListener(this);

		btnRun.setOnClickListener(this);
		btnReboot.setOnClickListener(this);
	}

	private boolean getCheckInfo() {
		SharedPreferences sp = getPreferences(MODE_PRIVATE);
		return sp.getBoolean(KEY_CHECK, false);
	}

	private void setCheckInfo(boolean checked) {
		SharedPreferences sp = getPreferences(MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putBoolean(KEY_CHECK, checked);
		editor.commit();
	}

	private void clickRun() {
		runShell("su -c settings put global captive_portal_detection_enabled 0");
	}

	private void clickReboot() {
		if (cbConfirm.isChecked() == false) {
			DialogCustomView dialog = new DialogCustomView();
			dialog.alertDialog(this, "", getString(R.string.reboot), null,
					new OnClickListener() {

						@Override
						public void onClick(View v) {
							reboot();
						}
					});
		} else {
			reboot();
		}
	}

	private void reboot() {
		runShell("su -c \"/system/bin/reboot\"");
	}

	private String runShell(String cmd) {
		String result = new String();

		Runtime mRuntime = Runtime.getRuntime();
		try {
			// Process中封装了返回的结果和执行错误的结果
			Process mProcess = mRuntime.exec(cmd);
			BufferedReader mReader = new BufferedReader(new InputStreamReader(
					mProcess.getErrorStream()));
			StringBuffer mRespBuff = new StringBuffer();
			char[] buff = new char[1024];
			int ch = 0;
			while ((ch = mReader.read(buff)) != -1) {
				mRespBuff.append(buff, 0, ch);
			}
			mReader.close();

			result = mRespBuff.toString();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btn_run) {
			clickRun();
		} else if (v.getId() == R.id.btn_reboot) {
			clickReboot();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (buttonView.getId() == R.id.cb_confirm) {
			setCheckInfo(isChecked);
		}
	}

}

其中的runShell方法即为运行脚本命令的方法,在此记录一下以备忘。

程序下载链接:点击这里

http://download.csdn.net/detail/junjun071308/9140291


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值