@TargetApi(Build.VERSION_CODES.LOLLIPOP) private boolean unSetWiFiProxySettings5() { try { WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiConfiguration config = getCurrentWifiConfiguration(manager); Class proxySettings = Class.forName("android.net.IpConfiguration$ProxySettings"); Class[] setProxyParams = new Class[2]; setProxyParams[0] = proxySettings; setProxyParams[1] = ProxyInfo.class; Method setProxy = WifiConfiguration.class.getDeclaredMethod("setProxy", setProxyParams); setProxy.setAccessible(true); Object[] methodParams = new Object[2]; methodParams[0] = Enum.valueOf(proxySettings, "NONE"); methodParams[1] = null; setProxy.invoke(config, methodParams); manager.updateNetwork(config); manager.disconnect(); manager.reconnect(); ToastUtil.showToast(context, "取消Proxy设置成功"); return true; } catch (Exception e) { ToastUtil.showToast(context, "取消Proxy设置失败"); return false; } }
@SuppressWarnings("unchecked") private static WifiConfiguration setProxy(WifiConfiguration conf, String hostname, int port, List<String> bypass, PROXY_TYPE type) { try { //linkProperties is no longer in WifiConfiguration Class proxyInfoClass = Class.forName("android.net.ProxyInfo"); Class[] setHttpProxyParams = new Class[1]; setHttpProxyParams[0] = proxyInfoClass; Class wifiConfigClass = Class.forName("android.net.wifi.WifiConfiguration"); Method setHttpProxy = wifiConfigClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams); setHttpProxy.setAccessible(true); //Get the ENUM ProxySettings in IpConfiguration Class ipConfigClass = Class.forName("android.net.IpConfiguration"); Field f = ipConfigClass.getField("proxySettings"); Class proxySettingsClass = f.getType(); Class[] setProxySettingsParams = new Class[1]; setProxySettingsParams[0] = proxySettingsClass; Method setProxySettings = wifiConfigClass.getDeclaredMethod("setProxySettings", setProxySettingsParams); setProxySettings.setAccessible(true); ProxyInfo pi = null; String Type = null; switch (type) { case MANUAL: Type = "STATIC"; pi = ProxyInfo.buildDirectProxy(hostname, port, bypass); break; case PAC_URL: Type = "PAC"; pi = ProxyInfo.buildPacProxy(Uri.parse(hostname)); } //pass the new object to setHttpProxy Object[] params_SetHttpProxy = new Object[1]; params_SetHttpProxy[0] = pi; setHttpProxy.invoke(conf, params_SetHttpProxy); //pass the enum to setProxySettings Object[] params_setProxySettings = new Object[1]; params_setProxySettings[0] = Enum.valueOf((Class<Enum>) proxySettingsClass, Type); setProxySettings.invoke(conf, params_setProxySettings); } catch (Exception e) { e.printStackTrace(); } return conf; }
/** * 高于 Android 5.0 系统使用 */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) private boolean setWiFiProxySettings5(String ip, int port) { try { WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiConfiguration config = getCurrentWifiConfiguration(manager); Class proxySettings = Class.forName("android.net.IpConfiguration$ProxySettings"); Class[] setProxyParams = new Class[2]; setProxyParams[0] = proxySettings; setProxyParams[1] = ProxyInfo.class; Method setProxy = WifiConfiguration.class.getDeclaredMethod("setProxy", setProxyParams); setProxy.setAccessible(true); ProxyInfo proxyInfo = ProxyInfo.buildDirectProxy(ip, port); Object[] methodParams = new Object[2]; methodParams[0] = Enum.valueOf(proxySettings, "STATIC"); methodParams[1] = proxyInfo; setProxy.invoke(config, methodParams); manager.updateNetwork(config); String result = getCurrentWifiConfiguration(manager).toString(); String key = "Proxy settings: "; int start = result.indexOf(key) + key.length(); if (result.substring(start, start + 4).equals("NONE")) { throw new RuntimeException("Can't update the Network, you should have the right WifiConfiguration"); } manager.disconnect(); manager.reconnect(); ToastUtil.showToast(context, "保存Proxy设置成功"); return true; } catch (Exception e) { ToastUtil.showToast(context, "保存Proxy设置失败"); return false; } }
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) static ProxyInfo parseProxyInfo(String host, String port, String bypass, String pacUri) throws ParseException { ProxyInfo proxyInfo = null; if (pacUri != null) { if (!Patterns.WEB_URL.matcher(pacUri).matches()) // PAC URI is invalid { throw new ParseException("Invalid PAC URL format", 0); } Log.d(TAG, "Using proxy auto-configuration URL: " + pacUri); proxyInfo = ProxyInfo.buildPacProxy(Uri.parse(pacUri)); } else if (host != null && !host.isEmpty() && port != null) { int parsedPort; try { parsedPort = Integer.parseInt(port); } catch (NumberFormatException e) { throw new ParseException("Invalid proxy port", 0); } if (bypass != null) { List<String> bypassList = Arrays.asList(bypass.split(",")); Log.d(TAG, "Using proxy <" + host + ":" + port +">, exclusion list: [" + TextUtils.join(", ", bypassList) + "]"); proxyInfo = ProxyInfo.buildDirectProxy(host, parsedPort, bypassList); } else { Log.d(TAG, "Using proxy <" + host + ":" + port +">"); proxyInfo = ProxyInfo.buildDirectProxy(host, parsedPort); } } else if (host != null && port == null) { throw new ParseException("Proxy host specified, but missing port", 0); } // If all values were null, proxyInfo is null return proxyInfo; }
/** * Shows a dialog that asks the user for a host and port, then sets the recommended global proxy * to these values. */ private void showSetGlobalHttpProxyDialog() { if (getActivity() == null || getActivity().isFinishing()) { return; } final View dialogView = getActivity().getLayoutInflater().inflate( R.layout.proxy_config_dialog, null); final EditText hostEditText = (EditText) dialogView.findViewById(R.id.proxy_host); final EditText portEditText = (EditText) dialogView.findViewById(R.id.proxy_port); final String host = System.getProperty("http.proxyHost"); if (!TextUtils.isEmpty(host)) { hostEditText.setText(host); portEditText.setText(System.getProperty("http.proxyPort")); } new AlertDialog.Builder(getActivity()) .setTitle(R.string.set_global_http_proxy) .setView(dialogView) .setPositiveButton(android.R.string.ok, (dialogInterface, i) -> { final String hostString = hostEditText.getText().toString(); if (hostString.isEmpty()) { showToast(R.string.no_host); return; } final String portString = portEditText.getText().toString(); if (portString.isEmpty()) { showToast(R.string.no_port); return; } final int port = Integer.parseInt(portString); if (port > 65535) { showToast(R.string.port_out_of_range); return; } mDevicePolicyManager.setRecommendedGlobalProxy(mAdminComponentName, ProxyInfo.buildDirectProxy(hostString, port)); }) .setNegativeButton(android.R.string.cancel, null) .show(); }