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
- Android执行shell脚本命令(备忘记录与实际应用)
- Linux/shell命令的实际应用——while;do;循环执行脚本
- Linux Shell远程执行命令(命令行与脚本方式)
- android应用程序执行shell脚本命令
- shell 脚本执行命令
- Android 执行shell脚本
- android执行shell脚本
- Android执行shell脚本
- Android shell 脚本执行
- Android执行shell脚本
- Android执行shell脚本
- 如何让Android系统或Android应用执行shell脚本
- php执行shell脚本命令
- shell脚本基础(备忘)
- 实用shell脚本命令记录
- python脚本执行shell命令或脚本
- Android执行shell命令
- Android执行shell命令
- CodeVS1428 棋盘制作
- Scala深入浅出进阶经典 第56讲:Scala中Self Types实战详解
- Struts2 用通配符方式动态调用方法
- mybatis的一级缓存实现详解
- Android解决软键盘遮挡Button
- Android执行shell脚本命令(备忘记录与实际应用)
- 2015年华为面试经历
- JavaScript高级程序设计之基本概念之变量第3.3讲笔记
- 用简单的方法实战演练搭建http服务器(Windows上)------专为http新手
- Scala深入浅出进阶经典 第57讲:Scala中Dependency Injection实战详解
- C/C++ 中的次方运算
- 【Android杂谈】AsyncTask的详细使用解析
- linux之sed用法
- 第107讲:Akka中的Future使用代码实战详解学习笔记