有这么一个需求,需要给手机设置全局代理。百度到的结果都是设置后,如果需要清除代理的话,需要重启手机,这里使用的方式是不需要重启的方式后
实现方式如下:
设置代理:
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 succe***esult = 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(); succe***esult = new BufferedReader(new inputStreamReader(process.getinputStream(), "UTF-8")); errorResult = new BufferedReader(new inputStreamReader(process.getErrorStream(), "UTF-8")); String s; while ((s = succe***esult.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, succe***esult, 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
总结
以上是内存溢出为你收集整理的android-为手机设置全局代理全部内容,希望文章能够帮你解决android-为手机设置全局代理所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)