有这么一个需求,需要给手机设置全局代理。百度到的结果都是设置后,如果需要清除代理的话,需要重启手机,这里使用的方式是不需要重启的方式后
实现方式如下:
设置代理:
String[] strings = {"settings put global http_proxy " + ip + ":" + port};
ShellUtils.CommandResult commandResult2 = ShellUtils.execCmd(strings, false, true);
直接使用adb shell命令设置,需要权限
<uses-permission android:name="android.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
使用该权限时,需要给应用系统签名
清除代理:
String[] strings = {"settings put global http_proxy :0"}; ShellUtils.CommandResult commandResult2 = ShellUtils.execCmd(strings, false, true);
附上shellutiils代码
package com.sh.util.downloadlib.util;
import android.text.TextUtils;
import android.util.Log;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
/**
* <pre>
* author: carden
* </pre>
*/
public final class ShellUtils {
private ShellUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* 是否是在root下执行命令
*
* @param command 命令
* @param isRoot 是否需要root权限执行
* @return CommandResult
*/
public static CommandResult execCmd(String command, boolean isRoot) {
return execCmd(new String[]{command}, isRoot, true);
}
/**
* 是否是在root下执行命令
*
* @param commands 多条命令链表
* @param isRoot 是否需要root权限执行
* @return CommandResult
*/
public static CommandResult execCmd(List<String> commands, boolean isRoot) {
return execCmd(commands == null ? null : commands.toArray(new String[]{}), isRoot, true);
}
/**
* 是否是在root下执行命令
*
* @param commands 多条命令数组
* @param isRoot 是否需要root权限执行
* @return CommandResult
*/
public static CommandResult execCmd(String[] commands, boolean isRoot) {
return execCmd(commands, isRoot, true);
}
/**
* 是否是在root下执行命令
*
* @param command 命令
* @param isRoot 是否需要root权限执行
* @param isNeedResultMsg 是否需要结果消息
* @return CommandResult
*/
public static CommandResult execCmd(String command, boolean isRoot, boolean isNeedResultMsg) {
return execCmd(new String[]{command}, isRoot, isNeedResultMsg);
}
/**
* 是否是在root下执行命令
*
* @param commands 命令链表
* @param isRoot 是否需要root权限执行
* @param isNeedResultMsg 是否需要结果消息
* @return CommandResult
*/
public static CommandResult execCmd(List<String> commands, boolean isRoot, boolean isNeedResultMsg) {
return execCmd(commands == null ? null : commands.toArray(new String[]{}), isRoot, isNeedResultMsg);
}
/**
* 是否是在root下执行命令
*
* @param commands 命令数组
* @param isRoot 是否需要root权限执行
* @param isNeedResultMsg 是否需要结果消息
* @return CommandResult
*/
public static CommandResult execCmd(String[] commands, boolean isRoot, boolean isNeedResultMsg) {
int result = -1;
if (commands == null || commands.length == 0) {
return new CommandResult(result, null, null);
}
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec(isRoot ? "su" : "sh");
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command == null) continue;
os.write(command.getBytes());
os.writeBytes("\n");
os.flush();
}
os.writeBytes("exit\n");
os.flush();
result = process.waitFor();
if (isNeedResultMsg) {
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
}
} catch (Exception e) {
e.printStackTrace();
Log.e("***",e.getMessage());
} finally {
closeIO(os, successResult, errorResult);
if (process != null) {
process.destroy();
}
}
return new CommandResult(
result,
successMsg == null ? null : successMsg.toString(),
errorMsg == null ? null : errorMsg.toString()
);
}
public static void closeIO(Closeable... closeables) {
if (closeables == null) return;
for (Closeable closeable : closeables) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 返回的命令结果
*/
public static class CommandResult {
/**
* 结果码
**/
public int result;
/**
* 成功信息
**/
public String successMsg;
/**
* 错误信息
**/
public String errorMsg;
public CommandResult(int result, String successMsg, String errorMsg) {
this.result = result;
this.successMsg = successMsg;
this.errorMsg = errorMsg;
}
@Override
public String toString() {
return "CommandResult{" +
"result=" + result +
", successMsg='" + successMsg + '\'' +
", errorMsg='" + errorMsg + '\'' +
'}';
}
}
public static String[] getHideShell(String shells) {
String[] c_shells = null;
if (!TextUtils.isEmpty(shells)) {
return null;
}
if (!shells.contains("#")) {
c_shells = new String[1];
for (int i = 0; i < c_shells.length; i++) {
c_shells[i] = shells;
}
return c_shells;
} else {
String strs[] = shells.split("#");
c_shells = new String[strs.length];
for (int i = 0; i < strs.length; i++) {
c_shells[i] =strs[i];
}
return c_shells;
}
}
}
thanks