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

来源:互联网 发布:软件代理加盟协议 编辑:程序博客网 时间:2024/06/16 09:32

        今天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";@Overrideprotected 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() {@Overridepublic 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;}@Overridepublic void onClick(View v) {if (v.getId() == R.id.btn_run) {clickRun();} else if (v.getId() == R.id.btn_reboot) {clickReboot();}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (buttonView.getId() == R.id.cb_confirm) {setCheckInfo(isChecked);}}}

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

程序下载链接:点击这里

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


0 0
原创粉丝点击